LCOV - code coverage report
Current view: top level - glib/gio/tests - gdbus-proxy-unique-name.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 83 83 100.0 %
Date: 2024-04-23 05:16:05 Functions: 3 3 100.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       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                 :          1 : proxy_new_cb (GObject       *source_object,
      36                 :            :               GAsyncResult  *res,
      37                 :            :               gpointer       user_data)
      38                 :            : {
      39                 :          1 :   GDBusProxy **ret = user_data;
      40                 :            :   GError *error;
      41                 :            : 
      42                 :          1 :   error = NULL;
      43                 :          1 :   *ret = g_dbus_proxy_new_finish (res, &error);
      44                 :          1 :   g_assert_no_error (error);
      45                 :          1 :   g_assert_nonnull (ret);
      46                 :            : 
      47                 :          1 :   g_main_loop_quit (loop);
      48                 :          1 : }
      49                 :            : 
      50                 :            : static void
      51                 :          1 : 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                 :          1 :   session_bus_up ();
      65                 :            : 
      66                 :          1 :   error = NULL;
      67                 :          1 :   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
      68                 :          1 :   g_assert_no_error (error);
      69                 :          1 :   g_assert_nonnull (c);
      70                 :            : 
      71                 :            :   /* use a proxy to the well-known name to set things up */
      72                 :          1 :   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                 :          1 :   g_assert_no_error (error);
      81                 :            : 
      82                 :            :   /* this is safe; testserver will exit once the bus goes away */
      83                 :          1 :   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                 :          1 :   _g_assert_property_notify (wp, "g-name-owner");
      87                 :            : 
      88                 :            :   /* now get the unique name of testserver's connection */
      89                 :          1 :   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                 :          1 :   error = NULL;
      95                 :          1 :   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                 :          1 :   g_assert_no_error (error);
     104                 :          1 :   name_owner = g_dbus_proxy_get_name_owner (p);
     105                 :          1 :   property_names = g_dbus_proxy_get_cached_property_names (p);
     106                 :          1 :   g_assert_true (g_dbus_is_unique_name (name_owner));
     107                 :          1 :   g_assert_nonnull (property_names);
     108                 :          1 :   g_assert_cmpint (g_strv_length (property_names), >, 0);
     109                 :          1 :   g_free (name_owner);
     110                 :          1 :   g_strfreev (property_names);
     111                 :            : 
     112                 :            :   /* also for async: we should have a name owner and cached properties */
     113                 :          1 :   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                 :          1 :   g_main_loop_run (loop);
     123                 :          1 :   name_owner = g_dbus_proxy_get_name_owner (ap);
     124                 :          1 :   property_names = g_dbus_proxy_get_cached_property_names (ap);
     125                 :          1 :   g_assert_true (g_dbus_is_unique_name (name_owner));
     126                 :          1 :   g_assert_nonnull (property_names);
     127                 :          1 :   g_assert_cmpint (g_strv_length (property_names), >, 0);
     128                 :          1 :   g_free (name_owner);
     129                 :          1 :   g_strfreev (property_names);
     130                 :            : 
     131                 :            :   /* Check property value is the initial value */
     132                 :          1 :   variant = g_dbus_proxy_get_cached_property (p, "y");
     133                 :          1 :   g_assert_nonnull (variant);
     134                 :          1 :   g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
     135                 :          1 :   g_variant_unref (variant);
     136                 :          1 :   variant = g_dbus_proxy_get_cached_property (ap, "y");
     137                 :          1 :   g_assert_nonnull (variant);
     138                 :          1 :   g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
     139                 :          1 :   g_variant_unref (variant);
     140                 :            : 
     141                 :            :   /* Check that properties are updated on p */
     142                 :          1 :   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                 :          1 :   g_assert_no_error (error);
     152                 :          1 :   g_assert_nonnull (result);
     153                 :          1 :   g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
     154                 :          1 :   g_variant_unref (result);
     155                 :          1 :   _g_assert_signal_received (p, "g-properties-changed");
     156                 :          1 :   variant = g_dbus_proxy_get_cached_property (p, "y");
     157                 :          1 :   g_assert_nonnull (variant);
     158                 :          1 :   g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
     159                 :          1 :   g_variant_unref (variant);
     160                 :          1 :   variant = g_dbus_proxy_get_cached_property (ap, "y");
     161                 :          1 :   g_assert_nonnull (variant);
     162                 :          1 :   g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
     163                 :          1 :   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                 :          1 :   result = g_dbus_proxy_call_sync (p,
     169                 :            :                                    "Quit",
     170                 :            :                                    NULL,
     171                 :            :                                    G_DBUS_CALL_FLAGS_NONE,
     172                 :            :                                    -1,
     173                 :            :                                    NULL,
     174                 :            :                                    &error);
     175                 :          1 :   g_assert_no_error (error);
     176                 :          1 :   g_assert_nonnull (result);
     177                 :          1 :   g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
     178                 :          1 :   g_variant_unref (result);
     179                 :            :   /* and wait... */
     180                 :          1 :   _g_assert_property_notify (p, "g-name-owner");
     181                 :            :   /* now we shouldn't have a name owner nor any cached properties */
     182                 :          1 :   g_assert_cmpstr (g_dbus_proxy_get_name_owner (p), ==, NULL);
     183                 :          1 :   g_assert_null (g_dbus_proxy_get_cached_property_names (p));
     184                 :          1 :   g_assert_null (g_dbus_proxy_get_cached_property (p, "y"));
     185                 :            : 
     186                 :          1 :   g_object_unref (p);
     187                 :          1 :   g_object_unref (ap);
     188                 :            : 
     189                 :          1 :   g_object_unref (wp);
     190                 :          1 :   g_free (unique_name);
     191                 :            : 
     192                 :          1 :   g_object_unref (c);
     193                 :            : 
     194                 :            :   /* tear down bus */
     195                 :          1 :   session_bus_down ();
     196                 :          1 : }
     197                 :            : 
     198                 :            : /* ---------------------------------------------------------------------------------------------------- */
     199                 :            : 
     200                 :            : int
     201                 :          1 : main (int   argc,
     202                 :            :       char *argv[])
     203                 :            : {
     204                 :            :   gint ret;
     205                 :            : 
     206                 :          1 :   g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
     207                 :            : 
     208                 :            :   /* all the tests rely on a shared main loop */
     209                 :          1 :   loop = g_main_loop_new (NULL, FALSE);
     210                 :            : 
     211                 :          1 :   g_test_dbus_unset ();
     212                 :            : 
     213                 :          1 :   g_test_add_func ("/gdbus/proxy-unique-name", test_proxy_unique_name);
     214                 :            : 
     215                 :          1 :   ret = g_test_run();
     216                 :          1 :   return ret;
     217                 :            : }

Generated by: LCOV version 1.14