Blame gio/tests/gdbus-connection.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 <sys/types.h>
Packit ae235b
Packit ae235b
#include "gdbus-tests.h"
Packit ae235b
Packit ae235b
/* all tests rely on a shared mainloop */
Packit ae235b
static GMainLoop *loop = NULL;
Packit ae235b
Packit ae235b
#if 0
Packit ae235b
G_GNUC_UNUSED static void
Packit ae235b
_log (const gchar *format, ...)
Packit ae235b
{
Packit ae235b
  GTimeVal now;
Packit ae235b
  time_t now_time;
Packit ae235b
  struct tm *now_tm;
Packit ae235b
  gchar time_buf[128];
Packit ae235b
  gchar *str;
Packit ae235b
  va_list var_args;
Packit ae235b
Packit ae235b
  va_start (var_args, format);
Packit ae235b
  str = g_strdup_vprintf (format, var_args);
Packit ae235b
  va_end (var_args);
Packit ae235b
Packit ae235b
  g_get_current_time (&now;;
Packit ae235b
  now_time = (time_t) now.tv_sec;
Packit ae235b
  now_tm = localtime (&now_time);
Packit ae235b
  strftime (time_buf, sizeof time_buf, "%H:%M:%S", now_tm);
Packit ae235b
Packit ae235b
  g_printerr ("%s.%06d: %s\n",
Packit ae235b
           time_buf, (gint) now.tv_usec / 1000,
Packit ae235b
           str);
Packit ae235b
  g_free (str);
Packit ae235b
}
Packit ae235b
#else
Packit ae235b
#define _log(...)
Packit ae235b
#endif
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
test_connection_quit_mainloop (gpointer user_data)
Packit ae235b
{
Packit ae235b
  volatile gboolean *quit_mainloop_fired = user_data;
Packit ae235b
  _log ("quit_mainloop_fired");
Packit ae235b
  *quit_mainloop_fired = TRUE;
Packit ae235b
  g_main_loop_quit (loop);
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
/* Connection life-cycle testing */
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
static const GDBusInterfaceInfo boo_interface_info =
Packit ae235b
{
Packit ae235b
  -1,
Packit ae235b
  "org.example.Boo",
Packit ae235b
  (GDBusMethodInfo **) NULL,
Packit ae235b
  (GDBusSignalInfo **) NULL,
Packit ae235b
  (GDBusPropertyInfo **) NULL,
Packit ae235b
  NULL,
Packit ae235b
};
Packit ae235b
Packit ae235b
static const GDBusInterfaceVTable boo_vtable =
Packit ae235b
{
Packit ae235b
  NULL, /* _method_call */
Packit ae235b
  NULL, /* _get_property */
Packit ae235b
  NULL  /* _set_property */
Packit ae235b
};
Packit ae235b
Packit ae235b
static GDBusMessage *
Packit ae235b
some_filter_func (GDBusConnection *connection,
Packit ae235b
                  GDBusMessage    *message,
Packit ae235b
                  gboolean         incoming,
Packit ae235b
                  gpointer         user_data)
Packit ae235b
{
Packit ae235b
  return message;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
on_name_owner_changed (GDBusConnection *connection,
Packit ae235b
                       const gchar     *sender_name,
Packit ae235b
                       const gchar     *object_path,
Packit ae235b
                       const gchar     *interface_name,
Packit ae235b
                       const gchar     *signal_name,
Packit ae235b
                       GVariant        *parameters,
Packit ae235b
                       gpointer         user_data)
Packit ae235b
{
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
a_gdestroynotify_that_sets_a_gboolean_to_true_and_quits_loop (gpointer user_data)
Packit ae235b
{
Packit ae235b
  volatile gboolean *val = user_data;
Packit ae235b
  *val = TRUE;
Packit ae235b
  _log ("destroynotify fired for %p", val);
Packit ae235b
  g_main_loop_quit (loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_connection_bus_failure (void)
Packit ae235b
{
Packit ae235b
  GDBusConnection *c;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * Check for correct behavior when no bus is present
Packit ae235b
   *
Packit ae235b
   */
Packit ae235b
  c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
Packit ae235b
  g_assert (error != NULL);
Packit ae235b
  g_assert (!g_dbus_error_is_remote_error (error));
Packit ae235b
  g_assert (c == NULL);
Packit ae235b
  g_error_free (error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_connection_life_cycle (void)
Packit ae235b
{
Packit ae235b
  gboolean ret;
Packit ae235b
  GDBusConnection *c;
Packit ae235b
  GDBusConnection *c2;
Packit ae235b
  GError *error;
Packit ae235b
  volatile gboolean on_signal_registration_freed_called;
Packit ae235b
  volatile gboolean on_filter_freed_called;
Packit ae235b
  volatile gboolean on_register_object_freed_called;
Packit ae235b
  volatile gboolean quit_mainloop_fired;
Packit ae235b
  guint quit_mainloop_id;
Packit ae235b
  guint registration_id;
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   *  Check for correct behavior when a bus is present
Packit ae235b
   */
Packit ae235b
  session_bus_up ();
Packit ae235b
  /* case 1 */
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
  g_assert (!g_dbus_connection_is_closed (c));
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * Check that singleton handling work
Packit ae235b
   */
Packit ae235b
  error = NULL;
Packit ae235b
  c2 = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (c2 != NULL);
Packit ae235b
  g_assert (c == c2);
Packit ae235b
  g_object_unref (c2);
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * Check that private connections work
Packit ae235b
   */
Packit ae235b
  c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (c2 != NULL);
Packit ae235b
  g_assert (c != c2);
Packit ae235b
  g_object_unref (c2);
Packit ae235b
Packit ae235b
  c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (c2 != NULL);
Packit ae235b
  g_assert (!g_dbus_connection_is_closed (c2));
Packit ae235b
  ret = g_dbus_connection_close_sync (c2, NULL, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (ret);
Packit ae235b
  _g_assert_signal_received (c2, "closed");
Packit ae235b
  g_assert (g_dbus_connection_is_closed (c2));
Packit ae235b
  ret = g_dbus_connection_close_sync (c2, NULL, &error);
Packit ae235b
  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
Packit ae235b
  g_error_free (error);
Packit ae235b
  g_assert (!ret);
Packit ae235b
  g_object_unref (c2);
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * Check that the finalization code works
Packit ae235b
   *
Packit ae235b
   * (and that the GDestroyNotify for filters and objects and signal
Packit ae235b
   * registrations are run as expected)
Packit ae235b
   */
Packit ae235b
  error = NULL;
Packit ae235b
  c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (c2 != NULL);
Packit ae235b
  /* signal registration */
Packit ae235b
  on_signal_registration_freed_called = FALSE;
Packit ae235b
  g_dbus_connection_signal_subscribe (c2,
Packit ae235b
                                      "org.freedesktop.DBus", /* bus name */
Packit ae235b
                                      "org.freedesktop.DBus", /* interface */
Packit ae235b
                                      "NameOwnerChanged",     /* member */
Packit ae235b
                                      "/org/freesktop/DBus",  /* path */
Packit ae235b
                                      NULL,                   /* arg0 */
Packit ae235b
                                      G_DBUS_SIGNAL_FLAGS_NONE,
Packit ae235b
                                      on_name_owner_changed,
Packit ae235b
                                      (gpointer) &on_signal_registration_freed_called,
Packit ae235b
                                      a_gdestroynotify_that_sets_a_gboolean_to_true_and_quits_loop);
Packit ae235b
  /* filter func */
Packit ae235b
  on_filter_freed_called = FALSE;
Packit ae235b
  g_dbus_connection_add_filter (c2,
Packit ae235b
                                some_filter_func,
Packit ae235b
                                (gpointer) &on_filter_freed_called,
Packit ae235b
                                a_gdestroynotify_that_sets_a_gboolean_to_true_and_quits_loop);
Packit ae235b
  /* object registration */
Packit ae235b
  on_register_object_freed_called = FALSE;
Packit ae235b
  error = NULL;
Packit ae235b
  registration_id = g_dbus_connection_register_object (c2,
Packit ae235b
                                                       "/foo",
Packit ae235b
                                                       (GDBusInterfaceInfo *) &boo_interface_info,
Packit ae235b
                                                       &boo_vtable,
Packit ae235b
                                                       (gpointer) &on_register_object_freed_called,
Packit ae235b
                                                       a_gdestroynotify_that_sets_a_gboolean_to_true_and_quits_loop,
Packit ae235b
                                                       &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (registration_id > 0);
Packit ae235b
  /* ok, finalize the connection and check that all the GDestroyNotify functions are invoked as expected */
Packit ae235b
  g_object_unref (c2);
Packit ae235b
  quit_mainloop_fired = FALSE;
Packit ae235b
  quit_mainloop_id = g_timeout_add (30000, test_connection_quit_mainloop, (gpointer) &quit_mainloop_fired);
Packit ae235b
  _log ("destroynotifies for\n"
Packit ae235b
        " register_object %p\n"
Packit ae235b
        " filter          %p\n"
Packit ae235b
        " signal          %p",
Packit ae235b
        &on_register_object_freed_called,
Packit ae235b
        &on_filter_freed_called,
Packit ae235b
        &on_signal_registration_freed_called);
Packit ae235b
  while (TRUE)
Packit ae235b
    {
Packit ae235b
      if (on_signal_registration_freed_called &&
Packit ae235b
          on_filter_freed_called &&
Packit ae235b
          on_register_object_freed_called)
Packit ae235b
        break;
Packit ae235b
      if (quit_mainloop_fired)
Packit ae235b
        break;
Packit ae235b
      _log ("entering loop");
Packit ae235b
      g_main_loop_run (loop);
Packit ae235b
      _log ("exiting loop");
Packit ae235b
    }
Packit ae235b
  g_source_remove (quit_mainloop_id);
Packit ae235b
  g_assert (on_signal_registration_freed_called);
Packit ae235b
  g_assert (on_filter_freed_called);
Packit ae235b
  g_assert (on_register_object_freed_called);
Packit ae235b
  g_assert (!quit_mainloop_fired);
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   *  Check for correct behavior when the bus goes away
Packit ae235b
   *
Packit ae235b
   */
Packit ae235b
  g_assert (!g_dbus_connection_is_closed (c));
Packit ae235b
  g_dbus_connection_set_exit_on_close (c, FALSE);
Packit ae235b
  session_bus_stop ();
Packit ae235b
  _g_assert_signal_received (c, "closed");
Packit ae235b
  g_assert (g_dbus_connection_is_closed (c));
Packit ae235b
  g_object_unref (c);
Packit ae235b
Packit ae235b
  session_bus_down ();
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
/* Test that sending and receiving messages work as expected */
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
static void
Packit ae235b
msg_cb_expect_error_disconnected (GDBusConnection *connection,
Packit ae235b
                                  GAsyncResult    *res,
Packit ae235b
                                  gpointer         user_data)
Packit ae235b
{
Packit ae235b
  GError *error;
Packit ae235b
  GVariant *result;
Packit ae235b
Packit ae235b
  /* Make sure gdbusconnection isn't holding @connection's lock. (#747349) */
Packit ae235b
  g_dbus_connection_get_last_serial (connection);
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  result = g_dbus_connection_call_finish (connection,
Packit ae235b
                                          res,
Packit ae235b
                                          &error);
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_error_free (error);
Packit ae235b
  g_assert (result == NULL);
Packit ae235b
Packit ae235b
  g_main_loop_quit (loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
msg_cb_expect_error_unknown_method (GDBusConnection *connection,
Packit ae235b
                                    GAsyncResult    *res,
Packit ae235b
                                    gpointer         user_data)
Packit ae235b
{
Packit ae235b
  GError *error;
Packit ae235b
  GVariant *result;
Packit ae235b
Packit ae235b
  /* Make sure gdbusconnection isn't holding @connection's lock. (#747349) */
Packit ae235b
  g_dbus_connection_get_last_serial (connection);
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  result = g_dbus_connection_call_finish (connection,
Packit ae235b
                                          res,
Packit ae235b
                                          &error);
Packit ae235b
  g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
Packit ae235b
  g_assert (g_dbus_error_is_remote_error (error));
Packit ae235b
  g_error_free (error);
Packit ae235b
  g_assert (result == NULL);
Packit ae235b
Packit ae235b
  g_main_loop_quit (loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
msg_cb_expect_success (GDBusConnection *connection,
Packit ae235b
                       GAsyncResult    *res,
Packit ae235b
                       gpointer         user_data)
Packit ae235b
{
Packit ae235b
  GError *error;
Packit ae235b
  GVariant *result;
Packit ae235b
Packit ae235b
  /* Make sure gdbusconnection isn't holding @connection's lock. (#747349) */
Packit ae235b
  g_dbus_connection_get_last_serial (connection);
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  result = g_dbus_connection_call_finish (connection,
Packit ae235b
                                          res,
Packit ae235b
                                          &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (result != NULL);
Packit ae235b
  g_variant_unref (result);
Packit ae235b
Packit ae235b
  g_main_loop_quit (loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
msg_cb_expect_error_cancelled (GDBusConnection *connection,
Packit ae235b
                               GAsyncResult    *res,
Packit ae235b
                               gpointer         user_data)
Packit ae235b
{
Packit ae235b
  GError *error;
Packit ae235b
  GVariant *result;
Packit ae235b
Packit ae235b
  /* Make sure gdbusconnection isn't holding @connection's lock. (#747349) */
Packit ae235b
  g_dbus_connection_get_last_serial (connection);
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  result = g_dbus_connection_call_finish (connection,
Packit ae235b
                                          res,
Packit ae235b
                                          &error);
Packit ae235b
  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
Packit ae235b
  g_assert (!g_dbus_error_is_remote_error (error));
Packit ae235b
  g_error_free (error);
Packit ae235b
  g_assert (result == NULL);
Packit ae235b
Packit ae235b
  g_main_loop_quit (loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
msg_cb_expect_error_cancelled_2 (GDBusConnection *connection,
Packit ae235b
                                 GAsyncResult    *res,
Packit ae235b
                                 gpointer         user_data)
Packit ae235b
{
Packit ae235b
  GError *error;
Packit ae235b
  GVariant *result;
Packit ae235b
Packit ae235b
  /* Make sure gdbusconnection isn't holding @connection's lock. (#747349) */
Packit ae235b
  g_dbus_connection_get_last_serial (connection);
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  result = g_dbus_connection_call_finish (connection,
Packit ae235b
                                          res,
Packit ae235b
                                          &error);
Packit ae235b
  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
Packit ae235b
  g_assert (!g_dbus_error_is_remote_error (error));
Packit ae235b
  g_error_free (error);
Packit ae235b
  g_assert (result == NULL);
Packit ae235b
Packit ae235b
  g_main_loop_quit (loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_connection_send (void)
Packit ae235b
{
Packit ae235b
  GDBusConnection *c;
Packit ae235b
  GCancellable *ca;
Packit ae235b
Packit ae235b
  session_bus_up ();
Packit ae235b
Packit ae235b
  /* First, get an unopened connection */
Packit ae235b
  c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
Packit ae235b
  g_assert (c != NULL);
Packit ae235b
  g_assert (!g_dbus_connection_is_closed (c));
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * Check that we never actually send a message if the GCancellable
Packit ae235b
   * is already cancelled - i.e.  we should get #G_IO_ERROR_CANCELLED
Packit ae235b
   * when the actual connection is not up.
Packit ae235b
   */
Packit ae235b
  ca = g_cancellable_new ();
Packit ae235b
  g_cancellable_cancel (ca);
Packit ae235b
  g_dbus_connection_call (c,
Packit ae235b
                          "org.freedesktop.DBus",  /* bus_name */
Packit ae235b
                          "/org/freedesktop/DBus", /* object path */
Packit ae235b
                          "org.freedesktop.DBus",  /* interface name */
Packit ae235b
                          "GetId",                 /* method name */
Packit ae235b
                          NULL, NULL,
Packit ae235b
                          G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                          -1,
Packit ae235b
                          ca,
Packit ae235b
                          (GAsyncReadyCallback) msg_cb_expect_error_cancelled,
Packit ae235b
                          NULL);
Packit ae235b
  g_main_loop_run (loop);
Packit ae235b
  g_object_unref (ca);
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * Check that we get a reply to the GetId() method call.
Packit ae235b
   */
Packit ae235b
  g_dbus_connection_call (c,
Packit ae235b
                          "org.freedesktop.DBus",  /* bus_name */
Packit ae235b
                          "/org/freedesktop/DBus", /* object path */
Packit ae235b
                          "org.freedesktop.DBus",  /* interface name */
Packit ae235b
                          "GetId",                 /* method name */
Packit ae235b
                          NULL, NULL,
Packit ae235b
                          G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                          -1,
Packit ae235b
                          NULL,
Packit ae235b
                          (GAsyncReadyCallback) msg_cb_expect_success,
Packit ae235b
                          NULL);
Packit ae235b
  g_main_loop_run (loop);
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * Check that we get an error reply to the NonExistantMethod() method call.
Packit ae235b
   */
Packit ae235b
  g_dbus_connection_call (c,
Packit ae235b
                          "org.freedesktop.DBus",  /* bus_name */
Packit ae235b
                          "/org/freedesktop/DBus", /* object path */
Packit ae235b
                          "org.freedesktop.DBus",  /* interface name */
Packit ae235b
                          "NonExistantMethod",     /* method name */
Packit ae235b
                          NULL, NULL,
Packit ae235b
                          G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                          -1,
Packit ae235b
                          NULL,
Packit ae235b
                          (GAsyncReadyCallback) msg_cb_expect_error_unknown_method,
Packit ae235b
                          NULL);
Packit ae235b
  g_main_loop_run (loop);
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * Check that cancellation works when the message is already in flight.
Packit ae235b
   */
Packit ae235b
  ca = g_cancellable_new ();
Packit ae235b
  g_dbus_connection_call (c,
Packit ae235b
                          "org.freedesktop.DBus",  /* bus_name */
Packit ae235b
                          "/org/freedesktop/DBus", /* object path */
Packit ae235b
                          "org.freedesktop.DBus",  /* interface name */
Packit ae235b
                          "GetId",                 /* method name */
Packit ae235b
                          NULL, NULL,
Packit ae235b
                          G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                          -1,
Packit ae235b
                          ca,
Packit ae235b
                          (GAsyncReadyCallback) msg_cb_expect_error_cancelled_2,
Packit ae235b
                          NULL);
Packit ae235b
  g_cancellable_cancel (ca);
Packit ae235b
  g_main_loop_run (loop);
Packit ae235b
  g_object_unref (ca);
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * Check that we get an error when sending to a connection that is disconnected.
Packit ae235b
   */
Packit ae235b
  g_dbus_connection_set_exit_on_close (c, FALSE);
Packit ae235b
  session_bus_stop ();
Packit ae235b
  _g_assert_signal_received (c, "closed");
Packit ae235b
  g_assert (g_dbus_connection_is_closed (c));
Packit ae235b
Packit ae235b
  g_dbus_connection_call (c,
Packit ae235b
                          "org.freedesktop.DBus",  /* bus_name */
Packit ae235b
                          "/org/freedesktop/DBus", /* object path */
Packit ae235b
                          "org.freedesktop.DBus",  /* interface name */
Packit ae235b
                          "GetId",                 /* method name */
Packit ae235b
                          NULL, NULL,
Packit ae235b
                          G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                          -1,
Packit ae235b
                          NULL,
Packit ae235b
                          (GAsyncReadyCallback) msg_cb_expect_error_disconnected,
Packit ae235b
                          NULL);
Packit ae235b
  g_main_loop_run (loop);
Packit ae235b
Packit ae235b
  g_object_unref (c);
Packit ae235b
Packit ae235b
  session_bus_down ();
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
/* Connection signal tests */
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_connection_signal_handler (GDBusConnection  *connection,
Packit ae235b
                                const gchar      *sender_name,
Packit ae235b
                                const gchar      *object_path,
Packit ae235b
                                const gchar      *interface_name,
Packit ae235b
                                const gchar      *signal_name,
Packit ae235b
                                GVariant         *parameters,
Packit ae235b
                                gpointer         user_data)
Packit ae235b
{
Packit ae235b
  gint *counter = user_data;
Packit ae235b
  *counter += 1;
Packit ae235b
Packit ae235b
  /*g_debug ("in test_connection_signal_handler (sender=%s path=%s interface=%s member=%s)",
Packit ae235b
           sender_name,
Packit ae235b
           object_path,
Packit ae235b
           interface_name,
Packit ae235b
           signal_name);*/
Packit ae235b
Packit ae235b
  g_main_loop_quit (loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_connection_signals (void)
Packit ae235b
{
Packit ae235b
  GDBusConnection *c1;
Packit ae235b
  GDBusConnection *c2;
Packit ae235b
  GDBusConnection *c3;
Packit ae235b
  guint s1;
Packit ae235b
  guint s1b;
Packit ae235b
  guint s2;
Packit ae235b
  guint s3;
Packit ae235b
  gint count_s1;
Packit ae235b
  gint count_s1b;
Packit ae235b
  gint count_s2;
Packit ae235b
  gint count_name_owner_changed;
Packit ae235b
  GError *error;
Packit ae235b
  gboolean ret;
Packit ae235b
  GVariant *result;
Packit ae235b
  gboolean quit_mainloop_fired;
Packit ae235b
  guint quit_mainloop_id;
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * Bring up first separate connections
Packit ae235b
   */
Packit ae235b
  session_bus_up ();
Packit ae235b
  /* if running with dbus-monitor, it claims the name :1.0 - so if we don't run with the monitor
Packit ae235b
   * emulate this
Packit ae235b
   */
Packit ae235b
  if (g_getenv ("G_DBUS_MONITOR") == NULL)
Packit ae235b
    {
Packit ae235b
      c1 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, NULL);
Packit ae235b
      g_assert (c1 != NULL);
Packit ae235b
      g_assert (!g_dbus_connection_is_closed (c1));
Packit ae235b
      g_object_unref (c1);
Packit ae235b
    }
Packit ae235b
  c1 = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
Packit ae235b
  g_assert (c1 != NULL);
Packit ae235b
  g_assert (!g_dbus_connection_is_closed (c1));
Packit ae235b
  g_assert_cmpstr (g_dbus_connection_get_unique_name (c1), ==, ":1.1");
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * Install two signal handlers for the first connection
Packit ae235b
   *
Packit ae235b
   *  - Listen to the signal "Foo" from :1.2 (e.g. c2)
Packit ae235b
   *  - Listen to the signal "Foo" from anyone (e.g. both c2 and c3)
Packit ae235b
   *
Packit ae235b
   * and then count how many times this signal handler was invoked.
Packit ae235b
   */
Packit ae235b
  s1 = g_dbus_connection_signal_subscribe (c1,
Packit ae235b
                                           ":1.2",
Packit ae235b
                                           "org.gtk.GDBus.ExampleInterface",
Packit ae235b
                                           "Foo",
Packit ae235b
                                           "/org/gtk/GDBus/ExampleInterface",
Packit ae235b
                                           NULL,
Packit ae235b
                                           G_DBUS_SIGNAL_FLAGS_NONE,
Packit ae235b
                                           test_connection_signal_handler,
Packit ae235b
                                           &count_s1,
Packit ae235b
                                           NULL);
Packit ae235b
  s2 = g_dbus_connection_signal_subscribe (c1,
Packit ae235b
                                           NULL, /* match any sender */
Packit ae235b
                                           "org.gtk.GDBus.ExampleInterface",
Packit ae235b
                                           "Foo",
Packit ae235b
                                           "/org/gtk/GDBus/ExampleInterface",
Packit ae235b
                                           NULL,
Packit ae235b
                                           G_DBUS_SIGNAL_FLAGS_NONE,
Packit ae235b
                                           test_connection_signal_handler,
Packit ae235b
                                           &count_s2,
Packit ae235b
                                           NULL);
Packit ae235b
  s3 = g_dbus_connection_signal_subscribe (c1,
Packit ae235b
                                           "org.freedesktop.DBus",  /* sender */
Packit ae235b
                                           "org.freedesktop.DBus",  /* interface */
Packit ae235b
                                           "NameOwnerChanged",      /* member */
Packit ae235b
                                           "/org/freedesktop/DBus", /* path */
Packit ae235b
                                           NULL,
Packit ae235b
                                           G_DBUS_SIGNAL_FLAGS_NONE,
Packit ae235b
                                           test_connection_signal_handler,
Packit ae235b
                                           &count_name_owner_changed,
Packit ae235b
                                           NULL);
Packit ae235b
  /* Note that s1b is *just like* s1 - this is to catch a bug where N
Packit ae235b
   * subscriptions of the same rule causes N calls to each of the N
Packit ae235b
   * subscriptions instead of just 1 call to each of the N subscriptions.
Packit ae235b
   */
Packit ae235b
  s1b = g_dbus_connection_signal_subscribe (c1,
Packit ae235b
                                            ":1.2",
Packit ae235b
                                            "org.gtk.GDBus.ExampleInterface",
Packit ae235b
                                            "Foo",
Packit ae235b
                                            "/org/gtk/GDBus/ExampleInterface",
Packit ae235b
                                            NULL,
Packit ae235b
                                            G_DBUS_SIGNAL_FLAGS_NONE,
Packit ae235b
                                            test_connection_signal_handler,
Packit ae235b
                                            &count_s1b,
Packit ae235b
                                            NULL);
Packit ae235b
  g_assert (s1 != 0);
Packit ae235b
  g_assert (s1b != 0);
Packit ae235b
  g_assert (s2 != 0);
Packit ae235b
  g_assert (s3 != 0);
Packit ae235b
Packit ae235b
  count_s1 = 0;
Packit ae235b
  count_s1b = 0;
Packit ae235b
  count_s2 = 0;
Packit ae235b
  count_name_owner_changed = 0;
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * Make c2 emit "Foo" - we should catch it twice
Packit ae235b
   *
Packit ae235b
   * Note that there is no way to be sure that the signal subscriptions
Packit ae235b
   * on c1 are effective yet - for all we know, the AddMatch() messages
Packit ae235b
   * could sit waiting in a buffer somewhere between this process and
Packit ae235b
   * the message bus. And emitting signals on c2 (a completely other
Packit ae235b
   * socket!) will not necessarily change this.
Packit ae235b
   *
Packit ae235b
   * To ensure this is not the case, do a synchronous call on c1.
Packit ae235b
   */
Packit ae235b
  result = g_dbus_connection_call_sync (c1,
Packit ae235b
                                        "org.freedesktop.DBus",  /* bus name */
Packit ae235b
                                        "/org/freedesktop/DBus", /* object path */
Packit ae235b
                                        "org.freedesktop.DBus",  /* interface name */
Packit ae235b
                                        "GetId",                 /* method name */
Packit ae235b
                                        NULL,                    /* parameters */
Packit ae235b
                                        NULL,                    /* return type */
Packit ae235b
                                        G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                                        -1,
Packit ae235b
                                        NULL,
Packit ae235b
                                        &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (result != NULL);
Packit ae235b
  g_variant_unref (result);
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * Bring up two other connections
Packit ae235b
   */
Packit ae235b
  c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, NULL);
Packit ae235b
  g_assert (c2 != NULL);
Packit ae235b
  g_assert (!g_dbus_connection_is_closed (c2));
Packit ae235b
  g_assert_cmpstr (g_dbus_connection_get_unique_name (c2), ==, ":1.2");
Packit ae235b
  c3 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, NULL);
Packit ae235b
  g_assert (c3 != NULL);
Packit ae235b
  g_assert (!g_dbus_connection_is_closed (c3));
Packit ae235b
  g_assert_cmpstr (g_dbus_connection_get_unique_name (c3), ==, ":1.3");
Packit ae235b
Packit ae235b
  /* now, emit the signal on c2 */
Packit ae235b
  ret = g_dbus_connection_emit_signal (c2,
Packit ae235b
                                       NULL, /* destination bus name */
Packit ae235b
                                       "/org/gtk/GDBus/ExampleInterface",
Packit ae235b
                                       "org.gtk.GDBus.ExampleInterface",
Packit ae235b
                                       "Foo",
Packit ae235b
                                       NULL,
Packit ae235b
                                       &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (ret);
Packit ae235b
  while (!(count_s1 >= 1 && count_s2 >= 1))
Packit ae235b
    g_main_loop_run (loop);
Packit ae235b
  g_assert_cmpint (count_s1, ==, 1);
Packit ae235b
  g_assert_cmpint (count_s2, ==, 1);
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * Make c3 emit "Foo" - we should catch it only once
Packit ae235b
   */
Packit ae235b
  ret = g_dbus_connection_emit_signal (c3,
Packit ae235b
                                       NULL, /* destination bus name */
Packit ae235b
                                       "/org/gtk/GDBus/ExampleInterface",
Packit ae235b
                                       "org.gtk.GDBus.ExampleInterface",
Packit ae235b
                                       "Foo",
Packit ae235b
                                       NULL,
Packit ae235b
                                       &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (ret);
Packit ae235b
  while (!(count_s1 == 1 && count_s2 == 2))
Packit ae235b
    g_main_loop_run (loop);
Packit ae235b
  g_assert_cmpint (count_s1, ==, 1);
Packit ae235b
  g_assert_cmpint (count_s2, ==, 2);
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * Also to check the total amount of NameOwnerChanged signals - use a 5 second ceiling
Packit ae235b
   * to avoid spinning forever
Packit ae235b
   */
Packit ae235b
  quit_mainloop_fired = FALSE;
Packit ae235b
  quit_mainloop_id = g_timeout_add (30000, test_connection_quit_mainloop, &quit_mainloop_fired);
Packit ae235b
  while (count_name_owner_changed < 2 && !quit_mainloop_fired)
Packit ae235b
    g_main_loop_run (loop);
Packit ae235b
  g_source_remove (quit_mainloop_id);
Packit ae235b
  g_assert_cmpint (count_s1, ==, 1);
Packit ae235b
  g_assert_cmpint (count_s2, ==, 2);
Packit ae235b
  g_assert_cmpint (count_name_owner_changed, ==, 2);
Packit ae235b
Packit ae235b
  g_dbus_connection_signal_unsubscribe (c1, s1);
Packit ae235b
  g_dbus_connection_signal_unsubscribe (c1, s2);
Packit ae235b
  g_dbus_connection_signal_unsubscribe (c1, s3);
Packit ae235b
  g_dbus_connection_signal_unsubscribe (c1, s1b);
Packit ae235b
Packit ae235b
  g_object_unref (c1);
Packit ae235b
  g_object_unref (c2);
Packit ae235b
  g_object_unref (c3);
Packit ae235b
Packit ae235b
  session_bus_down ();
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_match_rule (GDBusConnection  *connection,
Packit ae235b
                 GDBusSignalFlags  flags,
Packit ae235b
                 gchar            *arg0_rule,
Packit ae235b
                 gchar            *arg0,
Packit ae235b
                 gboolean          should_match)
Packit ae235b
{
Packit ae235b
  guint subscription_ids[2];
Packit ae235b
  gint emissions = 0;
Packit ae235b
  gint matches = 0;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  subscription_ids[0] = g_dbus_connection_signal_subscribe (connection,
Packit ae235b
                                                            NULL, "org.gtk.ExampleInterface", "Foo", "/",
Packit ae235b
                                                            NULL,
Packit ae235b
                                                            G_DBUS_SIGNAL_FLAGS_NONE,
Packit ae235b
                                                            test_connection_signal_handler,
Packit ae235b
                                                            &emissions, NULL);
Packit ae235b
  subscription_ids[1] = g_dbus_connection_signal_subscribe (connection,
Packit ae235b
                                                            NULL, "org.gtk.ExampleInterface", "Foo", "/",
Packit ae235b
                                                            arg0_rule,
Packit ae235b
                                                            flags,
Packit ae235b
                                                            test_connection_signal_handler,
Packit ae235b
                                                            &matches, NULL);
Packit ae235b
  g_assert_cmpint (subscription_ids[0], !=, 0);
Packit ae235b
  g_assert_cmpint (subscription_ids[1], !=, 0);
Packit ae235b
Packit ae235b
  g_dbus_connection_emit_signal (connection,
Packit ae235b
                                 NULL, "/", "org.gtk.ExampleInterface",
Packit ae235b
                                 "Foo", g_variant_new ("(s)", arg0),
Packit ae235b
                                 &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  /* synchronously ping a non-existent method to make sure the signals are dispatched */
Packit ae235b
  g_dbus_connection_call_sync (connection, "org.gtk.ExampleInterface", "/", "org.gtk.ExampleInterface",
Packit ae235b
                               "Bar", g_variant_new ("()"), G_VARIANT_TYPE_UNIT, G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                               -1, NULL, NULL);
Packit ae235b
Packit ae235b
  while (g_main_context_iteration (NULL, FALSE))
Packit ae235b
    ;
Packit ae235b
Packit ae235b
  g_assert_cmpint (emissions, ==, 1);
Packit ae235b
  g_assert_cmpint (matches, ==, should_match ? 1 : 0);
Packit ae235b
Packit ae235b
  g_dbus_connection_signal_unsubscribe (connection, subscription_ids[0]);
Packit ae235b
  g_dbus_connection_signal_unsubscribe (connection, subscription_ids[1]);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_connection_signal_match_rules (void)
Packit ae235b
{
Packit ae235b
  GDBusConnection *con;
Packit ae235b
Packit ae235b
  session_bus_up ();
Packit ae235b
  con = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
Packit ae235b
Packit ae235b
  test_match_rule (con, G_DBUS_SIGNAL_FLAGS_NONE, "foo", "foo", TRUE);
Packit ae235b
  test_match_rule (con, G_DBUS_SIGNAL_FLAGS_NONE, "foo", "bar", FALSE);
Packit ae235b
Packit ae235b
  test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE, "org.gtk", "", FALSE);
Packit ae235b
  test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE, "org.gtk", "org", FALSE);
Packit ae235b
  test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE, "org.gtk", "org.gtk", TRUE);
Packit ae235b
  test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE, "org.gtk", "org.gtk.Example", TRUE);
Packit ae235b
  test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE, "org.gtk", "org.gtk+", FALSE);
Packit ae235b
Packit ae235b
  test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/", "/", TRUE);
Packit ae235b
  test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/", "", FALSE);
Packit ae235b
  test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/org/gtk/Example", "/org/gtk/Example", TRUE);
Packit ae235b
  test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/org/gtk/", "/org/gtk/Example", TRUE);
Packit ae235b
  test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/org/gtk/Example", "/org/gtk/", TRUE);
Packit ae235b
  test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/org/gtk/Example", "/org/gtk", FALSE);
Packit ae235b
  test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/org/gtk+", "/org/gtk", FALSE);
Packit ae235b
Packit ae235b
  g_object_unref (con);
Packit ae235b
  session_bus_down ();
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  guint num_handled;
Packit ae235b
  guint num_outgoing;
Packit ae235b
  guint32 serial;
Packit ae235b
} FilterData;
Packit ae235b
Packit ae235b
static GDBusMessage *
Packit ae235b
filter_func (GDBusConnection *connection,
Packit ae235b
             GDBusMessage    *message,
Packit ae235b
             gboolean         incoming,
Packit ae235b
             gpointer         user_data)
Packit ae235b
{
Packit ae235b
  FilterData *data = user_data;
Packit ae235b
  guint32 reply_serial;
Packit ae235b
Packit ae235b
  if (incoming)
Packit ae235b
    {
Packit ae235b
      reply_serial = g_dbus_message_get_reply_serial (message);
Packit ae235b
      if (reply_serial == data->serial)
Packit ae235b
        data->num_handled += 1;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      data->num_outgoing += 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return message;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  gboolean alter_incoming;
Packit ae235b
  gboolean alter_outgoing;
Packit ae235b
} FilterEffects;
Packit ae235b
Packit ae235b
static GDBusMessage *
Packit ae235b
other_filter_func (GDBusConnection *connection,
Packit ae235b
                   GDBusMessage    *message,
Packit ae235b
                   gboolean         incoming,
Packit ae235b
                   gpointer         user_data)
Packit ae235b
{
Packit ae235b
  FilterEffects *effects = user_data;
Packit ae235b
  GDBusMessage *ret;
Packit ae235b
  gboolean alter;
Packit ae235b
Packit ae235b
  if (incoming)
Packit ae235b
    alter = effects->alter_incoming;
Packit ae235b
  else
Packit ae235b
    alter = effects->alter_outgoing;
Packit ae235b
Packit ae235b
  if (alter)
Packit ae235b
    {
Packit ae235b
      GDBusMessage *copy;
Packit ae235b
      GVariant *body;
Packit ae235b
      gchar *s;
Packit ae235b
      gchar *s2;
Packit ae235b
Packit ae235b
      copy = g_dbus_message_copy (message, NULL);
Packit ae235b
      g_object_unref (message);
Packit ae235b
Packit ae235b
      body = g_dbus_message_get_body (copy);
Packit ae235b
      g_variant_get (body, "(s)", &s);
Packit ae235b
      s2 = g_strdup_printf ("MOD: %s", s);
Packit ae235b
      g_dbus_message_set_body (copy, g_variant_new ("(s)", s2));
Packit ae235b
      g_free (s2);
Packit ae235b
      g_free (s);
Packit ae235b
Packit ae235b
      ret = copy;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      ret = message;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_connection_filter_name_owner_changed_signal_handler (GDBusConnection  *connection,
Packit ae235b
                                                          const gchar      *sender_name,
Packit ae235b
                                                          const gchar      *object_path,
Packit ae235b
                                                          const gchar      *interface_name,
Packit ae235b
                                                          const gchar      *signal_name,
Packit ae235b
                                                          GVariant         *parameters,
Packit ae235b
                                                          gpointer         user_data)
Packit ae235b
{
Packit ae235b
  const gchar *name;
Packit ae235b
  const gchar *old_owner;
Packit ae235b
  const gchar *new_owner;
Packit ae235b
Packit ae235b
  g_variant_get (parameters,
Packit ae235b
                 "(&s&s&s)",
Packit ae235b
                 &name,
Packit ae235b
                 &old_owner,
Packit ae235b
                 &new_owner);
Packit ae235b
Packit ae235b
  if (g_strcmp0 (name, "com.example.TestService") == 0 && strlen (new_owner) > 0)
Packit ae235b
    {
Packit ae235b
      g_main_loop_quit (loop);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
test_connection_filter_on_timeout (gpointer user_data)
Packit ae235b
{
Packit ae235b
  g_printerr ("Timeout waiting 30 sec on service\n");
Packit ae235b
  g_assert_not_reached ();
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_connection_filter (void)
Packit ae235b
{
Packit ae235b
  GDBusConnection *c;
Packit ae235b
  FilterData data;
Packit ae235b
  GDBusMessage *m;
Packit ae235b
  GDBusMessage *m2;
Packit ae235b
  GDBusMessage *r;
Packit ae235b
  GError *error;
Packit ae235b
  guint filter_id;
Packit ae235b
  guint timeout_mainloop_id;
Packit ae235b
  guint signal_handler_id;
Packit ae235b
  FilterEffects effects;
Packit ae235b
  GVariant *result;
Packit ae235b
  const gchar *s;
Packit ae235b
Packit ae235b
  memset (&data, '\0', sizeof (FilterData));
Packit ae235b
Packit ae235b
  session_bus_up ();
Packit ae235b
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
  filter_id = g_dbus_connection_add_filter (c,
Packit ae235b
                                            filter_func,
Packit ae235b
                                            &data,
Packit ae235b
                                            NULL);
Packit ae235b
Packit ae235b
  m = g_dbus_message_new_method_call ("org.freedesktop.DBus", /* name */
Packit ae235b
                                      "/org/freedesktop/DBus", /* path */
Packit ae235b
                                      "org.freedesktop.DBus", /* interface */
Packit ae235b
                                      "GetNameOwner");
Packit ae235b
  g_dbus_message_set_body (m, g_variant_new ("(s)", "org.freedesktop.DBus"));
Packit ae235b
  error = NULL;
Packit ae235b
  g_dbus_connection_send_message (c, m, G_DBUS_SEND_MESSAGE_FLAGS_NONE, &data.serial, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  while (data.num_handled == 0)
Packit ae235b
    g_thread_yield ();
Packit ae235b
Packit ae235b
  m2 = g_dbus_message_copy (m, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_dbus_connection_send_message (c, m2, G_DBUS_SEND_MESSAGE_FLAGS_NONE, &data.serial, &error);
Packit ae235b
  g_object_unref (m2);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  while (data.num_handled == 1)
Packit ae235b
    g_thread_yield ();
Packit ae235b
Packit ae235b
  m2 = g_dbus_message_copy (m, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_dbus_message_set_serial (m2, data.serial);
Packit ae235b
  /* lock the message to test PRESERVE_SERIAL flag. */
Packit ae235b
  g_dbus_message_lock (m2);
Packit ae235b
  g_dbus_connection_send_message (c, m2, G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL, &data.serial, &error);
Packit ae235b
  g_object_unref (m2);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  while (data.num_handled == 2)
Packit ae235b
    g_thread_yield ();
Packit ae235b
Packit ae235b
  m2 = g_dbus_message_copy (m, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  r = g_dbus_connection_send_message_with_reply_sync (c,
Packit ae235b
                                                      m2,
Packit ae235b
                                                      G_DBUS_SEND_MESSAGE_FLAGS_NONE,
Packit ae235b
                                                      -1,
Packit ae235b
                                                      &data.serial,
Packit ae235b
                                                      NULL, /* GCancellable */
Packit ae235b
                                                      &error);
Packit ae235b
  g_object_unref (m2);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (r != NULL);
Packit ae235b
  g_object_unref (r);
Packit ae235b
  g_assert_cmpint (data.num_handled, ==, 4);
Packit ae235b
Packit ae235b
  g_dbus_connection_remove_filter (c, filter_id);
Packit ae235b
Packit ae235b
  m2 = g_dbus_message_copy (m, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  r = g_dbus_connection_send_message_with_reply_sync (c,
Packit ae235b
                                                      m2,
Packit ae235b
                                                      G_DBUS_SEND_MESSAGE_FLAGS_NONE,
Packit ae235b
                                                      -1,
Packit ae235b
                                                      &data.serial,
Packit ae235b
                                                      NULL, /* GCancellable */
Packit ae235b
                                                      &error);
Packit ae235b
  g_object_unref (m2);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (r != NULL);
Packit ae235b
  g_object_unref (r);
Packit ae235b
  g_assert_cmpint (data.num_handled, ==, 4);
Packit ae235b
  g_assert_cmpint (data.num_outgoing, ==, 4);
Packit ae235b
Packit ae235b
  /* wait for service to be available */
Packit ae235b
  signal_handler_id = g_dbus_connection_signal_subscribe (c,
Packit ae235b
                                                          "org.freedesktop.DBus", /* sender */
Packit ae235b
                                                          "org.freedesktop.DBus",
Packit ae235b
                                                          "NameOwnerChanged",
Packit ae235b
                                                          "/org/freedesktop/DBus",
Packit ae235b
                                                          NULL, /* arg0 */
Packit ae235b
                                                          G_DBUS_SIGNAL_FLAGS_NONE,
Packit ae235b
                                                          test_connection_filter_name_owner_changed_signal_handler,
Packit ae235b
                                                          NULL,
Packit ae235b
                                                          NULL);
Packit ae235b
  g_assert_cmpint (signal_handler_id, !=, 0);
Packit ae235b
Packit ae235b
  /* this is safe; testserver will exit once the bus goes away */
Packit ae235b
  g_assert (g_spawn_command_line_async (g_test_get_filename (G_TEST_BUILT, "gdbus-testserver", NULL), NULL));
Packit ae235b
Packit ae235b
  timeout_mainloop_id = g_timeout_add (30000, test_connection_filter_on_timeout, NULL);
Packit ae235b
  g_main_loop_run (loop);
Packit ae235b
  g_source_remove (timeout_mainloop_id);
Packit ae235b
  g_dbus_connection_signal_unsubscribe (c, signal_handler_id);
Packit ae235b
Packit ae235b
  /* now test some combinations... */
Packit ae235b
  filter_id = g_dbus_connection_add_filter (c,
Packit ae235b
                                            other_filter_func,
Packit ae235b
                                            &effects,
Packit ae235b
                                            NULL);
Packit ae235b
  /* -- */
Packit ae235b
  effects.alter_incoming = FALSE;
Packit ae235b
  effects.alter_outgoing = FALSE;
Packit ae235b
  error = NULL;
Packit ae235b
  result = g_dbus_connection_call_sync (c,
Packit ae235b
                                        "com.example.TestService",      /* bus name */
Packit ae235b
                                        "/com/example/TestObject",      /* object path */
Packit ae235b
                                        "com.example.Frob",             /* interface name */
Packit ae235b
                                        "HelloWorld",                   /* method name */
Packit ae235b
                                        g_variant_new ("(s)", "Cat"),   /* parameters */
Packit ae235b
                                        G_VARIANT_TYPE ("(s)"),         /* return type */
Packit ae235b
                                        G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                                        -1,
Packit ae235b
                                        NULL,
Packit ae235b
                                        &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_variant_get (result, "(&s)", &s);
Packit ae235b
  g_assert_cmpstr (s, ==, "You greeted me with 'Cat'. Thanks!");
Packit ae235b
  g_variant_unref (result);
Packit ae235b
  /* -- */
Packit ae235b
  effects.alter_incoming = TRUE;
Packit ae235b
  effects.alter_outgoing = TRUE;
Packit ae235b
  error = NULL;
Packit ae235b
  result = g_dbus_connection_call_sync (c,
Packit ae235b
                                        "com.example.TestService",      /* bus name */
Packit ae235b
                                        "/com/example/TestObject",      /* object path */
Packit ae235b
                                        "com.example.Frob",             /* interface name */
Packit ae235b
                                        "HelloWorld",                   /* method name */
Packit ae235b
                                        g_variant_new ("(s)", "Cat"),   /* parameters */
Packit ae235b
                                        G_VARIANT_TYPE ("(s)"),         /* return type */
Packit ae235b
                                        G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                                        -1,
Packit ae235b
                                        NULL,
Packit ae235b
                                        &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_variant_get (result, "(&s)", &s);
Packit ae235b
  g_assert_cmpstr (s, ==, "MOD: You greeted me with 'MOD: Cat'. Thanks!");
Packit ae235b
  g_variant_unref (result);
Packit ae235b
Packit ae235b
Packit ae235b
  g_dbus_connection_remove_filter (c, filter_id);
Packit ae235b
Packit ae235b
  g_object_unref (c);
Packit ae235b
  g_object_unref (m);
Packit ae235b
Packit ae235b
  session_bus_down ();
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
#define NUM_THREADS 50
Packit ae235b
Packit ae235b
static void
Packit ae235b
send_bogus_message (GDBusConnection *c, guint32 *out_serial)
Packit ae235b
{
Packit ae235b
  GDBusMessage *m;
Packit ae235b
  GError *error;
Packit ae235b
Packit ae235b
  m = g_dbus_message_new_method_call ("org.freedesktop.DBus", /* name */
Packit ae235b
                                      "/org/freedesktop/DBus", /* path */
Packit ae235b
                                      "org.freedesktop.DBus", /* interface */
Packit ae235b
                                      "GetNameOwner");
Packit ae235b
  g_dbus_message_set_body (m, g_variant_new ("(s)", "org.freedesktop.DBus"));
Packit ae235b
  error = NULL;
Packit ae235b
  g_dbus_connection_send_message (c, m, G_DBUS_SEND_MESSAGE_FLAGS_NONE, out_serial, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_object_unref (m);
Packit ae235b
}
Packit ae235b
Packit ae235b
#define SLEEP_USEC (100 * 1000)
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
serials_thread_func (GDBusConnection *c)
Packit ae235b
{
Packit ae235b
  guint32 message_serial;
Packit ae235b
  guint i;
Packit ae235b
Packit ae235b
  /* No calls on this thread yet */
Packit ae235b
  g_assert_cmpint (g_dbus_connection_get_last_serial(c), ==, 0);
Packit ae235b
Packit ae235b
  /* Send a bogus message and store its serial */
Packit ae235b
  message_serial = 0;
Packit ae235b
  send_bogus_message (c, &message_serial);
Packit ae235b
Packit ae235b
  /* Give it some time to actually send the message out. 10 seconds
Packit ae235b
   * should be plenty, even on slow machines. */
Packit ae235b
  for (i = 0; i < 10 * G_USEC_PER_SEC / SLEEP_USEC; i++)
Packit ae235b
    {
Packit ae235b
      if (g_dbus_connection_get_last_serial(c) != 0)
Packit ae235b
        break;
Packit ae235b
Packit ae235b
      g_usleep (SLEEP_USEC);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_assert_cmpint (g_dbus_connection_get_last_serial(c), !=, 0);
Packit ae235b
  g_assert_cmpint (g_dbus_connection_get_last_serial(c), ==, message_serial);
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_connection_serials (void)
Packit ae235b
{
Packit ae235b
  GDBusConnection *c;
Packit ae235b
  GError *error;
Packit ae235b
  GThread *pool[NUM_THREADS];
Packit ae235b
  int i;
Packit ae235b
Packit ae235b
  session_bus_up ();
Packit ae235b
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
  /* Status after initialization */
Packit ae235b
  g_assert_cmpint (g_dbus_connection_get_last_serial (c), ==, 1);
Packit ae235b
Packit ae235b
  /* Send a bogus message */
Packit ae235b
  send_bogus_message (c, NULL);
Packit ae235b
  g_assert_cmpint (g_dbus_connection_get_last_serial (c), ==, 2);
Packit ae235b
Packit ae235b
  /* Start the threads */
Packit ae235b
  for (i = 0; i < NUM_THREADS; i++)
Packit ae235b
    pool[i] = g_thread_new (NULL, (GThreadFunc) serials_thread_func, c);
Packit ae235b
Packit ae235b
  /* Wait until threads are finished */
Packit ae235b
  for (i = 0; i < NUM_THREADS; i++)
Packit ae235b
      g_thread_join (pool[i]);
Packit ae235b
Packit ae235b
  /* No calls in between on this thread, should be the last value */
Packit ae235b
  g_assert_cmpint (g_dbus_connection_get_last_serial (c), ==, 2);
Packit ae235b
Packit ae235b
  send_bogus_message (c, NULL);
Packit ae235b
Packit ae235b
  /* All above calls + calls in threads */
Packit ae235b
  g_assert_cmpint (g_dbus_connection_get_last_serial (c), ==, 3 + NUM_THREADS);
Packit ae235b
Packit ae235b
  g_object_unref (c);
Packit ae235b
Packit ae235b
  session_bus_down ();
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_connection_basic (void)
Packit ae235b
{
Packit ae235b
  GDBusConnection *connection;
Packit ae235b
  GError *error;
Packit ae235b
  GDBusCapabilityFlags flags;
Packit ae235b
  gchar *guid;
Packit ae235b
  gchar *name;
Packit ae235b
  gboolean closed;
Packit ae235b
  gboolean exit_on_close;
Packit ae235b
  GIOStream *stream;
Packit ae235b
  GCredentials *credentials;
Packit ae235b
Packit ae235b
  session_bus_up ();
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (connection != NULL);
Packit ae235b
Packit ae235b
  flags = g_dbus_connection_get_capabilities (connection);
Packit ae235b
  g_assert (flags == G_DBUS_CAPABILITY_FLAGS_NONE ||
Packit ae235b
            flags == G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
Packit ae235b
Packit ae235b
  credentials = g_dbus_connection_get_peer_credentials (connection);
Packit ae235b
  g_assert (credentials == NULL);
Packit ae235b
Packit ae235b
  g_object_get (connection,
Packit ae235b
                "stream", &stream,
Packit ae235b
                "guid", &guid,
Packit ae235b
                "unique-name", &name,
Packit ae235b
                "closed", &closed,
Packit ae235b
                "exit-on-close", &exit_on_close,
Packit ae235b
                "capabilities", &flags,
Packit ae235b
                NULL);
Packit ae235b
Packit ae235b
  g_assert (G_IS_IO_STREAM (stream));
Packit ae235b
  g_assert (g_dbus_is_guid (guid));
Packit ae235b
  g_assert (g_dbus_is_unique_name (name));
Packit ae235b
  g_assert (!closed);
Packit ae235b
  g_assert (exit_on_close);
Packit ae235b
  g_assert (flags == G_DBUS_CAPABILITY_FLAGS_NONE ||
Packit ae235b
            flags == G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
Packit ae235b
  g_object_unref (stream);
Packit ae235b
  g_free (name);
Packit ae235b
  g_free (guid);
Packit ae235b
Packit ae235b
  g_object_unref (connection);
Packit ae235b
Packit ae235b
  session_bus_down ();
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
int
Packit ae235b
main (int   argc,
Packit ae235b
      char *argv[])
Packit ae235b
{
Packit ae235b
  int ret;
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
  g_test_dbus_unset ();
Packit ae235b
Packit ae235b
  /* gdbus cleanup is pretty racy due to worker threads, so always do this test first */
Packit ae235b
  g_test_add_func ("/gdbus/connection/bus-failure", test_connection_bus_failure);
Packit ae235b
Packit ae235b
  g_test_add_func ("/gdbus/connection/basic", test_connection_basic);
Packit ae235b
  g_test_add_func ("/gdbus/connection/life-cycle", test_connection_life_cycle);
Packit ae235b
  g_test_add_func ("/gdbus/connection/send", test_connection_send);
Packit ae235b
  g_test_add_func ("/gdbus/connection/signals", test_connection_signals);
Packit ae235b
  g_test_add_func ("/gdbus/connection/signal-match-rules", test_connection_signal_match_rules);
Packit ae235b
  g_test_add_func ("/gdbus/connection/filter", test_connection_filter);
Packit ae235b
  g_test_add_func ("/gdbus/connection/serials", test_connection_serials);
Packit ae235b
  ret = g_test_run();
Packit ae235b
Packit ae235b
  g_main_loop_unref (loop);
Packit ae235b
  return ret;
Packit ae235b
}