Blame gio/tests/gdbus-export.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 shared mainloop */
Packit ae235b
static GMainLoop *loop = NULL;
Packit ae235b
Packit ae235b
static GDBusConnection *c = NULL;
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
/* Test that we can export objects, the hierarchy is correct and the right handlers are invoked */
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
static const GDBusArgInfo foo_method1_in_args =
Packit ae235b
{
Packit ae235b
  -1,
Packit ae235b
  "an_input_string",
Packit ae235b
  "s",
Packit ae235b
  NULL
Packit ae235b
};
Packit ae235b
static const GDBusArgInfo * const foo_method1_in_arg_pointers[] = {&foo_method1_in_args, NULL};
Packit ae235b
Packit ae235b
static const GDBusArgInfo foo_method1_out_args =
Packit ae235b
{
Packit ae235b
  -1,
Packit ae235b
  "an_output_string",
Packit ae235b
  "s",
Packit ae235b
  NULL
Packit ae235b
};
Packit ae235b
static const GDBusArgInfo * const foo_method1_out_arg_pointers[] = {&foo_method1_out_args, NULL};
Packit ae235b
Packit ae235b
static const GDBusMethodInfo foo_method_info_method1 =
Packit ae235b
{
Packit ae235b
  -1,
Packit ae235b
  "Method1",
Packit ae235b
  (GDBusArgInfo **) &foo_method1_in_arg_pointers,
Packit ae235b
  (GDBusArgInfo **) &foo_method1_out_arg_pointers,
Packit ae235b
  NULL
Packit ae235b
};
Packit ae235b
static const GDBusMethodInfo foo_method_info_method2 =
Packit ae235b
{
Packit ae235b
  -1,
Packit ae235b
  "Method2",
Packit ae235b
  NULL,
Packit ae235b
  NULL,
Packit ae235b
  NULL
Packit ae235b
};
Packit ae235b
static const GDBusMethodInfo * const foo_method_info_pointers[] = {&foo_method_info_method1, &foo_method_info_method2, NULL};
Packit ae235b
Packit ae235b
static const GDBusSignalInfo foo_signal_info =
Packit ae235b
{
Packit ae235b
  -1,
Packit ae235b
  "SignalAlpha",
Packit ae235b
  NULL,
Packit ae235b
  NULL
Packit ae235b
};
Packit ae235b
static const GDBusSignalInfo * const foo_signal_info_pointers[] = {&foo_signal_info, NULL};
Packit ae235b
Packit ae235b
static const GDBusPropertyInfo foo_property_info[] =
Packit ae235b
{
Packit ae235b
  {
Packit ae235b
    -1,
Packit ae235b
    "PropertyUno",
Packit ae235b
    "s",
Packit ae235b
    G_DBUS_PROPERTY_INFO_FLAGS_READABLE | G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE,
Packit ae235b
    NULL
Packit ae235b
  },
Packit ae235b
  {
Packit ae235b
    -1,
Packit ae235b
    "NotWritable",
Packit ae235b
    "s",
Packit ae235b
    G_DBUS_PROPERTY_INFO_FLAGS_READABLE,
Packit ae235b
    NULL
Packit ae235b
  },
Packit ae235b
  {
Packit ae235b
    -1,
Packit ae235b
    "NotReadable",
Packit ae235b
    "s",
Packit ae235b
    G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE,
Packit ae235b
    NULL
Packit ae235b
  }
Packit ae235b
};
Packit ae235b
static const GDBusPropertyInfo * const foo_property_info_pointers[] =
Packit ae235b
{
Packit ae235b
  &foo_property_info[0],
Packit ae235b
  &foo_property_info[1],
Packit ae235b
  &foo_property_info[2],
Packit ae235b
  NULL
Packit ae235b
};
Packit ae235b
Packit ae235b
static const GDBusInterfaceInfo foo_interface_info =
Packit ae235b
{
Packit ae235b
  -1,
Packit ae235b
  "org.example.Foo",
Packit ae235b
  (GDBusMethodInfo **) &foo_method_info_pointers,
Packit ae235b
  (GDBusSignalInfo **) &foo_signal_info_pointers,
Packit ae235b
  (GDBusPropertyInfo **)&foo_property_info_pointers,
Packit ae235b
  NULL,
Packit ae235b
};
Packit ae235b
Packit ae235b
/* Foo2 is just Foo without the properties */
Packit ae235b
static const GDBusInterfaceInfo foo2_interface_info =
Packit ae235b
{
Packit ae235b
  -1,
Packit ae235b
  "org.example.Foo2",
Packit ae235b
  (GDBusMethodInfo **) &foo_method_info_pointers,
Packit ae235b
  (GDBusSignalInfo **) &foo_signal_info_pointers,
Packit ae235b
};
Packit ae235b
Packit ae235b
static void
Packit ae235b
foo_method_call (GDBusConnection       *connection,
Packit ae235b
                 const gchar           *sender,
Packit ae235b
                 const gchar           *object_path,
Packit ae235b
                 const gchar           *interface_name,
Packit ae235b
                 const gchar           *method_name,
Packit ae235b
                 GVariant              *parameters,
Packit ae235b
                 GDBusMethodInvocation *invocation,
Packit ae235b
                 gpointer               user_data)
Packit ae235b
{
Packit ae235b
  if (g_strcmp0 (method_name, "Method1") == 0)
Packit ae235b
    {
Packit ae235b
      const gchar *input;
Packit ae235b
      gchar *output;
Packit ae235b
      g_variant_get (parameters, "(&s)", &input);
Packit ae235b
      output = g_strdup_printf ("You passed the string '%s'. Jolly good!", input);
Packit ae235b
      g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", output));
Packit ae235b
      g_free (output);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_dbus_method_invocation_return_dbus_error (invocation,
Packit ae235b
                                                  "org.example.SomeError",
Packit ae235b
                                                  "How do you like them apples, buddy!");
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static GVariant *
Packit ae235b
foo_get_property (GDBusConnection       *connection,
Packit ae235b
                  const gchar           *sender,
Packit ae235b
                  const gchar           *object_path,
Packit ae235b
                  const gchar           *interface_name,
Packit ae235b
                  const gchar           *property_name,
Packit ae235b
                  GError               **error,
Packit ae235b
                  gpointer               user_data)
Packit ae235b
{
Packit ae235b
  GVariant *ret;
Packit ae235b
  gchar *s;
Packit ae235b
  s = g_strdup_printf ("Property '%s' Is What It Is!", property_name);
Packit ae235b
  ret = g_variant_new_string (s);
Packit ae235b
  g_free (s);
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
foo_set_property (GDBusConnection       *connection,
Packit ae235b
                  const gchar           *sender,
Packit ae235b
                  const gchar           *object_path,
Packit ae235b
                  const gchar           *interface_name,
Packit ae235b
                  const gchar           *property_name,
Packit ae235b
                  GVariant              *value,
Packit ae235b
                  GError               **error,
Packit ae235b
                  gpointer               user_data)
Packit ae235b
{
Packit ae235b
  gchar *s;
Packit ae235b
  s = g_variant_print (value, TRUE);
Packit ae235b
  g_set_error (error,
Packit ae235b
               G_DBUS_ERROR,
Packit ae235b
               G_DBUS_ERROR_SPAWN_FILE_INVALID,
Packit ae235b
               "Returning some error instead of writing the value '%s' to the property '%s'",
Packit ae235b
               property_name, s);
Packit ae235b
  g_free (s);
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static const GDBusInterfaceVTable foo_vtable =
Packit ae235b
{
Packit ae235b
  foo_method_call,
Packit ae235b
  foo_get_property,
Packit ae235b
  foo_set_property
Packit ae235b
};
Packit ae235b
Packit ae235b
/* -------------------- */
Packit ae235b
Packit ae235b
static const GDBusMethodInfo bar_method_info[] =
Packit ae235b
{
Packit ae235b
  {
Packit ae235b
    -1,
Packit ae235b
    "MethodA",
Packit ae235b
    NULL,
Packit ae235b
    NULL,
Packit ae235b
    NULL
Packit ae235b
  },
Packit ae235b
  {
Packit ae235b
    -1,
Packit ae235b
    "MethodB",
Packit ae235b
    NULL,
Packit ae235b
    NULL,
Packit ae235b
    NULL
Packit ae235b
  }
Packit ae235b
};
Packit ae235b
static const GDBusMethodInfo * const bar_method_info_pointers[] = {&bar_method_info[0], &bar_method_info[1], NULL};
Packit ae235b
Packit ae235b
static const GDBusSignalInfo bar_signal_info[] =
Packit ae235b
{
Packit ae235b
  {
Packit ae235b
    -1,
Packit ae235b
    "SignalMars",
Packit ae235b
    NULL,
Packit ae235b
    NULL
Packit ae235b
  }
Packit ae235b
};
Packit ae235b
static const GDBusSignalInfo * const bar_signal_info_pointers[] = {&bar_signal_info[0], NULL};
Packit ae235b
Packit ae235b
static const GDBusPropertyInfo bar_property_info[] =
Packit ae235b
{
Packit ae235b
  {
Packit ae235b
    -1,
Packit ae235b
    "PropertyDuo",
Packit ae235b
    "s",
Packit ae235b
    G_DBUS_PROPERTY_INFO_FLAGS_READABLE,
Packit ae235b
    NULL
Packit ae235b
  }
Packit ae235b
};
Packit ae235b
static const GDBusPropertyInfo * const bar_property_info_pointers[] = {&bar_property_info[0], NULL};
Packit ae235b
Packit ae235b
static const GDBusInterfaceInfo bar_interface_info =
Packit ae235b
{
Packit ae235b
  -1,
Packit ae235b
  "org.example.Bar",
Packit ae235b
  (GDBusMethodInfo **) bar_method_info_pointers,
Packit ae235b
  (GDBusSignalInfo **) bar_signal_info_pointers,
Packit ae235b
  (GDBusPropertyInfo **) bar_property_info_pointers,
Packit ae235b
  NULL,
Packit ae235b
};
Packit ae235b
Packit ae235b
/* -------------------- */
Packit ae235b
Packit ae235b
static const GDBusMethodInfo dyna_method_info[] =
Packit ae235b
{
Packit ae235b
  {
Packit ae235b
    -1,
Packit ae235b
    "DynaCyber",
Packit ae235b
    NULL,
Packit ae235b
    NULL,
Packit ae235b
    NULL
Packit ae235b
  }
Packit ae235b
};
Packit ae235b
static const GDBusMethodInfo * const dyna_method_info_pointers[] = {&dyna_method_info[0], NULL};
Packit ae235b
Packit ae235b
static const GDBusInterfaceInfo dyna_interface_info =
Packit ae235b
{
Packit ae235b
  -1,
Packit ae235b
  "org.example.Dyna",
Packit ae235b
  (GDBusMethodInfo **) dyna_method_info_pointers,
Packit ae235b
  NULL, /* no signals */
Packit ae235b
  NULL, /* no properties */
Packit ae235b
  NULL,
Packit ae235b
};
Packit ae235b
Packit ae235b
static void
Packit ae235b
dyna_cyber (GDBusConnection *connection,
Packit ae235b
            const gchar *sender,
Packit ae235b
            const gchar *object_path,
Packit ae235b
            const gchar *interface_name,
Packit ae235b
            const gchar *method_name,
Packit ae235b
            GVariant *parameters,
Packit ae235b
            GDBusMethodInvocation *invocation,
Packit ae235b
            gpointer user_data)
Packit ae235b
{
Packit ae235b
  GPtrArray *data = user_data;
Packit ae235b
  gchar *node_name;
Packit ae235b
  guint n;
Packit ae235b
Packit ae235b
  node_name = strrchr (object_path, '/') + 1;
Packit ae235b
Packit ae235b
  /* Add new node if it is not already known */
Packit ae235b
  for (n = 0; n < data->len ; n++)
Packit ae235b
    {
Packit ae235b
      if (g_strcmp0 (g_ptr_array_index (data, n), node_name) == 0)
Packit ae235b
        goto out;
Packit ae235b
    }
Packit ae235b
  g_ptr_array_add (data, g_strdup(node_name));
Packit ae235b
Packit ae235b
  out:
Packit ae235b
    g_dbus_method_invocation_return_value (invocation, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
static const GDBusInterfaceVTable dyna_interface_vtable =
Packit ae235b
{
Packit ae235b
  dyna_cyber,
Packit ae235b
  NULL,
Packit ae235b
  NULL
Packit ae235b
};
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
static void
Packit ae235b
introspect_callback (GDBusProxy   *proxy,
Packit ae235b
                     GAsyncResult *res,
Packit ae235b
                     gpointer      user_data)
Packit ae235b
{
Packit ae235b
  gchar **xml_data = user_data;
Packit ae235b
  GVariant *result;
Packit ae235b
  GError *error;
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  result = g_dbus_proxy_call_finish (proxy,
Packit ae235b
                                              res,
Packit ae235b
                                              &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (result != NULL);
Packit ae235b
  g_variant_get (result, "(s)", xml_data);
Packit ae235b
  g_variant_unref (result);
Packit ae235b
Packit ae235b
  g_main_loop_quit (loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gchar **
Packit ae235b
get_nodes_at (GDBusConnection  *c,
Packit ae235b
              const gchar      *object_path)
Packit ae235b
{
Packit ae235b
  GError *error;
Packit ae235b
  GDBusProxy *proxy;
Packit ae235b
  gchar *xml_data;
Packit ae235b
  GPtrArray *p;
Packit ae235b
  GDBusNodeInfo *node_info;
Packit ae235b
  guint n;
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  proxy = g_dbus_proxy_new_sync (c,
Packit ae235b
                                 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
Packit ae235b
                                 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
Packit ae235b
                                 NULL,
Packit ae235b
                                 g_dbus_connection_get_unique_name (c),
Packit ae235b
                                 object_path,
Packit ae235b
                                 "org.freedesktop.DBus.Introspectable",
Packit ae235b
                                 NULL,
Packit ae235b
                                 &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (proxy != NULL);
Packit ae235b
Packit ae235b
  /* do this async to avoid libdbus-1 deadlocks */
Packit ae235b
  xml_data = NULL;
Packit ae235b
  g_dbus_proxy_call (proxy,
Packit ae235b
                     "Introspect",
Packit ae235b
                     NULL,
Packit ae235b
                     G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                     -1,
Packit ae235b
                     NULL,
Packit ae235b
                     (GAsyncReadyCallback) introspect_callback,
Packit ae235b
                     &xml_data);
Packit ae235b
  g_main_loop_run (loop);
Packit ae235b
  g_assert (xml_data != NULL);
Packit ae235b
Packit ae235b
  node_info = g_dbus_node_info_new_for_xml (xml_data, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (node_info != NULL);
Packit ae235b
Packit ae235b
  p = g_ptr_array_new ();
Packit ae235b
  for (n = 0; node_info->nodes != NULL && node_info->nodes[n] != NULL; n++)
Packit ae235b
    {
Packit ae235b
      const GDBusNodeInfo *sub_node_info = node_info->nodes[n];
Packit ae235b
      g_ptr_array_add (p, g_strdup (sub_node_info->path));
Packit ae235b
    }
Packit ae235b
  g_ptr_array_add (p, NULL);
Packit ae235b
Packit ae235b
  g_object_unref (proxy);
Packit ae235b
  g_free (xml_data);
Packit ae235b
  g_dbus_node_info_unref (node_info);
Packit ae235b
Packit ae235b
  return (gchar **) g_ptr_array_free (p, FALSE);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
has_interface (GDBusConnection *c,
Packit ae235b
               const gchar     *object_path,
Packit ae235b
               const gchar     *interface_name)
Packit ae235b
{
Packit ae235b
  GError *error;
Packit ae235b
  GDBusProxy *proxy;
Packit ae235b
  gchar *xml_data;
Packit ae235b
  GDBusNodeInfo *node_info;
Packit ae235b
  gboolean ret;
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  proxy = g_dbus_proxy_new_sync (c,
Packit ae235b
                                 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
Packit ae235b
                                 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
Packit ae235b
                                 NULL,
Packit ae235b
                                 g_dbus_connection_get_unique_name (c),
Packit ae235b
                                 object_path,
Packit ae235b
                                 "org.freedesktop.DBus.Introspectable",
Packit ae235b
                                 NULL,
Packit ae235b
                                 &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (proxy != NULL);
Packit ae235b
Packit ae235b
  /* do this async to avoid libdbus-1 deadlocks */
Packit ae235b
  xml_data = NULL;
Packit ae235b
  g_dbus_proxy_call (proxy,
Packit ae235b
                     "Introspect",
Packit ae235b
                     NULL,
Packit ae235b
                     G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                     -1,
Packit ae235b
                     NULL,
Packit ae235b
                     (GAsyncReadyCallback) introspect_callback,
Packit ae235b
                     &xml_data);
Packit ae235b
  g_main_loop_run (loop);
Packit ae235b
  g_assert (xml_data != NULL);
Packit ae235b
Packit ae235b
  node_info = g_dbus_node_info_new_for_xml (xml_data, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (node_info != NULL);
Packit ae235b
Packit ae235b
  ret = (g_dbus_node_info_lookup_interface (node_info, interface_name) != NULL);
Packit ae235b
Packit ae235b
  g_object_unref (proxy);
Packit ae235b
  g_free (xml_data);
Packit ae235b
  g_dbus_node_info_unref (node_info);
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
static guint
Packit ae235b
count_interfaces (GDBusConnection *c,
Packit ae235b
                  const gchar     *object_path)
Packit ae235b
{
Packit ae235b
  GError *error;
Packit ae235b
  GDBusProxy *proxy;
Packit ae235b
  gchar *xml_data;
Packit ae235b
  GDBusNodeInfo *node_info;
Packit ae235b
  guint ret;
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  proxy = g_dbus_proxy_new_sync (c,
Packit ae235b
                                 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
Packit ae235b
                                 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
Packit ae235b
                                 NULL,
Packit ae235b
                                 g_dbus_connection_get_unique_name (c),
Packit ae235b
                                 object_path,
Packit ae235b
                                 "org.freedesktop.DBus.Introspectable",
Packit ae235b
                                 NULL,
Packit ae235b
                                 &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (proxy != NULL);
Packit ae235b
Packit ae235b
  /* do this async to avoid libdbus-1 deadlocks */
Packit ae235b
  xml_data = NULL;
Packit ae235b
  g_dbus_proxy_call (proxy,
Packit ae235b
                     "Introspect",
Packit ae235b
                     NULL,
Packit ae235b
                     G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                     -1,
Packit ae235b
                     NULL,
Packit ae235b
                     (GAsyncReadyCallback) introspect_callback,
Packit ae235b
                     &xml_data);
Packit ae235b
  g_main_loop_run (loop);
Packit ae235b
  g_assert (xml_data != NULL);
Packit ae235b
Packit ae235b
  node_info = g_dbus_node_info_new_for_xml (xml_data, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (node_info != NULL);
Packit ae235b
Packit ae235b
  ret = 0;
Packit ae235b
  while (node_info->interfaces != NULL && node_info->interfaces[ret] != NULL)
Packit ae235b
    ret++;
Packit ae235b
Packit ae235b
  g_object_unref (proxy);
Packit ae235b
  g_free (xml_data);
Packit ae235b
  g_dbus_node_info_unref (node_info);
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
dyna_create_callback (GDBusProxy   *proxy,
Packit ae235b
                      GAsyncResult  *res,
Packit ae235b
                      gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GVariant *result;
Packit ae235b
  GError *error;
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  result = g_dbus_proxy_call_finish (proxy,
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
/* Dynamically create @object_name under /foo/dyna */
Packit ae235b
static void
Packit ae235b
dyna_create (GDBusConnection *c,
Packit ae235b
             const gchar     *object_name)
Packit ae235b
{
Packit ae235b
  GError *error;
Packit ae235b
  GDBusProxy *proxy;
Packit ae235b
  gchar *object_path;
Packit ae235b
Packit ae235b
  object_path = g_strconcat ("/foo/dyna/", object_name, NULL);
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  proxy = g_dbus_proxy_new_sync (c,
Packit ae235b
                                 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
Packit ae235b
                                 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
Packit ae235b
                                 NULL,
Packit ae235b
                                 g_dbus_connection_get_unique_name (c),
Packit ae235b
                                 object_path,
Packit ae235b
                                 "org.example.Dyna",
Packit ae235b
                                 NULL,
Packit ae235b
                                 &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (proxy != NULL);
Packit ae235b
Packit ae235b
  /* do this async to avoid libdbus-1 deadlocks */
Packit ae235b
  g_dbus_proxy_call (proxy,
Packit ae235b
                     "DynaCyber",
Packit ae235b
                     g_variant_new ("()"),
Packit ae235b
                     G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                     -1,
Packit ae235b
                     NULL,
Packit ae235b
                     (GAsyncReadyCallback) dyna_create_callback,
Packit ae235b
                     NULL);
Packit ae235b
  g_main_loop_run (loop);
Packit ae235b
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  g_object_unref (proxy);
Packit ae235b
  g_free (object_path);
Packit ae235b
Packit ae235b
  return;
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  guint num_unregistered_calls;
Packit ae235b
  guint num_unregistered_subtree_calls;
Packit ae235b
  guint num_subtree_nodes;
Packit ae235b
} ObjectRegistrationData;
Packit ae235b
Packit ae235b
static void
Packit ae235b
on_object_unregistered (gpointer user_data)
Packit ae235b
{
Packit ae235b
  ObjectRegistrationData *data = user_data;
Packit ae235b
Packit ae235b
  data->num_unregistered_calls++;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
on_subtree_unregistered (gpointer user_data)
Packit ae235b
{
Packit ae235b
  ObjectRegistrationData *data = user_data;
Packit ae235b
Packit ae235b
  data->num_unregistered_subtree_calls++;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
_g_strv_has_string (const gchar* const * haystack,
Packit ae235b
                    const gchar *needle)
Packit ae235b
{
Packit ae235b
  guint n;
Packit ae235b
Packit ae235b
  for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
Packit ae235b
    {
Packit ae235b
      if (g_strcmp0 (haystack[n], needle) == 0)
Packit ae235b
        return TRUE;
Packit ae235b
    }
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* -------------------- */
Packit ae235b
Packit ae235b
static gchar **
Packit ae235b
subtree_enumerate (GDBusConnection       *connection,
Packit ae235b
                   const gchar           *sender,
Packit ae235b
                   const gchar           *object_path,
Packit ae235b
                   gpointer               user_data)
Packit ae235b
{
Packit ae235b
  ObjectRegistrationData *data = user_data;
Packit ae235b
  GPtrArray *p;
Packit ae235b
  gchar **nodes;
Packit ae235b
  guint n;
Packit ae235b
Packit ae235b
  p = g_ptr_array_new ();
Packit ae235b
Packit ae235b
  for (n = 0; n < data->num_subtree_nodes; n++)
Packit ae235b
    {
Packit ae235b
      g_ptr_array_add (p, g_strdup_printf ("vp%d", n));
Packit ae235b
      g_ptr_array_add (p, g_strdup_printf ("evp%d", n));
Packit ae235b
    }
Packit ae235b
  g_ptr_array_add (p, NULL);
Packit ae235b
Packit ae235b
  nodes = (gchar **) g_ptr_array_free (p, FALSE);
Packit ae235b
Packit ae235b
  return nodes;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Only allows certain objects, and aborts on unknowns */
Packit ae235b
static GDBusInterfaceInfo **
Packit ae235b
subtree_introspect (GDBusConnection       *connection,
Packit ae235b
                    const gchar           *sender,
Packit ae235b
                    const gchar           *object_path,
Packit ae235b
                    const gchar           *node,
Packit ae235b
                    gpointer               user_data)
Packit ae235b
{
Packit ae235b
  const GDBusInterfaceInfo *interfaces[2] = {
Packit ae235b
     NULL /* filled in below */, NULL
Packit ae235b
  };
Packit ae235b
Packit ae235b
  /* VPs implement the Foo interface, EVPs implement the Bar interface. The root
Packit ae235b
   * does not implement any interfaces
Packit ae235b
   */
Packit ae235b
  if (node == NULL)
Packit ae235b
    {
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
  else if (g_str_has_prefix (node, "vp"))
Packit ae235b
    {
Packit ae235b
      interfaces[0] = &foo_interface_info;
Packit ae235b
    }
Packit ae235b
  else if (g_str_has_prefix (node, "evp"))
Packit ae235b
    {
Packit ae235b
      interfaces[0] = &bar_interface_info;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_assert_not_reached ();
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return g_memdup (interfaces, 2 * sizeof (void *));
Packit ae235b
}
Packit ae235b
Packit ae235b
static const GDBusInterfaceVTable *
Packit ae235b
subtree_dispatch (GDBusConnection             *connection,
Packit ae235b
                  const gchar                 *sender,
Packit ae235b
                  const gchar                 *object_path,
Packit ae235b
                  const gchar                 *interface_name,
Packit ae235b
                  const gchar                 *node,
Packit ae235b
                  gpointer                    *out_user_data,
Packit ae235b
                  gpointer                     user_data)
Packit ae235b
{
Packit ae235b
  if (g_strcmp0 (interface_name, "org.example.Foo") == 0)
Packit ae235b
    return &foo_vtable;
Packit ae235b
  else
Packit ae235b
    return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static const GDBusSubtreeVTable subtree_vtable =
Packit ae235b
{
Packit ae235b
  subtree_enumerate,
Packit ae235b
  subtree_introspect,
Packit ae235b
  subtree_dispatch
Packit ae235b
};
Packit ae235b
Packit ae235b
/* -------------------- */
Packit ae235b
Packit ae235b
static gchar **
Packit ae235b
dynamic_subtree_enumerate (GDBusConnection       *connection,
Packit ae235b
                           const gchar           *sender,
Packit ae235b
                           const gchar           *object_path,
Packit ae235b
                           gpointer               user_data)
Packit ae235b
{
Packit ae235b
  GPtrArray *data = user_data;
Packit ae235b
  gchar **nodes = g_new (gchar*, data->len + 1);
Packit ae235b
  guint n;
Packit ae235b
Packit ae235b
  for (n = 0; n < data->len; n++)
Packit ae235b
    {
Packit ae235b
      nodes[n] = g_strdup (g_ptr_array_index (data, n));
Packit ae235b
    }
Packit ae235b
  nodes[data->len] = NULL;
Packit ae235b
Packit ae235b
  return nodes;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Allow all objects to be introspected */
Packit ae235b
static GDBusInterfaceInfo **
Packit ae235b
dynamic_subtree_introspect (GDBusConnection       *connection,
Packit ae235b
                            const gchar           *sender,
Packit ae235b
                            const gchar           *object_path,
Packit ae235b
                            const gchar           *node,
Packit ae235b
                            gpointer               user_data)
Packit ae235b
{
Packit ae235b
  const GDBusInterfaceInfo *interfaces[2] = { &dyna_interface_info, NULL };
Packit ae235b
Packit ae235b
  return g_memdup (interfaces, 2 * sizeof (void *));
Packit ae235b
}
Packit ae235b
Packit ae235b
static const GDBusInterfaceVTable *
Packit ae235b
dynamic_subtree_dispatch (GDBusConnection             *connection,
Packit ae235b
                          const gchar                 *sender,
Packit ae235b
                          const gchar                 *object_path,
Packit ae235b
                          const gchar                 *interface_name,
Packit ae235b
                          const gchar                 *node,
Packit ae235b
                          gpointer                    *out_user_data,
Packit ae235b
                          gpointer                     user_data)
Packit ae235b
{
Packit ae235b
  *out_user_data = user_data;
Packit ae235b
  return &dyna_interface_vtable;
Packit ae235b
}
Packit ae235b
Packit ae235b
static const GDBusSubtreeVTable dynamic_subtree_vtable =
Packit ae235b
{
Packit ae235b
  dynamic_subtree_enumerate,
Packit ae235b
  dynamic_subtree_introspect,
Packit ae235b
  dynamic_subtree_dispatch
Packit ae235b
};
Packit ae235b
Packit ae235b
/* -------------------- */
Packit ae235b
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  const gchar *object_path;
Packit ae235b
  gboolean check_remote_errors;
Packit ae235b
} TestDispatchThreadFuncArgs;
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
test_dispatch_thread_func (gpointer user_data)
Packit ae235b
{
Packit ae235b
  TestDispatchThreadFuncArgs *args = user_data;
Packit ae235b
  const gchar *object_path = args->object_path;
Packit ae235b
  GDBusProxy *foo_proxy;
Packit ae235b
  GVariant *value;
Packit ae235b
  GVariant *inner;
Packit ae235b
  GError *error;
Packit ae235b
  gchar *s;
Packit ae235b
  const gchar *value_str;
Packit ae235b
Packit ae235b
  foo_proxy = g_dbus_proxy_new_sync (c,
Packit ae235b
                                     G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS |
Packit ae235b
                                     G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
Packit ae235b
                                     NULL,
Packit ae235b
                                     g_dbus_connection_get_unique_name (c),
Packit ae235b
                                     object_path,
Packit ae235b
                                     "org.example.Foo",
Packit ae235b
                                     NULL,
Packit ae235b
                                     &error);
Packit ae235b
  g_assert (foo_proxy != NULL);
Packit ae235b
Packit ae235b
  /* generic interfaces */
Packit ae235b
  error = NULL;
Packit ae235b
  value = g_dbus_proxy_call_sync (foo_proxy,
Packit ae235b
                                  "org.freedesktop.DBus.Peer.Ping",
Packit ae235b
                                  NULL,
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 (value != NULL);
Packit ae235b
  g_variant_unref (value);
Packit ae235b
Packit ae235b
  /* user methods */
Packit ae235b
  error = NULL;
Packit ae235b
  value = g_dbus_proxy_call_sync (foo_proxy,
Packit ae235b
                                  "Method1",
Packit ae235b
                                  g_variant_new ("(s)", "winwinwin"),
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 (value != NULL);
Packit ae235b
  g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE ("(s)")));
Packit ae235b
  g_variant_get (value, "(&s)", &value_str);
Packit ae235b
  g_assert_cmpstr (value_str, ==, "You passed the string 'winwinwin'. Jolly good!");
Packit ae235b
  g_variant_unref (value);
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  value = g_dbus_proxy_call_sync (foo_proxy,
Packit ae235b
                                  "Method2",
Packit ae235b
                                  NULL,
Packit ae235b
                                  G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                                  -1,
Packit ae235b
                                  NULL,
Packit ae235b
                                  &error);
Packit ae235b
  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
Packit ae235b
  g_assert_cmpstr (error->message, ==, "GDBus.Error:org.example.SomeError: How do you like them apples, buddy!");
Packit ae235b
  g_error_free (error);
Packit ae235b
  g_assert (value == NULL);
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  value = g_dbus_proxy_call_sync (foo_proxy,
Packit ae235b
                                  "Method2",
Packit ae235b
                                  g_variant_new ("(s)", "failfailfail"),
Packit ae235b
                                  G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                                  -1,
Packit ae235b
                                  NULL,
Packit ae235b
                                  &error);
Packit ae235b
  g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
Packit ae235b
  g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: Type of message, '(s)', does not match expected type '()'");
Packit ae235b
  g_error_free (error);
Packit ae235b
  g_assert (value == NULL);
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  value = g_dbus_proxy_call_sync (foo_proxy,
Packit ae235b
                                  "NonExistantMethod",
Packit ae235b
                                  NULL,
Packit ae235b
                                  G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                                  -1,
Packit ae235b
                                  NULL,
Packit ae235b
                                  &error);
Packit ae235b
  g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
Packit ae235b
  g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.UnknownMethod: No such method 'NonExistantMethod'");
Packit ae235b
  g_error_free (error);
Packit ae235b
  g_assert (value == NULL);
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  value = g_dbus_proxy_call_sync (foo_proxy,
Packit ae235b
                                  "org.example.FooXYZ.NonExistant",
Packit ae235b
                                  NULL,
Packit ae235b
                                  G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                                  -1,
Packit ae235b
                                  NULL,
Packit ae235b
                                  &error);
Packit ae235b
  g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
Packit ae235b
  g_error_free (error);
Packit ae235b
  g_assert (value == NULL);
Packit ae235b
Packit ae235b
  /* user properties */
Packit ae235b
  error = NULL;
Packit ae235b
  value = g_dbus_proxy_call_sync (foo_proxy,
Packit ae235b
                                  "org.freedesktop.DBus.Properties.Get",
Packit ae235b
                                  g_variant_new ("(ss)",
Packit ae235b
                                                 "org.example.Foo",
Packit ae235b
                                                 "PropertyUno"),
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 (value != NULL);
Packit ae235b
  g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE ("(v)")));
Packit ae235b
  g_variant_get (value, "(v)", &inner);
Packit ae235b
  g_assert (g_variant_is_of_type (inner, G_VARIANT_TYPE_STRING));
Packit ae235b
  g_assert_cmpstr (g_variant_get_string (inner, NULL), ==, "Property 'PropertyUno' Is What It Is!");
Packit ae235b
  g_variant_unref (value);
Packit ae235b
  g_variant_unref (inner);
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  value = g_dbus_proxy_call_sync (foo_proxy,
Packit ae235b
                                  "org.freedesktop.DBus.Properties.Get",
Packit ae235b
                                  g_variant_new ("(ss)",
Packit ae235b
                                                 "org.example.Foo",
Packit ae235b
                                                 "ThisDoesntExist"),
Packit ae235b
                                  G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                                  -1,
Packit ae235b
                                  NULL,
Packit ae235b
                                  &error);
Packit ae235b
  g_assert (value == NULL);
Packit ae235b
  g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
Packit ae235b
  g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: No such property 'ThisDoesntExist'");
Packit ae235b
  g_error_free (error);
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  value = g_dbus_proxy_call_sync (foo_proxy,
Packit ae235b
                                  "org.freedesktop.DBus.Properties.Get",
Packit ae235b
                                  g_variant_new ("(ss)",
Packit ae235b
                                                 "org.example.Foo",
Packit ae235b
                                                 "NotReadable"),
Packit ae235b
                                  G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                                  -1,
Packit ae235b
                                  NULL,
Packit ae235b
                                  &error);
Packit ae235b
  g_assert (value == NULL);
Packit ae235b
  g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
Packit ae235b
  g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: Property 'NotReadable' is not readable");
Packit ae235b
  g_error_free (error);
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  value = g_dbus_proxy_call_sync (foo_proxy,
Packit ae235b
                                  "org.freedesktop.DBus.Properties.Set",
Packit ae235b
                                  g_variant_new ("(ssv)",
Packit ae235b
                                                 "org.example.Foo",
Packit ae235b
                                                 "NotReadable",
Packit ae235b
                                                 g_variant_new_string ("But Writable you are!")),
Packit ae235b
                                  G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                                  -1,
Packit ae235b
                                  NULL,
Packit ae235b
                                  &error);
Packit ae235b
  g_assert (value == NULL);
Packit ae235b
  if (args->check_remote_errors)
Packit ae235b
    {
Packit ae235b
      /* _with_closures variant doesn't support customizing error data. */
Packit ae235b
      g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_SPAWN_FILE_INVALID);
Packit ae235b
      g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.Spawn.FileInvalid: Returning some error instead of writing the value 'NotReadable' to the property ''But Writable you are!''");
Packit ae235b
    }
Packit ae235b
  g_assert (error != NULL && error->domain == G_DBUS_ERROR);
Packit ae235b
  g_error_free (error);
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  value = g_dbus_proxy_call_sync (foo_proxy,
Packit ae235b
                                  "org.freedesktop.DBus.Properties.Set",
Packit ae235b
                                  g_variant_new ("(ssv)",
Packit ae235b
                                                 "org.example.Foo",
Packit ae235b
                                                 "NotWritable",
Packit ae235b
                                                 g_variant_new_uint32 (42)),
Packit ae235b
                                  G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                                  -1,
Packit ae235b
                                  NULL,
Packit ae235b
                                  &error);
Packit ae235b
  g_assert (value == NULL);
Packit ae235b
  g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
Packit ae235b
  g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: Property 'NotWritable' is not writable");
Packit ae235b
  g_error_free (error);
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  value = g_dbus_proxy_call_sync (foo_proxy,
Packit ae235b
                                  "org.freedesktop.DBus.Properties.GetAll",
Packit ae235b
                                  g_variant_new ("(s)",
Packit ae235b
                                                 "org.example.Foo"),
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 (value != NULL);
Packit ae235b
  g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE ("(a{sv})")));
Packit ae235b
  s = g_variant_print (value, TRUE);
Packit ae235b
  g_assert_cmpstr (s, ==, "({'PropertyUno': <\"Property 'PropertyUno' Is What It Is!\">, 'NotWritable': <\"Property 'NotWritable' Is What It Is!\">},)");
Packit ae235b
  g_free (s);
Packit ae235b
  g_variant_unref (value);
Packit ae235b
Packit ae235b
  g_object_unref (foo_proxy);
Packit ae235b
Packit ae235b
  g_main_loop_quit (loop);
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_dispatch (const gchar *object_path,
Packit ae235b
               gboolean     check_remote_errors)
Packit ae235b
{
Packit ae235b
  GThread *thread;
Packit ae235b
  
Packit ae235b
  TestDispatchThreadFuncArgs args = {object_path, check_remote_errors};
Packit ae235b
Packit ae235b
  /* run this in a thread to avoid deadlocks */
Packit ae235b
  thread = g_thread_new ("test_dispatch",
Packit ae235b
                         test_dispatch_thread_func,
Packit ae235b
                         (gpointer) &args);
Packit ae235b
  g_main_loop_run (loop);
Packit ae235b
  g_thread_join (thread);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_object_registration (void)
Packit ae235b
{
Packit ae235b
  GError *error;
Packit ae235b
  ObjectRegistrationData data;
Packit ae235b
  GPtrArray *dyna_data;
Packit ae235b
  gchar **nodes;
Packit ae235b
  guint boss_foo_reg_id;
Packit ae235b
  guint boss_bar_reg_id;
Packit ae235b
  guint worker1_foo_reg_id;
Packit ae235b
  guint worker1p1_foo_reg_id;
Packit ae235b
  guint worker2_bar_reg_id;
Packit ae235b
  guint intern1_foo_reg_id;
Packit ae235b
  guint intern2_bar_reg_id;
Packit ae235b
  guint intern2_foo_reg_id;
Packit ae235b
  guint intern3_bar_reg_id;
Packit ae235b
  guint registration_id;
Packit ae235b
  guint subtree_registration_id;
Packit ae235b
  guint non_subtree_object_path_foo_reg_id;
Packit ae235b
  guint non_subtree_object_path_bar_reg_id;
Packit ae235b
  guint dyna_subtree_registration_id;
Packit ae235b
  guint num_successful_registrations;
Packit ae235b
Packit ae235b
  data.num_unregistered_calls = 0;
Packit ae235b
  data.num_unregistered_subtree_calls = 0;
Packit ae235b
  data.num_subtree_nodes = 0;
Packit ae235b
Packit ae235b
  num_successful_registrations = 0;
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
  registration_id = g_dbus_connection_register_object (c,
Packit ae235b
                                                       "/foo/boss",
Packit ae235b
                                                       (GDBusInterfaceInfo *) &foo_interface_info,
Packit ae235b
                                                       &foo_vtable,
Packit ae235b
                                                       &data,
Packit ae235b
                                                       on_object_unregistered,
Packit ae235b
                                                       &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (registration_id > 0);
Packit ae235b
  boss_foo_reg_id = registration_id;
Packit ae235b
  num_successful_registrations++;
Packit ae235b
Packit ae235b
  registration_id = g_dbus_connection_register_object (c,
Packit ae235b
                                                       "/foo/boss",
Packit ae235b
                                                       (GDBusInterfaceInfo *) &bar_interface_info,
Packit ae235b
                                                       NULL,
Packit ae235b
                                                       &data,
Packit ae235b
                                                       on_object_unregistered,
Packit ae235b
                                                       &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (registration_id > 0);
Packit ae235b
  boss_bar_reg_id = registration_id;
Packit ae235b
  num_successful_registrations++;
Packit ae235b
Packit ae235b
  registration_id = g_dbus_connection_register_object (c,
Packit ae235b
                                                       "/foo/boss/worker1",
Packit ae235b
                                                       (GDBusInterfaceInfo *) &foo_interface_info,
Packit ae235b
                                                       NULL,
Packit ae235b
                                                       &data,
Packit ae235b
                                                       on_object_unregistered,
Packit ae235b
                                                       &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (registration_id > 0);
Packit ae235b
  worker1_foo_reg_id = registration_id;
Packit ae235b
  num_successful_registrations++;
Packit ae235b
Packit ae235b
  registration_id = g_dbus_connection_register_object (c,
Packit ae235b
                                                       "/foo/boss/worker1p1",
Packit ae235b
                                                       (GDBusInterfaceInfo *) &foo_interface_info,
Packit ae235b
                                                       NULL,
Packit ae235b
                                                       &data,
Packit ae235b
                                                       on_object_unregistered,
Packit ae235b
                                                       &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (registration_id > 0);
Packit ae235b
  worker1p1_foo_reg_id = registration_id;
Packit ae235b
  num_successful_registrations++;
Packit ae235b
Packit ae235b
  registration_id = g_dbus_connection_register_object (c,
Packit ae235b
                                                       "/foo/boss/worker2",
Packit ae235b
                                                       (GDBusInterfaceInfo *) &bar_interface_info,
Packit ae235b
                                                       NULL,
Packit ae235b
                                                       &data,
Packit ae235b
                                                       on_object_unregistered,
Packit ae235b
                                                       &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (registration_id > 0);
Packit ae235b
  worker2_bar_reg_id = registration_id;
Packit ae235b
  num_successful_registrations++;
Packit ae235b
Packit ae235b
  registration_id = g_dbus_connection_register_object (c,
Packit ae235b
                                                       "/foo/boss/interns/intern1",
Packit ae235b
                                                       (GDBusInterfaceInfo *) &foo_interface_info,
Packit ae235b
                                                       NULL,
Packit ae235b
                                                       &data,
Packit ae235b
                                                       on_object_unregistered,
Packit ae235b
                                                       &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (registration_id > 0);
Packit ae235b
  intern1_foo_reg_id = registration_id;
Packit ae235b
  num_successful_registrations++;
Packit ae235b
Packit ae235b
  /* ... and try again at another path */
Packit ae235b
  registration_id = g_dbus_connection_register_object (c,
Packit ae235b
                                                       "/foo/boss/interns/intern2",
Packit ae235b
                                                       (GDBusInterfaceInfo *) &bar_interface_info,
Packit ae235b
                                                       NULL,
Packit ae235b
                                                       &data,
Packit ae235b
                                                       on_object_unregistered,
Packit ae235b
                                                       &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (registration_id > 0);
Packit ae235b
  intern2_bar_reg_id = registration_id;
Packit ae235b
  num_successful_registrations++;
Packit ae235b
Packit ae235b
  /* register at the same path/interface - this should fail */
Packit ae235b
  registration_id = g_dbus_connection_register_object (c,
Packit ae235b
                                                       "/foo/boss/interns/intern2",
Packit ae235b
                                                       (GDBusInterfaceInfo *) &bar_interface_info,
Packit ae235b
                                                       NULL,
Packit ae235b
                                                       &data,
Packit ae235b
                                                       on_object_unregistered,
Packit ae235b
                                                       &error);
Packit ae235b
  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
Packit ae235b
  g_assert (!g_dbus_error_is_remote_error (error));
Packit ae235b
  g_error_free (error);
Packit ae235b
  error = NULL;
Packit ae235b
  g_assert (registration_id == 0);
Packit ae235b
Packit ae235b
  /* register at different interface - shouldn't fail */
Packit ae235b
  registration_id = g_dbus_connection_register_object (c,
Packit ae235b
                                                       "/foo/boss/interns/intern2",
Packit ae235b
                                                       (GDBusInterfaceInfo *) &foo_interface_info,
Packit ae235b
                                                       NULL,
Packit ae235b
                                                       &data,
Packit ae235b
                                                       on_object_unregistered,
Packit ae235b
                                                       &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (registration_id > 0);
Packit ae235b
  intern2_foo_reg_id = registration_id;
Packit ae235b
  num_successful_registrations++;
Packit ae235b
Packit ae235b
  /* unregister it via the id */
Packit ae235b
  g_assert (g_dbus_connection_unregister_object (c, registration_id));
Packit ae235b
  g_main_context_iteration (NULL, FALSE);
Packit ae235b
  g_assert_cmpint (data.num_unregistered_calls, ==, 1);
Packit ae235b
  intern2_foo_reg_id = 0;
Packit ae235b
Packit ae235b
  /* register it back */
Packit ae235b
  registration_id = g_dbus_connection_register_object (c,
Packit ae235b
                                                       "/foo/boss/interns/intern2",
Packit ae235b
                                                       (GDBusInterfaceInfo *) &foo_interface_info,
Packit ae235b
                                                       NULL,
Packit ae235b
                                                       &data,
Packit ae235b
                                                       on_object_unregistered,
Packit ae235b
                                                       &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (registration_id > 0);
Packit ae235b
  intern2_foo_reg_id = registration_id;
Packit ae235b
  num_successful_registrations++;
Packit ae235b
Packit ae235b
  registration_id = g_dbus_connection_register_object (c,
Packit ae235b
                                                       "/foo/boss/interns/intern3",
Packit ae235b
                                                       (GDBusInterfaceInfo *) &bar_interface_info,
Packit ae235b
                                                       NULL,
Packit ae235b
                                                       &data,
Packit ae235b
                                                       on_object_unregistered,
Packit ae235b
                                                       &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (registration_id > 0);
Packit ae235b
  intern3_bar_reg_id = registration_id;
Packit ae235b
  num_successful_registrations++;
Packit ae235b
Packit ae235b
  /* now register a whole subtree at /foo/boss/executives */
Packit ae235b
  subtree_registration_id = g_dbus_connection_register_subtree (c,
Packit ae235b
                                                                "/foo/boss/executives",
Packit ae235b
                                                                &subtree_vtable,
Packit ae235b
                                                                G_DBUS_SUBTREE_FLAGS_NONE,
Packit ae235b
                                                                &data,
Packit ae235b
                                                                on_subtree_unregistered,
Packit ae235b
                                                                &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (subtree_registration_id > 0);
Packit ae235b
  /* try registering it again.. this should fail */
Packit ae235b
  registration_id = g_dbus_connection_register_subtree (c,
Packit ae235b
                                                        "/foo/boss/executives",
Packit ae235b
                                                        &subtree_vtable,
Packit ae235b
                                                        G_DBUS_SUBTREE_FLAGS_NONE,
Packit ae235b
                                                        &data,
Packit ae235b
                                                        on_subtree_unregistered,
Packit ae235b
                                                        &error);
Packit ae235b
  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
Packit ae235b
  g_assert (!g_dbus_error_is_remote_error (error));
Packit ae235b
  g_error_free (error);
Packit ae235b
  error = NULL;
Packit ae235b
  g_assert (registration_id == 0);
Packit ae235b
Packit ae235b
  /* unregister it, then register it again */
Packit ae235b
  g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 0);
Packit ae235b
  g_assert (g_dbus_connection_unregister_subtree (c, subtree_registration_id));
Packit ae235b
  g_main_context_iteration (NULL, FALSE);
Packit ae235b
  g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 1);
Packit ae235b
  subtree_registration_id = g_dbus_connection_register_subtree (c,
Packit ae235b
                                                                "/foo/boss/executives",
Packit ae235b
                                                                &subtree_vtable,
Packit ae235b
                                                                G_DBUS_SUBTREE_FLAGS_NONE,
Packit ae235b
                                                                &data,
Packit ae235b
                                                                on_subtree_unregistered,
Packit ae235b
                                                                &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (subtree_registration_id > 0);
Packit ae235b
Packit ae235b
  /* try to register something under /foo/boss/executives - this should work
Packit ae235b
   * because registered subtrees and registered objects can coexist.
Packit ae235b
   *
Packit ae235b
   * Make the exported object implement *two* interfaces so we can check
Packit ae235b
   * that the right introspection handler is invoked.
Packit ae235b
   */
Packit ae235b
  registration_id = g_dbus_connection_register_object (c,
Packit ae235b
                                                       "/foo/boss/executives/non_subtree_object",
Packit ae235b
                                                       (GDBusInterfaceInfo *) &bar_interface_info,
Packit ae235b
                                                       NULL,
Packit ae235b
                                                       &data,
Packit ae235b
                                                       on_object_unregistered,
Packit ae235b
                                                       &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (registration_id > 0);
Packit ae235b
  non_subtree_object_path_bar_reg_id = registration_id;
Packit ae235b
  num_successful_registrations++;
Packit ae235b
  registration_id = g_dbus_connection_register_object (c,
Packit ae235b
                                                       "/foo/boss/executives/non_subtree_object",
Packit ae235b
                                                       (GDBusInterfaceInfo *) &foo_interface_info,
Packit ae235b
                                                       NULL,
Packit ae235b
                                                       &data,
Packit ae235b
                                                       on_object_unregistered,
Packit ae235b
                                                       &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (registration_id > 0);
Packit ae235b
  non_subtree_object_path_foo_reg_id = registration_id;
Packit ae235b
  num_successful_registrations++;
Packit ae235b
Packit ae235b
  /* now register a dynamic subtree, spawning objects as they are called */
Packit ae235b
  dyna_data = g_ptr_array_new_with_free_func (g_free);
Packit ae235b
  dyna_subtree_registration_id = g_dbus_connection_register_subtree (c,
Packit ae235b
                                                                     "/foo/dyna",
Packit ae235b
                                                                     &dynamic_subtree_vtable,
Packit ae235b
                                                                     G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES,
Packit ae235b
                                                                     dyna_data,
Packit ae235b
                                                                     (GDestroyNotify)g_ptr_array_unref,
Packit ae235b
                                                                     &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (dyna_subtree_registration_id > 0);
Packit ae235b
Packit ae235b
  /* First assert that we have no nodes in the dynamic subtree */
Packit ae235b
  nodes = get_nodes_at (c, "/foo/dyna");
Packit ae235b
  g_assert (nodes != NULL);
Packit ae235b
  g_assert_cmpint (g_strv_length (nodes), ==, 0);
Packit ae235b
  g_strfreev (nodes);
Packit ae235b
  g_assert_cmpint (count_interfaces (c, "/foo/dyna"), ==, 4);
Packit ae235b
Packit ae235b
  /* Install three nodes in the dynamic subtree via the dyna_data backdoor and
Packit ae235b
   * assert that they show up correctly in the introspection data */
Packit ae235b
  g_ptr_array_add (dyna_data, g_strdup ("lol"));
Packit ae235b
  g_ptr_array_add (dyna_data, g_strdup ("cat"));
Packit ae235b
  g_ptr_array_add (dyna_data, g_strdup ("cheezburger"));
Packit ae235b
  nodes = get_nodes_at (c, "/foo/dyna");
Packit ae235b
  g_assert (nodes != NULL);
Packit ae235b
  g_assert_cmpint (g_strv_length (nodes), ==, 3);
Packit ae235b
  g_assert_cmpstr (nodes[0], ==, "lol");
Packit ae235b
  g_assert_cmpstr (nodes[1], ==, "cat");
Packit ae235b
  g_assert_cmpstr (nodes[2], ==, "cheezburger");
Packit ae235b
  g_strfreev (nodes);
Packit ae235b
  g_assert_cmpint (count_interfaces (c, "/foo/dyna/lol"), ==, 4);
Packit ae235b
  g_assert_cmpint (count_interfaces (c, "/foo/dyna/cat"), ==, 4);
Packit ae235b
  g_assert_cmpint (count_interfaces (c, "/foo/dyna/cheezburger"), ==, 4);
Packit ae235b
Packit ae235b
  /* Call a non-existing object path and assert that it has been created */
Packit ae235b
  dyna_create (c, "dynamicallycreated");
Packit ae235b
  nodes = get_nodes_at (c, "/foo/dyna");
Packit ae235b
  g_assert (nodes != NULL);
Packit ae235b
  g_assert_cmpint (g_strv_length (nodes), ==, 4);
Packit ae235b
  g_assert_cmpstr (nodes[0], ==, "lol");
Packit ae235b
  g_assert_cmpstr (nodes[1], ==, "cat");
Packit ae235b
  g_assert_cmpstr (nodes[2], ==, "cheezburger");
Packit ae235b
  g_assert_cmpstr (nodes[3], ==, "dynamicallycreated");
Packit ae235b
  g_strfreev (nodes);
Packit ae235b
  g_assert_cmpint (count_interfaces (c, "/foo/dyna/dynamicallycreated"), ==, 4);
Packit ae235b
Packit ae235b
  /* now check that the object hierarachy is properly generated... yes, it's a bit
Packit ae235b
   * perverse that we round-trip to the bus to introspect ourselves ;-)
Packit ae235b
   */
Packit ae235b
  nodes = get_nodes_at (c, "/");
Packit ae235b
  g_assert (nodes != NULL);
Packit ae235b
  g_assert_cmpint (g_strv_length (nodes), ==, 1);
Packit ae235b
  g_assert_cmpstr (nodes[0], ==, "foo");
Packit ae235b
  g_strfreev (nodes);
Packit ae235b
  g_assert_cmpint (count_interfaces (c, "/"), ==, 0);
Packit ae235b
Packit ae235b
  nodes = get_nodes_at (c, "/foo");
Packit ae235b
  g_assert (nodes != NULL);
Packit ae235b
  g_assert_cmpint (g_strv_length (nodes), ==, 2);
Packit ae235b
  g_assert_cmpstr (nodes[0], ==, "boss");
Packit ae235b
  g_assert_cmpstr (nodes[1], ==, "dyna");
Packit ae235b
  g_strfreev (nodes);
Packit ae235b
  g_assert_cmpint (count_interfaces (c, "/foo"), ==, 0);
Packit ae235b
Packit ae235b
  nodes = get_nodes_at (c, "/foo/boss");
Packit ae235b
  g_assert (nodes != NULL);
Packit ae235b
  g_assert_cmpint (g_strv_length (nodes), ==, 5);
Packit ae235b
  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "worker1"));
Packit ae235b
  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "worker1p1"));
Packit ae235b
  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "worker2"));
Packit ae235b
  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "interns"));
Packit ae235b
  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "executives"));
Packit ae235b
  g_strfreev (nodes);
Packit ae235b
  /* any registered object always implement org.freedesktop.DBus.[Peer,Introspectable,Properties] */
Packit ae235b
  g_assert_cmpint (count_interfaces (c, "/foo/boss"), ==, 5);
Packit ae235b
  g_assert (has_interface (c, "/foo/boss", foo_interface_info.name));
Packit ae235b
  g_assert (has_interface (c, "/foo/boss", bar_interface_info.name));
Packit ae235b
Packit ae235b
  /* check subtree nodes - we should have only non_subtree_object in /foo/boss/executives
Packit ae235b
   * because data.num_subtree_nodes is 0
Packit ae235b
   */
Packit ae235b
  nodes = get_nodes_at (c, "/foo/boss/executives");
Packit ae235b
  g_assert (nodes != NULL);
Packit ae235b
  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
Packit ae235b
  g_assert_cmpint (g_strv_length (nodes), ==, 1);
Packit ae235b
  g_strfreev (nodes);
Packit ae235b
  g_assert_cmpint (count_interfaces (c, "/foo/boss/executives"), ==, 0);
Packit ae235b
Packit ae235b
  /* now change data.num_subtree_nodes and check */
Packit ae235b
  data.num_subtree_nodes = 2;
Packit ae235b
  nodes = get_nodes_at (c, "/foo/boss/executives");
Packit ae235b
  g_assert (nodes != NULL);
Packit ae235b
  g_assert_cmpint (g_strv_length (nodes), ==, 5);
Packit ae235b
  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
Packit ae235b
  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp0"));
Packit ae235b
  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp1"));
Packit ae235b
  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp0"));
Packit ae235b
  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp1"));
Packit ae235b
  /* check that /foo/boss/executives/non_subtree_object is not handled by the
Packit ae235b
   * subtree handlers - we can do this because objects from subtree handlers
Packit ae235b
   * has exactly one interface and non_subtree_object has two
Packit ae235b
   */
Packit ae235b
  g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/non_subtree_object"), ==, 5);
Packit ae235b
  g_assert (has_interface (c, "/foo/boss/executives/non_subtree_object", foo_interface_info.name));
Packit ae235b
  g_assert (has_interface (c, "/foo/boss/executives/non_subtree_object", bar_interface_info.name));
Packit ae235b
  /* check that the vp and evp objects are handled by the subtree handlers */
Packit ae235b
  g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/vp0"), ==, 4);
Packit ae235b
  g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/vp1"), ==, 4);
Packit ae235b
  g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/evp0"), ==, 4);
Packit ae235b
  g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/evp1"), ==, 4);
Packit ae235b
  g_assert (has_interface (c, "/foo/boss/executives/vp0", foo_interface_info.name));
Packit ae235b
  g_assert (has_interface (c, "/foo/boss/executives/vp1", foo_interface_info.name));
Packit ae235b
  g_assert (has_interface (c, "/foo/boss/executives/evp0", bar_interface_info.name));
Packit ae235b
  g_assert (has_interface (c, "/foo/boss/executives/evp1", bar_interface_info.name));
Packit ae235b
  g_strfreev (nodes);
Packit ae235b
  data.num_subtree_nodes = 3;
Packit ae235b
  nodes = get_nodes_at (c, "/foo/boss/executives");
Packit ae235b
  g_assert (nodes != NULL);
Packit ae235b
  g_assert_cmpint (g_strv_length (nodes), ==, 7);
Packit ae235b
  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
Packit ae235b
  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp0"));
Packit ae235b
  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp1"));
Packit ae235b
  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp2"));
Packit ae235b
  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp0"));
Packit ae235b
  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp1"));
Packit ae235b
  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp2"));
Packit ae235b
  g_strfreev (nodes);
Packit ae235b
Packit ae235b
  /* This is to check that a bug (rather, class of bugs) in gdbusconnection.c's
Packit ae235b
   *
Packit ae235b
   *  g_dbus_connection_list_registered_unlocked()
Packit ae235b
   *
Packit ae235b
   * where /foo/boss/worker1 reported a child '1', is now fixed.
Packit ae235b
   */
Packit ae235b
  nodes = get_nodes_at (c, "/foo/boss/worker1");
Packit ae235b
  g_assert (nodes != NULL);
Packit ae235b
  g_assert_cmpint (g_strv_length (nodes), ==, 0);
Packit ae235b
  g_strfreev (nodes);
Packit ae235b
Packit ae235b
  /* check that calls are properly dispatched to the functions in foo_vtable for objects
Packit ae235b
   * implementing the org.example.Foo interface
Packit ae235b
   *
Packit ae235b
   * We do this for both a regular registered object (/foo/boss) and also for an object
Packit ae235b
   * registered through the subtree mechanism.
Packit ae235b
   */
Packit ae235b
  test_dispatch ("/foo/boss", TRUE);
Packit ae235b
  test_dispatch ("/foo/boss/executives/vp0", TRUE);
Packit ae235b
Packit ae235b
  /* To prevent from exiting and attaching a D-Bus tool like D-Feet; uncomment: */
Packit ae235b
#if 0
Packit ae235b
  g_debug ("Point D-feet or other tool at: %s", g_test_dbus_get_temporary_address());
Packit ae235b
  g_main_loop_run (loop);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  /* check that unregistering the subtree handler works */
Packit ae235b
  g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 1);
Packit ae235b
  g_assert (g_dbus_connection_unregister_subtree (c, subtree_registration_id));
Packit ae235b
  g_main_context_iteration (NULL, FALSE);
Packit ae235b
  g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 2);
Packit ae235b
  nodes = get_nodes_at (c, "/foo/boss/executives");
Packit ae235b
  g_assert (nodes != NULL);
Packit ae235b
  g_assert_cmpint (g_strv_length (nodes), ==, 1);
Packit ae235b
  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
Packit ae235b
  g_strfreev (nodes);
Packit ae235b
Packit ae235b
  g_assert (g_dbus_connection_unregister_object (c, boss_foo_reg_id));
Packit ae235b
  g_assert (g_dbus_connection_unregister_object (c, boss_bar_reg_id));
Packit ae235b
  g_assert (g_dbus_connection_unregister_object (c, worker1_foo_reg_id));
Packit ae235b
  g_assert (g_dbus_connection_unregister_object (c, worker1p1_foo_reg_id));
Packit ae235b
  g_assert (g_dbus_connection_unregister_object (c, worker2_bar_reg_id));
Packit ae235b
  g_assert (g_dbus_connection_unregister_object (c, intern1_foo_reg_id));
Packit ae235b
  g_assert (g_dbus_connection_unregister_object (c, intern2_bar_reg_id));
Packit ae235b
  g_assert (g_dbus_connection_unregister_object (c, intern2_foo_reg_id));
Packit ae235b
  g_assert (g_dbus_connection_unregister_object (c, intern3_bar_reg_id));
Packit ae235b
  g_assert (g_dbus_connection_unregister_object (c, non_subtree_object_path_bar_reg_id));
Packit ae235b
  g_assert (g_dbus_connection_unregister_object (c, non_subtree_object_path_foo_reg_id));
Packit ae235b
Packit ae235b
  g_main_context_iteration (NULL, FALSE);
Packit ae235b
  g_assert_cmpint (data.num_unregistered_calls, ==, num_successful_registrations);
Packit ae235b
Packit ae235b
  /* check that we no longer export any objects - TODO: it looks like there's a bug in
Packit ae235b
   * libdbus-1 here: libdbus still reports the '/foo' object; so disable the test for now
Packit ae235b
   */
Packit ae235b
#if 0
Packit ae235b
  nodes = get_nodes_at (c, "/");
Packit ae235b
  g_assert (nodes != NULL);
Packit ae235b
  g_assert_cmpint (g_strv_length (nodes), ==, 0);
Packit ae235b
  g_strfreev (nodes);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  g_object_unref (c);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_object_registration_with_closures (void)
Packit ae235b
{
Packit ae235b
  GError *error;
Packit ae235b
  guint registration_id;
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
  registration_id = g_dbus_connection_register_object_with_closures (c,
Packit ae235b
                                                                     "/foo/boss",
Packit ae235b
                                                                     (GDBusInterfaceInfo *) &foo_interface_info,
Packit ae235b
                                                                     g_cclosure_new (G_CALLBACK (foo_method_call), NULL, NULL),
Packit ae235b
                                                                     g_cclosure_new (G_CALLBACK (foo_get_property), NULL, NULL),
Packit ae235b
                                                                     g_cclosure_new (G_CALLBACK (foo_set_property), NULL, NULL),
Packit ae235b
                                                                     &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (registration_id > 0);
Packit ae235b
Packit ae235b
  test_dispatch ("/foo/boss", FALSE);
Packit ae235b
Packit ae235b
  g_assert (g_dbus_connection_unregister_object (c, registration_id));
Packit ae235b
Packit ae235b
  g_object_unref (c);
Packit ae235b
}
Packit ae235b
Packit ae235b
static const GDBusInterfaceInfo test_interface_info1 =
Packit ae235b
{
Packit ae235b
  -1,
Packit ae235b
  "org.example.Foo",
Packit ae235b
  (GDBusMethodInfo **) NULL,
Packit ae235b
  (GDBusSignalInfo **) NULL,
Packit ae235b
  (GDBusPropertyInfo **) NULL,
Packit ae235b
  NULL,
Packit ae235b
};
Packit ae235b
Packit ae235b
static const GDBusInterfaceInfo test_interface_info2 =
Packit ae235b
{
Packit ae235b
  -1,
Packit ae235b
  "org.freedesktop.DBus.Properties",
Packit ae235b
  (GDBusMethodInfo **) NULL,
Packit ae235b
  (GDBusSignalInfo **) NULL,
Packit ae235b
  (GDBusPropertyInfo **) NULL,
Packit ae235b
  NULL,
Packit ae235b
};
Packit ae235b
Packit ae235b
static void
Packit ae235b
check_interfaces (GDBusConnection  *c,
Packit ae235b
                  const gchar      *object_path,
Packit ae235b
                  const gchar     **interfaces)
Packit ae235b
{
Packit ae235b
  GError *error;
Packit ae235b
  GDBusProxy *proxy;
Packit ae235b
  gchar *xml_data;
Packit ae235b
  GDBusNodeInfo *node_info;
Packit ae235b
  gint i, j;
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  proxy = g_dbus_proxy_new_sync (c,
Packit ae235b
                                 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
Packit ae235b
                                 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
Packit ae235b
                                 NULL,
Packit ae235b
                                 g_dbus_connection_get_unique_name (c),
Packit ae235b
                                 object_path,
Packit ae235b
                                 "org.freedesktop.DBus.Introspectable",
Packit ae235b
                                 NULL,
Packit ae235b
                                 &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (proxy != NULL);
Packit ae235b
Packit ae235b
  /* do this async to avoid libdbus-1 deadlocks */
Packit ae235b
  xml_data = NULL;
Packit ae235b
  g_dbus_proxy_call (proxy,
Packit ae235b
                     "Introspect",
Packit ae235b
                     NULL,
Packit ae235b
                     G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                     -1,
Packit ae235b
                     NULL,
Packit ae235b
                     (GAsyncReadyCallback) introspect_callback,
Packit ae235b
                     &xml_data);
Packit ae235b
  g_main_loop_run (loop);
Packit ae235b
  g_assert (xml_data != NULL);
Packit ae235b
Packit ae235b
  node_info = g_dbus_node_info_new_for_xml (xml_data, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (node_info != NULL);
Packit ae235b
Packit ae235b
  g_assert (node_info->interfaces != NULL);
Packit ae235b
  for (i = 0; node_info->interfaces[i]; i++) ;
Packit ae235b
#if 0
Packit ae235b
  if (g_strv_length ((gchar**)interfaces) != i - 1)
Packit ae235b
    {
Packit ae235b
      g_printerr ("expected ");
Packit ae235b
      for (i = 0; interfaces[i]; i++)
Packit ae235b
        g_printerr ("%s ", interfaces[i]);
Packit ae235b
      g_printerr ("\ngot ");
Packit ae235b
      for (i = 0; node_info->interfaces[i]; i++)
Packit ae235b
        g_printerr ("%s ", node_info->interfaces[i]->name);
Packit ae235b
      g_printerr ("\n");
Packit ae235b
    }
Packit ae235b
#endif
Packit ae235b
  g_assert_cmpint (g_strv_length ((gchar**)interfaces), ==, i - 1);
Packit ae235b
Packit ae235b
  for (i = 0; interfaces[i]; i++)
Packit ae235b
    {
Packit ae235b
      for (j = 0; node_info->interfaces[j]; j++)
Packit ae235b
        {
Packit ae235b
          if (strcmp (interfaces[i], node_info->interfaces[j]->name) == 0)
Packit ae235b
            goto found;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      g_assert_not_reached ();
Packit ae235b
Packit ae235b
 found: ;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_object_unref (proxy);
Packit ae235b
  g_free (xml_data);
Packit ae235b
  g_dbus_node_info_unref (node_info);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_registered_interfaces (void)
Packit ae235b
{
Packit ae235b
  GError *error;
Packit ae235b
  guint id1, id2;
Packit ae235b
  const gchar *interfaces[] = {
Packit ae235b
    "org.example.Foo",
Packit ae235b
    "org.freedesktop.DBus.Properties",
Packit ae235b
    "org.freedesktop.DBus.Introspectable",
Packit ae235b
    NULL,
Packit ae235b
  };
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
  id1 = g_dbus_connection_register_object (c,
Packit ae235b
                                           "/test",
Packit ae235b
                                           (GDBusInterfaceInfo *) &test_interface_info1,
Packit ae235b
                                           NULL,
Packit ae235b
                                           NULL,
Packit ae235b
                                           NULL,
Packit ae235b
                                           &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (id1 > 0);
Packit ae235b
  id2 = g_dbus_connection_register_object (c,
Packit ae235b
                                           "/test",
Packit ae235b
                                           (GDBusInterfaceInfo *) &test_interface_info2,
Packit ae235b
                                           NULL,
Packit ae235b
                                           NULL,
Packit ae235b
                                           NULL,
Packit ae235b
                                           &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (id2 > 0);
Packit ae235b
Packit ae235b
  check_interfaces (c, "/test", interfaces);
Packit ae235b
Packit ae235b
  g_assert (g_dbus_connection_unregister_object (c, id1));
Packit ae235b
  g_assert (g_dbus_connection_unregister_object (c, id2));
Packit ae235b
  g_object_unref (c);
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_async_method_call (GDBusConnection       *connection,
Packit ae235b
                        const gchar           *sender,
Packit ae235b
                        const gchar           *object_path,
Packit ae235b
                        const gchar           *interface_name,
Packit ae235b
                        const gchar           *method_name,
Packit ae235b
                        GVariant              *parameters,
Packit ae235b
                        GDBusMethodInvocation *invocation,
Packit ae235b
                        gpointer               user_data)
Packit ae235b
{
Packit ae235b
  const GDBusPropertyInfo *property;
Packit ae235b
Packit ae235b
  /* Strictly speaking, this function should also expect to receive
Packit ae235b
   * method calls not on the org.freedesktop.DBus.Properties interface,
Packit ae235b
   * but we don't do any during this testcase, so assert that.
Packit ae235b
   */
Packit ae235b
  g_assert_cmpstr (interface_name, ==, "org.freedesktop.DBus.Properties");
Packit ae235b
  g_assert (g_dbus_method_invocation_get_method_info (invocation) == NULL);
Packit ae235b
Packit ae235b
  property = g_dbus_method_invocation_get_property_info (invocation);
Packit ae235b
Packit ae235b
  /* We should never be seeing any property calls on the com.example.Bar
Packit ae235b
   * interface because it doesn't export any properties.
Packit ae235b
   *
Packit ae235b
   * In each case below make sure the interface is org.example.Foo.
Packit ae235b
   */
Packit ae235b
Packit ae235b
  /* Do a whole lot of asserts to make sure that invalid calls are still
Packit ae235b
   * getting properly rejected by GDBusConnection and that our
Packit ae235b
   * environment is as we expect it to be.
Packit ae235b
   */
Packit ae235b
  if (g_str_equal (method_name, "Get"))
Packit ae235b
    {
Packit ae235b
      const gchar *iface_name, *prop_name;
Packit ae235b
Packit ae235b
      g_variant_get (parameters, "(&s&s)", &iface_name, &prop_name);
Packit ae235b
      g_assert_cmpstr (iface_name, ==, "org.example.Foo");
Packit ae235b
      g_assert (property != NULL);
Packit ae235b
      g_assert_cmpstr (prop_name, ==, property->name);
Packit ae235b
      g_assert (property->flags & G_DBUS_PROPERTY_INFO_FLAGS_READABLE);
Packit ae235b
      g_dbus_method_invocation_return_value (invocation, g_variant_new ("(v)", g_variant_new_string (prop_name)));
Packit ae235b
    }
Packit ae235b
Packit ae235b
  else if (g_str_equal (method_name, "Set"))
Packit ae235b
    {
Packit ae235b
      const gchar *iface_name, *prop_name;
Packit ae235b
      GVariant *value;
Packit ae235b
Packit ae235b
      g_variant_get (parameters, "(&s&sv)", &iface_name, &prop_name, &value);
Packit ae235b
      g_assert_cmpstr (iface_name, ==, "org.example.Foo");
Packit ae235b
      g_assert (property != NULL);
Packit ae235b
      g_assert_cmpstr (prop_name, ==, property->name);
Packit ae235b
      g_assert (property->flags & G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE);
Packit ae235b
      g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE (property->signature)));
Packit ae235b
      g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
Packit ae235b
      g_variant_unref (value);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  else if (g_str_equal (method_name, "GetAll"))
Packit ae235b
    {
Packit ae235b
      const gchar *iface_name;
Packit ae235b
Packit ae235b
      g_variant_get (parameters, "(&s)", &iface_name);
Packit ae235b
      g_assert_cmpstr (iface_name, ==, "org.example.Foo");
Packit ae235b
      g_assert (property == NULL);
Packit ae235b
      g_dbus_method_invocation_return_value (invocation,
Packit ae235b
                                             g_variant_new_parsed ("({ 'PropertyUno': < 'uno' >,"
Packit ae235b
                                                                   "   'NotWritable': < 'notwrite' > },)"));
Packit ae235b
    }
Packit ae235b
Packit ae235b
  else
Packit ae235b
    g_assert_not_reached ();
Packit ae235b
}
Packit ae235b
Packit ae235b
static gint outstanding_cases;
Packit ae235b
Packit ae235b
static void
Packit ae235b
ensure_result_cb (GObject      *source,
Packit ae235b
                  GAsyncResult *result,
Packit ae235b
                  gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GDBusConnection *connection = G_DBUS_CONNECTION (source);
Packit ae235b
  GVariant *reply;
Packit ae235b
Packit ae235b
  reply = g_dbus_connection_call_finish (connection, result, NULL);
Packit ae235b
Packit ae235b
  if (user_data == NULL)
Packit ae235b
    {
Packit ae235b
      /* Expected an error */
Packit ae235b
      g_assert (reply == NULL);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      /* Expected a reply of a particular format. */
Packit ae235b
      gchar *str;
Packit ae235b
Packit ae235b
      g_assert (reply != NULL);
Packit ae235b
      str = g_variant_print (reply, TRUE);
Packit ae235b
      g_assert_cmpstr (str, ==, (const gchar *) user_data);
Packit ae235b
      g_free (str);
Packit ae235b
Packit ae235b
      g_variant_unref (reply);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_assert_cmpint (outstanding_cases, >, 0);
Packit ae235b
  outstanding_cases--;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_async_case (GDBusConnection *connection,
Packit ae235b
                 const gchar     *expected_reply,
Packit ae235b
                 const gchar     *method,
Packit ae235b
                 const gchar     *format_string,
Packit ae235b
                 ...)
Packit ae235b
{
Packit ae235b
  va_list ap;
Packit ae235b
Packit ae235b
  va_start (ap, format_string);
Packit ae235b
Packit ae235b
  g_dbus_connection_call (connection, g_dbus_connection_get_unique_name (connection), "/foo",
Packit ae235b
                          "org.freedesktop.DBus.Properties", method, g_variant_new_va (format_string, NULL, &ap),
Packit ae235b
                          NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, ensure_result_cb, (gpointer) expected_reply);
Packit ae235b
Packit ae235b
  va_end (ap);
Packit ae235b
Packit ae235b
  outstanding_cases++;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_async_properties (void)
Packit ae235b
{
Packit ae235b
  GError *error = NULL;
Packit ae235b
  guint registration_id, registration_id2;
Packit ae235b
  static const GDBusInterfaceVTable vtable = {
Packit ae235b
    test_async_method_call, NULL, NULL
Packit ae235b
  };
Packit ae235b
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
  registration_id = g_dbus_connection_register_object (c,
Packit ae235b
                                                       "/foo",
Packit ae235b
                                                       (GDBusInterfaceInfo *) &foo_interface_info,
Packit ae235b
                                                       &vtable, NULL, NULL, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (registration_id);
Packit ae235b
  registration_id2 = g_dbus_connection_register_object (c,
Packit ae235b
                                                        "/foo",
Packit ae235b
                                                        (GDBusInterfaceInfo *) &foo2_interface_info,
Packit ae235b
                                                        &vtable, NULL, NULL, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (registration_id);
Packit ae235b
Packit ae235b
  test_async_case (c, NULL, "random", "()");
Packit ae235b
Packit ae235b
  /* Test a variety of error cases */
Packit ae235b
  test_async_case (c, NULL, "Get", "(si)", "wrong signature", 5);
Packit ae235b
  test_async_case (c, NULL, "Get", "(ss)", "org.example.WrongInterface", "zzz");
Packit ae235b
  test_async_case (c, NULL, "Get", "(ss)", "org.example.Foo", "NoSuchProperty");
Packit ae235b
  test_async_case (c, NULL, "Get", "(ss)", "org.example.Foo", "NotReadable");
Packit ae235b
Packit ae235b
  test_async_case (c, NULL, "Set", "(si)", "wrong signature", 5);
Packit ae235b
  test_async_case (c, NULL, "Set", "(ssv)", "org.example.WrongInterface", "zzz", g_variant_new_string (""));
Packit ae235b
  test_async_case (c, NULL, "Set", "(ssv)", "org.example.Foo", "NoSuchProperty", g_variant_new_string (""));
Packit ae235b
  test_async_case (c, NULL, "Set", "(ssv)", "org.example.Foo", "NotWritable", g_variant_new_string (""));
Packit ae235b
  test_async_case (c, NULL, "Set", "(ssv)", "org.example.Foo", "PropertyUno", g_variant_new_object_path ("/wrong"));
Packit ae235b
Packit ae235b
  test_async_case (c, NULL, "GetAll", "(si)", "wrong signature", 5);
Packit ae235b
  test_async_case (c, NULL, "GetAll", "(s)", "org.example.WrongInterface");
Packit ae235b
Packit ae235b
  /* Make sure that we get no unexpected async property calls for com.example.Foo2 */
Packit ae235b
  test_async_case (c, NULL, "Get", "(ss)", "org.example.Foo2", "zzz");
Packit ae235b
  test_async_case (c, NULL, "Set", "(ssv)", "org.example.Foo2", "zzz", g_variant_new_string (""));
Packit ae235b
  test_async_case (c, "(@a{sv} {},)", "GetAll", "(s)", "org.example.Foo2");
Packit ae235b
Packit ae235b
  /* Now do the proper things */
Packit ae235b
  test_async_case (c, "(<'PropertyUno'>,)", "Get", "(ss)", "org.example.Foo", "PropertyUno");
Packit ae235b
  test_async_case (c, "(<'NotWritable'>,)", "Get", "(ss)", "org.example.Foo", "NotWritable");
Packit ae235b
  test_async_case (c, "()", "Set", "(ssv)", "org.example.Foo", "PropertyUno", g_variant_new_string (""));
Packit ae235b
  test_async_case (c, "()", "Set", "(ssv)", "org.example.Foo", "NotReadable", g_variant_new_string (""));
Packit ae235b
  test_async_case (c, "({'PropertyUno': <'uno'>, 'NotWritable': <'notwrite'>},)", "GetAll", "(s)", "org.example.Foo");
Packit ae235b
Packit ae235b
  while (outstanding_cases)
Packit ae235b
    g_main_context_iteration (NULL, TRUE);
Packit ae235b
Packit ae235b
  g_dbus_connection_unregister_object (c, registration_id);
Packit ae235b
  g_dbus_connection_unregister_object (c, registration_id2);
Packit ae235b
  g_object_unref (c);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
int
Packit ae235b
main (int   argc,
Packit ae235b
      char *argv[])
Packit ae235b
{
Packit ae235b
  gint ret;
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
  g_test_add_func ("/gdbus/object-registration", test_object_registration);
Packit ae235b
  g_test_add_func ("/gdbus/object-registration-with-closures", test_object_registration_with_closures);
Packit ae235b
  g_test_add_func ("/gdbus/registered-interfaces", test_registered_interfaces);
Packit ae235b
  g_test_add_func ("/gdbus/async-properties", test_async_properties);
Packit ae235b
Packit ae235b
  /* TODO: check that we spit out correct introspection data */
Packit ae235b
  /* TODO: check that registering a whole subtree works */
Packit ae235b
Packit ae235b
  ret = session_bus_run ();
Packit ae235b
Packit ae235b
  g_main_loop_unref (loop);
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}