Blame gio/tests/gdbus-proxy-well-known-name.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
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
static void
Packit ae235b
proxy_new_cb (GObject       *source_object,
Packit ae235b
              GAsyncResult  *res,
Packit ae235b
              gpointer       user_data)
Packit ae235b
{
Packit ae235b
  GDBusProxy **ret = user_data;
Packit ae235b
  GError *error;
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  *ret = g_dbus_proxy_new_finish (res, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (ret != NULL);
Packit ae235b
Packit ae235b
  g_main_loop_quit (loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_proxy_well_known_name (void)
Packit ae235b
{
Packit ae235b
  GDBusProxy *p;
Packit ae235b
  GDBusProxy *p2;
Packit ae235b
  GDBusProxy *ap;
Packit ae235b
  GDBusProxy *ap2;
Packit ae235b
  GDBusConnection *c;
Packit ae235b
  GError *error;
Packit ae235b
  gchar *name_owner;
Packit ae235b
  gchar **property_names;
Packit ae235b
  GVariant *variant;
Packit ae235b
  GVariant *result;
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
  error = NULL;
Packit ae235b
  p = g_dbus_proxy_new_sync (c,
Packit ae235b
                             G_DBUS_PROXY_FLAGS_NONE,
Packit ae235b
                             NULL,                      /* GDBusInterfaceInfo* */
Packit ae235b
                             "com.example.TestService", /* name */
Packit ae235b
                             "/com/example/TestObject", /* object path */
Packit ae235b
                             "com.example.Frob",        /* interface name */
Packit ae235b
                             NULL,                      /* GCancellable */
Packit ae235b
                             &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  /* we shouldn't have a name owner nor any cached properties */
Packit ae235b
  g_assert_cmpstr (g_dbus_proxy_get_name_owner (p), ==, NULL);
Packit ae235b
  g_assert (g_dbus_proxy_get_cached_property_names (p) == NULL);
Packit ae235b
Packit ae235b
  /* also for async: we shouldn't have a name owner nor any cached properties */
Packit ae235b
  g_dbus_proxy_new (c,
Packit ae235b
                    G_DBUS_PROXY_FLAGS_NONE,
Packit ae235b
                    NULL,                      /* GDBusInterfaceInfo* */
Packit ae235b
                    "com.example.TestService", /* name */
Packit ae235b
                    "/com/example/TestObject", /* object path */
Packit ae235b
                    "com.example.Frob",        /* interface name */
Packit ae235b
                    NULL,                      /* GCancellable */
Packit ae235b
                    (GAsyncReadyCallback) proxy_new_cb,
Packit ae235b
                    &ap);
Packit ae235b
  g_main_loop_run (loop);
Packit ae235b
  g_assert_cmpstr (g_dbus_proxy_get_name_owner (ap), ==, NULL);
Packit ae235b
  g_assert (g_dbus_proxy_get_cached_property_names (ap) == NULL);
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
  /* check that we get the notify::g-name-owner signal */
Packit ae235b
  _g_assert_property_notify (p, "g-name-owner");
Packit ae235b
Packit ae235b
  /* Now we should have a name owner as well as properties */
Packit ae235b
  name_owner = g_dbus_proxy_get_name_owner (p);
Packit ae235b
  property_names = g_dbus_proxy_get_cached_property_names (p);
Packit ae235b
  g_assert (g_dbus_is_unique_name (name_owner));
Packit ae235b
  g_assert (property_names != NULL && g_strv_length (property_names) > 0);
Packit ae235b
  g_free (name_owner);
Packit ae235b
  g_strfreev (property_names);
Packit ae235b
Packit ae235b
  /* if we create another proxy with the service being available, check that
Packit ae235b
   * it has a name owner and properties
Packit ae235b
   */
Packit ae235b
  error = NULL;
Packit ae235b
  p2 = g_dbus_proxy_new_sync (c,
Packit ae235b
                              G_DBUS_PROXY_FLAGS_NONE,
Packit ae235b
                              NULL,                      /* GDBusInterfaceInfo* */
Packit ae235b
                              "com.example.TestService", /* name */
Packit ae235b
                              "/com/example/TestObject", /* object path */
Packit ae235b
                              "com.example.Frob",        /* interface name */
Packit ae235b
                              NULL,                      /* GCancellable */
Packit ae235b
                              &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  name_owner = g_dbus_proxy_get_name_owner (p2);
Packit ae235b
  property_names = g_dbus_proxy_get_cached_property_names (p2);
Packit ae235b
  g_assert (g_dbus_is_unique_name (name_owner));
Packit ae235b
  g_assert (property_names != NULL && g_strv_length (property_names) > 0);
Packit ae235b
  g_free (name_owner);
Packit ae235b
  g_strfreev (property_names);
Packit ae235b
Packit ae235b
  /* also for async: we should have a name owner and cached properties */
Packit ae235b
  g_dbus_proxy_new (c,
Packit ae235b
                    G_DBUS_PROXY_FLAGS_NONE,
Packit ae235b
                    NULL,                      /* GDBusInterfaceInfo* */
Packit ae235b
                    "com.example.TestService", /* name */
Packit ae235b
                    "/com/example/TestObject", /* object path */
Packit ae235b
                    "com.example.Frob",        /* interface name */
Packit ae235b
                    NULL,                      /* GCancellable */
Packit ae235b
                    (GAsyncReadyCallback) proxy_new_cb,
Packit ae235b
                    &ap2;;
Packit ae235b
  g_main_loop_run (loop);
Packit ae235b
  name_owner = g_dbus_proxy_get_name_owner (ap2);
Packit ae235b
  property_names = g_dbus_proxy_get_cached_property_names (ap2);
Packit ae235b
  g_assert (g_dbus_is_unique_name (name_owner));
Packit ae235b
  g_assert (property_names != NULL && g_strv_length (property_names) > 0);
Packit ae235b
  g_free (name_owner);
Packit ae235b
  g_strfreev (property_names);
Packit ae235b
Packit ae235b
  /* Check property value is the initial value */
Packit ae235b
  variant = g_dbus_proxy_get_cached_property (p, "y");
Packit ae235b
  g_assert (variant != NULL);
Packit ae235b
  g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
Packit ae235b
  g_variant_unref (variant);
Packit ae235b
  variant = g_dbus_proxy_get_cached_property (p2, "y");
Packit ae235b
  g_assert (variant != NULL);
Packit ae235b
  g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
Packit ae235b
  g_variant_unref (variant);
Packit ae235b
  variant = g_dbus_proxy_get_cached_property (ap, "y");
Packit ae235b
  g_assert (variant != NULL);
Packit ae235b
  g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
Packit ae235b
  g_variant_unref (variant);
Packit ae235b
  variant = g_dbus_proxy_get_cached_property (ap2, "y");
Packit ae235b
  g_assert (variant != NULL);
Packit ae235b
  g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
Packit ae235b
  g_variant_unref (variant);
Packit ae235b
Packit ae235b
  /* Check that properties are updated on both p and p2 */
Packit ae235b
  result = g_dbus_proxy_call_sync (p,
Packit ae235b
                                   "FrobSetProperty",
Packit ae235b
                                   g_variant_new ("(sv)",
Packit ae235b
                                                  "y",
Packit ae235b
                                                  g_variant_new_byte (42)),
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_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
Packit ae235b
  g_variant_unref (result);
Packit ae235b
  _g_assert_signal_received (p, "g-properties-changed");
Packit ae235b
  variant = g_dbus_proxy_get_cached_property (p, "y");
Packit ae235b
  g_assert (variant != NULL);
Packit ae235b
  g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
Packit ae235b
  g_variant_unref (variant);
Packit ae235b
  variant = g_dbus_proxy_get_cached_property (p2, "y");
Packit ae235b
  g_assert (variant != NULL);
Packit ae235b
  g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
Packit ae235b
  g_variant_unref (variant);
Packit ae235b
  variant = g_dbus_proxy_get_cached_property (ap, "y");
Packit ae235b
  g_assert (variant != NULL);
Packit ae235b
  g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
Packit ae235b
  g_variant_unref (variant);
Packit ae235b
  variant = g_dbus_proxy_get_cached_property (ap2, "y");
Packit ae235b
  g_assert (variant != NULL);
Packit ae235b
  g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
Packit ae235b
  g_variant_unref (variant);
Packit ae235b
Packit ae235b
  /* Nuke the service and check that we get the signal and then don't
Packit ae235b
   * have a name owner nor any cached properties
Packit ae235b
   */
Packit ae235b
  result = g_dbus_proxy_call_sync (p,
Packit ae235b
                                   "Quit",
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 (result != NULL);
Packit ae235b
  g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
Packit ae235b
  g_variant_unref (result);
Packit ae235b
  /* and wait... */
Packit ae235b
  _g_assert_property_notify (p, "g-name-owner");
Packit ae235b
  /* now we shouldn't have a name owner nor any cached properties */
Packit ae235b
  g_assert_cmpstr (g_dbus_proxy_get_name_owner (p), ==, NULL);
Packit ae235b
  g_assert (g_dbus_proxy_get_cached_property_names (p) == NULL);
Packit ae235b
  g_assert (g_dbus_proxy_get_cached_property (p, "y") == NULL);
Packit ae235b
Packit ae235b
  /* now bring back the server and wait for the proxy to be updated.. now
Packit ae235b
   * the 'y' property should be back at 1...
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
  /* check that we get the notify::g-name-owner signal */
Packit ae235b
  _g_assert_property_notify (p, "g-name-owner");
Packit ae235b
  /* Now we should have a name owner as well as properties */
Packit ae235b
  name_owner = g_dbus_proxy_get_name_owner (p);
Packit ae235b
  property_names = g_dbus_proxy_get_cached_property_names (p);
Packit ae235b
  g_assert (g_dbus_is_unique_name (name_owner));
Packit ae235b
  g_assert (property_names != NULL && g_strv_length (property_names) > 0);
Packit ae235b
  g_free (name_owner);
Packit ae235b
  g_strfreev (property_names);
Packit ae235b
  /* and finally check the 'y' property */
Packit ae235b
  variant = g_dbus_proxy_get_cached_property (p, "y");
Packit ae235b
  g_assert (variant != NULL);
Packit ae235b
  g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
Packit ae235b
  g_variant_unref (variant);
Packit ae235b
Packit ae235b
  g_object_unref (p2);
Packit ae235b
  g_object_unref (p);
Packit ae235b
  g_object_unref (ap2);
Packit ae235b
  g_object_unref (ap);
Packit ae235b
Packit ae235b
  g_object_unref (c);
Packit ae235b
Packit ae235b
  /* tear down bus */
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
  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_dbus_unset ();
Packit ae235b
Packit ae235b
  g_test_add_func ("/gdbus/proxy-well-known-name", test_proxy_well_known_name);
Packit ae235b
Packit ae235b
  ret = g_test_run();
Packit ae235b
  return ret;
Packit ae235b
}