(root)/
glib-2.79.0/
gio/
tests/
gdbus-proxy-unique-name.c
       1  /* GLib testing framework examples and tests
       2   *
       3   * Copyright (C) 2008-2010 Red Hat, Inc.
       4   *
       5   * SPDX-License-Identifier: LGPL-2.1-or-later
       6   *
       7   * This library is free software; you can redistribute it and/or
       8   * modify it under the terms of the GNU Lesser General Public
       9   * License as published by the Free Software Foundation; either
      10   * version 2.1 of the License, or (at your option) any later version.
      11   *
      12   * This library is distributed in the hope that it will be useful,
      13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15   * Lesser General Public License for more details.
      16   *
      17   * You should have received a copy of the GNU Lesser General
      18   * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      19   *
      20   * Author: Cosimo Cecchi <cosimoc@gnome.org>
      21   */
      22  
      23  #include <gio/gio.h>
      24  #include <unistd.h>
      25  #include <string.h>
      26  
      27  #include "gdbus-tests.h"
      28  
      29  /* all tests rely on a shared mainloop */
      30  static GMainLoop *loop = NULL;
      31  
      32  /* ---------------------------------------------------------------------------------------------------- */
      33  
      34  static void
      35  proxy_new_cb (GObject       *source_object,
      36                GAsyncResult  *res,
      37                gpointer       user_data)
      38  {
      39    GDBusProxy **ret = user_data;
      40    GError *error;
      41  
      42    error = NULL;
      43    *ret = g_dbus_proxy_new_finish (res, &error);
      44    g_assert_no_error (error);
      45    g_assert_nonnull (ret);
      46  
      47    g_main_loop_quit (loop);
      48  }
      49  
      50  static void
      51  test_proxy_unique_name (void)
      52  {
      53    GDBusProxy *wp;
      54    GDBusProxy *p;
      55    GDBusProxy *ap;
      56    GDBusConnection *c;
      57    GError *error;
      58    gchar *name_owner;
      59    gchar **property_names;
      60    GVariant *variant;
      61    GVariant *result;
      62    char *unique_name;
      63  
      64    session_bus_up ();
      65  
      66    error = NULL;
      67    c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
      68    g_assert_no_error (error);
      69    g_assert_nonnull (c);
      70  
      71    /* use a proxy to the well-known name to set things up */
      72    wp = g_dbus_proxy_new_sync (c,
      73                                G_DBUS_PROXY_FLAGS_NONE,
      74                                NULL,                      /* GDBusInterfaceInfo* */
      75                                "com.example.TestService", /* name */
      76                                "/com/example/TestObject", /* object path */
      77                                "com.example.Frob",        /* interface name */
      78                                NULL,                      /* GCancellable */
      79                                &error);
      80    g_assert_no_error (error);
      81  
      82    /* this is safe; testserver will exit once the bus goes away */
      83    g_assert_true (g_spawn_command_line_async (g_test_get_filename (G_TEST_BUILT, "gdbus-testserver", NULL), NULL));
      84  
      85    /* check that we get the notify::g-name-owner signal */
      86    _g_assert_property_notify (wp, "g-name-owner");
      87  
      88    /* now get the unique name of testserver's connection */
      89    unique_name = g_dbus_proxy_get_name_owner (wp);
      90  
      91    /* if we create another a proxy with the service being available, check that
      92     * it has a name owner and properties
      93     */
      94    error = NULL;
      95    p = g_dbus_proxy_new_sync (c,
      96                                G_DBUS_PROXY_FLAGS_NONE,
      97                                NULL,                      /* GDBusInterfaceInfo* */
      98                                unique_name,               /* name */
      99                                "/com/example/TestObject", /* object path */
     100                                "com.example.Frob",        /* interface name */
     101                                NULL,                      /* GCancellable */
     102                                &error);
     103    g_assert_no_error (error);
     104    name_owner = g_dbus_proxy_get_name_owner (p);
     105    property_names = g_dbus_proxy_get_cached_property_names (p);
     106    g_assert_true (g_dbus_is_unique_name (name_owner));
     107    g_assert_nonnull (property_names);
     108    g_assert_cmpint (g_strv_length (property_names), >, 0);
     109    g_free (name_owner);
     110    g_strfreev (property_names);
     111  
     112    /* also for async: we should have a name owner and cached properties */
     113    g_dbus_proxy_new (c,
     114                      G_DBUS_PROXY_FLAGS_NONE,
     115                      NULL,                      /* GDBusInterfaceInfo* */
     116                      unique_name,               /* name */
     117                      "/com/example/TestObject", /* object path */
     118                      "com.example.Frob",        /* interface name */
     119                      NULL,                      /* GCancellable */
     120                      (GAsyncReadyCallback) proxy_new_cb,
     121                      &ap);
     122    g_main_loop_run (loop);
     123    name_owner = g_dbus_proxy_get_name_owner (ap);
     124    property_names = g_dbus_proxy_get_cached_property_names (ap);
     125    g_assert_true (g_dbus_is_unique_name (name_owner));
     126    g_assert_nonnull (property_names);
     127    g_assert_cmpint (g_strv_length (property_names), >, 0);
     128    g_free (name_owner);
     129    g_strfreev (property_names);
     130  
     131    /* Check property value is the initial value */
     132    variant = g_dbus_proxy_get_cached_property (p, "y");
     133    g_assert_nonnull (variant);
     134    g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
     135    g_variant_unref (variant);
     136    variant = g_dbus_proxy_get_cached_property (ap, "y");
     137    g_assert_nonnull (variant);
     138    g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
     139    g_variant_unref (variant);
     140  
     141    /* Check that properties are updated on p */
     142    result = g_dbus_proxy_call_sync (p,
     143                                     "FrobSetProperty",
     144                                     g_variant_new ("(sv)",
     145                                                    "y",
     146                                                    g_variant_new_byte (42)),
     147                                     G_DBUS_CALL_FLAGS_NONE,
     148                                     -1,
     149                                     NULL,
     150                                     &error);
     151    g_assert_no_error (error);
     152    g_assert_nonnull (result);
     153    g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
     154    g_variant_unref (result);
     155    _g_assert_signal_received (p, "g-properties-changed");
     156    variant = g_dbus_proxy_get_cached_property (p, "y");
     157    g_assert_nonnull (variant);
     158    g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
     159    g_variant_unref (variant);
     160    variant = g_dbus_proxy_get_cached_property (ap, "y");
     161    g_assert_nonnull (variant);
     162    g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
     163    g_variant_unref (variant);
     164  
     165    /* Nuke the service and check that we get the signal and then don't
     166     * have a name owner nor any cached properties
     167     */
     168    result = g_dbus_proxy_call_sync (p,
     169                                     "Quit",
     170                                     NULL,
     171                                     G_DBUS_CALL_FLAGS_NONE,
     172                                     -1,
     173                                     NULL,
     174                                     &error);
     175    g_assert_no_error (error);
     176    g_assert_nonnull (result);
     177    g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
     178    g_variant_unref (result);
     179    /* and wait... */
     180    _g_assert_property_notify (p, "g-name-owner");
     181    /* now we shouldn't have a name owner nor any cached properties */
     182    g_assert_cmpstr (g_dbus_proxy_get_name_owner (p), ==, NULL);
     183    g_assert_null (g_dbus_proxy_get_cached_property_names (p));
     184    g_assert_null (g_dbus_proxy_get_cached_property (p, "y"));
     185  
     186    g_object_unref (p);
     187    g_object_unref (ap);
     188  
     189    g_object_unref (wp);
     190    g_free (unique_name);
     191  
     192    g_object_unref (c);
     193  
     194    /* tear down bus */
     195    session_bus_down ();
     196  }
     197  
     198  /* ---------------------------------------------------------------------------------------------------- */
     199  
     200  int
     201  main (int   argc,
     202        char *argv[])
     203  {
     204    gint ret;
     205  
     206    g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
     207  
     208    /* all the tests rely on a shared main loop */
     209    loop = g_main_loop_new (NULL, FALSE);
     210  
     211    g_test_dbus_unset ();
     212  
     213    g_test_add_func ("/gdbus/proxy-unique-name", test_proxy_unique_name);
     214  
     215    ret = g_test_run();
     216    return ret;
     217  }