LCOV - code coverage report
Current view: top level - glib/gio/tests - gdbus-tests.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 66 79 83.5 %
Date: 2024-04-23 05:16:05 Functions: 8 11 72.7 %
Branches: 5 6 83.3 %

           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: David Zeuthen <davidz@redhat.com>
      21                 :            :  */
      22                 :            : 
      23                 :            : #include <gio/gio.h>
      24                 :            : #ifndef _MSC_VER
      25                 :            : #include <unistd.h>
      26                 :            : #endif
      27                 :            : 
      28                 :            : #include "gdbus-tests.h"
      29                 :            : 
      30                 :            : /* ---------------------------------------------------------------------------------------------------- */
      31                 :            : 
      32                 :            : typedef struct
      33                 :            : {
      34                 :            :   GMainLoop *loop;
      35                 :            :   gboolean   timed_out;
      36                 :            : } PropertyNotifyData;
      37                 :            : 
      38                 :            : static void
      39                 :         39 : on_property_notify (GObject    *object,
      40                 :            :                     GParamSpec *pspec,
      41                 :            :                     gpointer    user_data)
      42                 :            : {
      43                 :         39 :   PropertyNotifyData *data = user_data;
      44                 :         39 :   g_main_loop_quit (data->loop);
      45                 :         39 : }
      46                 :            : 
      47                 :            : static gboolean
      48                 :          0 : on_property_notify_timeout (gpointer user_data)
      49                 :            : {
      50                 :          0 :   PropertyNotifyData *data = user_data;
      51                 :          0 :   data->timed_out = TRUE;
      52                 :          0 :   g_main_loop_quit (data->loop);
      53                 :          0 :   return G_SOURCE_CONTINUE;
      54                 :            : }
      55                 :            : 
      56                 :            : gboolean
      57                 :         39 : _g_assert_property_notify_run (gpointer     object,
      58                 :            :                                const gchar *property_name)
      59                 :            : {
      60                 :            :   gchar *s;
      61                 :            :   gulong handler_id;
      62                 :            :   guint timeout_id;
      63                 :            :   PropertyNotifyData data;
      64                 :            : 
      65                 :         39 :   data.loop = g_main_loop_new (g_main_context_get_thread_default (), FALSE);
      66                 :         39 :   data.timed_out = FALSE;
      67                 :         39 :   s = g_strdup_printf ("notify::%s", property_name);
      68                 :         39 :   handler_id = g_signal_connect (object,
      69                 :            :                                  s,
      70                 :            :                                  G_CALLBACK (on_property_notify),
      71                 :            :                                  &data);
      72                 :         39 :   g_free (s);
      73                 :         39 :   timeout_id = g_timeout_add_seconds (30,
      74                 :            :                                       on_property_notify_timeout,
      75                 :            :                                       &data);
      76                 :         39 :   g_main_loop_run (data.loop);
      77                 :         39 :   g_signal_handler_disconnect (object, handler_id);
      78                 :         39 :   g_source_remove (timeout_id);
      79                 :         39 :   g_main_loop_unref (data.loop);
      80                 :            : 
      81                 :         39 :   return data.timed_out;
      82                 :            : }
      83                 :            : 
      84                 :            : static gboolean
      85                 :          0 : _give_up (gpointer data)
      86                 :            : {
      87                 :          0 :   g_error ("%s", (const gchar *) data);
      88                 :            :   g_return_val_if_reached (G_SOURCE_CONTINUE);
      89                 :            : }
      90                 :            : 
      91                 :            : typedef struct
      92                 :            : {
      93                 :            :   GMainContext *context;
      94                 :            :   gboolean name_appeared;
      95                 :            :   gboolean unwatch_complete;
      96                 :            : } WatchData;
      97                 :            : 
      98                 :            : static void
      99                 :          2 : name_appeared_cb (GDBusConnection *connection,
     100                 :            :                   const gchar     *name,
     101                 :            :                   const gchar     *name_owner,
     102                 :            :                   gpointer         user_data)
     103                 :            : {
     104                 :          2 :   WatchData *data = user_data;
     105                 :            : 
     106                 :          2 :   g_assert (name_owner != NULL);
     107                 :          2 :   data->name_appeared = TRUE;
     108                 :          2 :   g_main_context_wakeup (data->context);
     109                 :          2 : }
     110                 :            : 
     111                 :            : static void
     112                 :          2 : watch_free_cb (gpointer user_data)
     113                 :            : {
     114                 :          2 :   WatchData *data = user_data;
     115                 :            : 
     116                 :          2 :   data->unwatch_complete = TRUE;
     117                 :          2 :   g_main_context_wakeup (data->context);
     118                 :          2 : }
     119                 :            : 
     120                 :            : void
     121                 :          2 : ensure_gdbus_testserver_up (GDBusConnection *connection,
     122                 :            :                             GMainContext    *context)
     123                 :            : {
     124                 :          2 :   GSource *timeout_source = NULL;
     125                 :            :   guint watch_id;
     126                 :          2 :   WatchData data = { context, FALSE, FALSE };
     127                 :            : 
     128                 :          2 :   g_main_context_push_thread_default (context);
     129                 :            : 
     130                 :          2 :   watch_id = g_bus_watch_name_on_connection (connection,
     131                 :            :                                              "com.example.TestService",
     132                 :            :                                              G_BUS_NAME_WATCHER_FLAGS_NONE,
     133                 :            :                                              name_appeared_cb,
     134                 :            :                                              NULL,
     135                 :            :                                              &data,
     136                 :            :                                              watch_free_cb);
     137                 :            : 
     138                 :          2 :   timeout_source = g_timeout_source_new_seconds (60);
     139                 :          2 :   g_source_set_callback (timeout_source, _give_up,
     140                 :            :                          "waited more than ~ 60s for gdbus-testserver to take its bus name",
     141                 :            :                          NULL);
     142                 :          2 :   g_source_attach (timeout_source, context);
     143                 :            : 
     144         [ +  + ]:          6 :   while (!data.name_appeared)
     145                 :          4 :     g_main_context_iteration (context, TRUE);
     146                 :            : 
     147                 :          2 :   g_bus_unwatch_name (watch_id);
     148                 :            : 
     149         [ +  + ]:          4 :   while (!data.unwatch_complete)
     150                 :          2 :     g_main_context_iteration (context, TRUE);
     151                 :            : 
     152                 :          2 :   g_source_destroy (timeout_source);
     153                 :          2 :   g_source_unref (timeout_source);
     154                 :            : 
     155                 :          2 :   g_main_context_pop_thread_default (context);
     156                 :          2 : }
     157                 :            : 
     158                 :            : /* ---------------------------------------------------------------------------------------------------- */
     159                 :            : 
     160                 :            : typedef struct
     161                 :            : {
     162                 :            :   GMainLoop *loop;
     163                 :            :   gboolean   timed_out;
     164                 :            : } SignalReceivedData;
     165                 :            : 
     166                 :            : static void
     167                 :         29 : on_signal_received (gpointer user_data)
     168                 :            : {
     169                 :         29 :   SignalReceivedData *data = user_data;
     170                 :         29 :   g_main_loop_quit (data->loop);
     171                 :         29 : }
     172                 :            : 
     173                 :            : static gboolean
     174                 :          0 : on_signal_received_timeout (gpointer user_data)
     175                 :            : {
     176                 :          0 :   SignalReceivedData *data = user_data;
     177                 :          0 :   data->timed_out = TRUE;
     178                 :          0 :   g_main_loop_quit (data->loop);
     179                 :          0 :   return G_SOURCE_CONTINUE;
     180                 :            : }
     181                 :            : 
     182                 :            : gboolean
     183                 :         29 : _g_assert_signal_received_run (gpointer     object,
     184                 :            :                                const gchar *signal_name)
     185                 :            : {
     186                 :            :   gulong handler_id;
     187                 :            :   guint timeout_id;
     188                 :            :   SignalReceivedData data;
     189                 :            : 
     190                 :         29 :   data.loop = g_main_loop_new (g_main_context_get_thread_default (), FALSE);
     191                 :         29 :   data.timed_out = FALSE;
     192                 :         29 :   handler_id = g_signal_connect_swapped (object,
     193                 :            :                                          signal_name,
     194                 :            :                                          G_CALLBACK (on_signal_received),
     195                 :            :                                          &data);
     196                 :         29 :   timeout_id = g_timeout_add_seconds (30,
     197                 :            :                                       on_signal_received_timeout,
     198                 :            :                                       &data);
     199                 :         29 :   g_main_loop_run (data.loop);
     200                 :         29 :   g_signal_handler_disconnect (object, handler_id);
     201                 :         29 :   g_source_remove (timeout_id);
     202                 :         29 :   g_main_loop_unref (data.loop);
     203                 :            : 
     204                 :         29 :   return data.timed_out;
     205                 :            : }
     206                 :            : 
     207                 :            : /* ---------------------------------------------------------------------------------------------------- */
     208                 :            : 
     209                 :            : GDBusConnection *
     210                 :          7 : _g_bus_get_priv (GBusType            bus_type,
     211                 :            :                  GCancellable       *cancellable,
     212                 :            :                  GError            **error)
     213                 :            : {
     214                 :            :   gchar *address;
     215                 :            :   GDBusConnection *ret;
     216                 :            : 
     217                 :          7 :   ret = NULL;
     218                 :            : 
     219                 :          7 :   address = g_dbus_address_get_for_bus_sync (bus_type, cancellable, error);
     220         [ -  + ]:          7 :   if (address == NULL)
     221                 :          0 :     goto out;
     222                 :            : 
     223                 :          7 :   ret = g_dbus_connection_new_for_address_sync (address,
     224                 :            :                                                 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
     225                 :            :                                                 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
     226                 :            :                                                 NULL, /* GDBusAuthObserver */
     227                 :            :                                                 cancellable,
     228                 :            :                                                 error);
     229                 :          7 :   g_free (address);
     230                 :            : 
     231                 :          7 :  out:
     232                 :          7 :   return ret;
     233                 :            : }
     234                 :            : 
     235                 :            : /* ---------------------------------------------------------------------------------------------------- */

Generated by: LCOV version 1.14