LCOV - code coverage report
Current view: top level - gio/tests - gdbus-connection-slow.c (source / functions) Coverage Total Hit
Test: unnamed Lines: 92.2 % 77 71
Test Date: 2025-11-11 05:25:45 Functions: 75.0 % 8 6
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: David Zeuthen <davidz@redhat.com>
      21                 :             :  */
      22                 :             : 
      23                 :             : #include <gio/gio.h>
      24                 :             : #include <unistd.h>
      25                 :             : #include <string.h>
      26                 :             : 
      27                 :             : #include <sys/types.h>
      28                 :             : 
      29                 :             : #include "gdbus-tests.h"
      30                 :             : 
      31                 :             : #define WAIT_FOR_FLUSH_MSEC 10000
      32                 :             : 
      33                 :             : /* all tests rely on a shared mainloop */
      34                 :             : static GMainLoop *loop = NULL;
      35                 :             : 
      36                 :             : /* ---------------------------------------------------------------------------------------------------- */
      37                 :             : 
      38                 :             : static void
      39                 :          50 : test_connection_flush_signal_handler (GDBusConnection  *connection,
      40                 :             :                                       const gchar      *sender_name,
      41                 :             :                                       const gchar      *object_path,
      42                 :             :                                       const gchar      *interface_name,
      43                 :             :                                       const gchar      *signal_name,
      44                 :             :                                       GVariant         *parameters,
      45                 :             :                                       gpointer         user_data)
      46                 :             : {
      47                 :          50 :   g_main_loop_quit (loop);
      48                 :          50 : }
      49                 :             : 
      50                 :             : static gboolean
      51                 :           0 : test_connection_flush_on_timeout (gpointer user_data)
      52                 :             : {
      53                 :           0 :   guint iteration = GPOINTER_TO_UINT (user_data);
      54                 :           0 :   g_printerr ("Timeout waiting %d msec on iteration %d\n", WAIT_FOR_FLUSH_MSEC, iteration);
      55                 :             :   g_assert_not_reached ();
      56                 :             :   return G_SOURCE_REMOVE;
      57                 :             : }
      58                 :             : 
      59                 :             : static void
      60                 :           1 : test_connection_flush (void)
      61                 :             : {
      62                 :             :   GDBusConnection *connection;
      63                 :             :   GError *error;
      64                 :             :   guint n;
      65                 :             :   guint signal_handler_id;
      66                 :             :   const gchar *flush_helper;
      67                 :             : 
      68                 :           1 :   session_bus_up ();
      69                 :             : 
      70                 :           1 :   error = NULL;
      71                 :           1 :   connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
      72                 :           1 :   g_assert_no_error (error);
      73                 :           1 :   g_assert (connection != NULL);
      74                 :             : 
      75                 :           1 :   signal_handler_id = g_dbus_connection_signal_subscribe (connection,
      76                 :             :                                                           NULL, /* sender */
      77                 :             :                                                           "org.gtk.GDBus.FlushInterface",
      78                 :             :                                                           "SomeSignal",
      79                 :             :                                                           "/org/gtk/GDBus/FlushObject",
      80                 :             :                                                           NULL,
      81                 :             :                                                           G_DBUS_SIGNAL_FLAGS_NONE,
      82                 :             :                                                           test_connection_flush_signal_handler,
      83                 :             :                                                           NULL,
      84                 :             :                                                           NULL);
      85                 :           1 :   g_assert_cmpint (signal_handler_id, !=, 0);
      86                 :             : 
      87                 :             :   /* We need to wait for the subscription to have actually taken effect
      88                 :             :    * before forking the subprocess, otherwise there is a race condition
      89                 :             :    * between the message bus adding the match rule and the subprocess
      90                 :             :    * sending the signal. If the message bus wins the race, then the test
      91                 :             :    * passes, but if the subprocess wins the race, it will be too late
      92                 :             :    * for this process to receive the signal because it already happened.
      93                 :             :    * The easiest way to avoid this race is to do a round-trip to the
      94                 :             :    * message bus and back. */
      95                 :           1 :   connection_wait_for_bus (connection);
      96                 :             : 
      97                 :           1 :   flush_helper = g_test_get_filename (G_TEST_BUILT, "gdbus-connection-flush-helper", NULL);
      98                 :          51 :   for (n = 0; n < 50; n++)
      99                 :             :     {
     100                 :             :       gboolean ret;
     101                 :             :       gint wait_status;
     102                 :             :       guint timeout_mainloop_id;
     103                 :          50 :       gchar *flush_helper_stdout = NULL;
     104                 :          50 :       gchar *flush_helper_stderr = NULL;
     105                 :             : 
     106                 :          50 :       error = NULL;
     107                 :          50 :       ret = g_spawn_command_line_sync (flush_helper,
     108                 :             :                                        &flush_helper_stdout,
     109                 :             :                                        &flush_helper_stderr,
     110                 :             :                                        &wait_status,
     111                 :         100 :                                        &error) &&
     112                 :          50 :             g_spawn_check_wait_status (wait_status, &error);
     113                 :          50 :       if (!ret)
     114                 :           0 :           g_test_message ("Child process ā€˜%s’ failed. stdout:\n%s\nstderr:\n%s",
     115                 :             :                           flush_helper, flush_helper_stdout, flush_helper_stderr);
     116                 :             : 
     117                 :          50 :       g_free (flush_helper_stdout);
     118                 :          50 :       g_free (flush_helper_stderr);
     119                 :             : 
     120                 :          50 :       g_assert_no_error (error);
     121                 :          50 :       g_assert_true (ret);
     122                 :             : 
     123                 :          50 :       timeout_mainloop_id = g_timeout_add (WAIT_FOR_FLUSH_MSEC, test_connection_flush_on_timeout, GUINT_TO_POINTER (n));
     124                 :          50 :       g_main_loop_run (loop);
     125                 :          50 :       g_source_remove (timeout_mainloop_id);
     126                 :             :     }
     127                 :             : 
     128                 :           1 :   g_dbus_connection_signal_unsubscribe (connection, g_steal_handle_id (&signal_handler_id));
     129                 :           1 :   g_object_unref (connection);
     130                 :             : 
     131                 :           1 :   session_bus_down ();
     132                 :           1 : }
     133                 :             : 
     134                 :             : /* ---------------------------------------------------------------------------------------------------- */
     135                 :             : 
     136                 :             : /* Message size > 20MiB ... should be enough to make sure the message
     137                 :             :  * is fragmented when shoved across any transport
     138                 :             :  */
     139                 :             : #define LARGE_MESSAGE_STRING_LENGTH (20*1024*1024)
     140                 :             : /* the test will fail if the service name has not appeared after this amount of seconds */
     141                 :             : #define LARGE_MESSAGE_TIMEOUT_SECONDS 10
     142                 :             : 
     143                 :             : static void
     144                 :           0 : large_message_timeout_cb (gpointer data)
     145                 :             : {
     146                 :             :   (void)data;
     147                 :             : 
     148                 :           0 :   g_error ("Error: timeout waiting for dbus name to appear");
     149                 :             : }
     150                 :             : 
     151                 :             : static void
     152                 :           1 : large_message_on_name_appeared (GDBusConnection *connection,
     153                 :             :                                 const gchar *name,
     154                 :             :                                 const gchar *name_owner,
     155                 :             :                                 gpointer user_data)
     156                 :             : {
     157                 :             :   GError *error;
     158                 :             :   gchar *request;
     159                 :             :   const gchar *reply;
     160                 :             :   GVariant *result;
     161                 :             :   guint n;
     162                 :             : 
     163                 :           1 :   g_assert (g_source_remove (GPOINTER_TO_UINT (user_data)));
     164                 :             : 
     165                 :           1 :   request = g_new (gchar, LARGE_MESSAGE_STRING_LENGTH + 1);
     166                 :    20971521 :   for (n = 0; n < LARGE_MESSAGE_STRING_LENGTH; n++)
     167                 :    20971520 :     request[n] = '0' + (n%10);
     168                 :           1 :   request[n] = '\0';
     169                 :             : 
     170                 :           1 :   error = NULL;
     171                 :           1 :   result = g_dbus_connection_call_sync (connection,
     172                 :             :                                         "com.example.TestService",      /* bus name */
     173                 :             :                                         "/com/example/TestObject",      /* object path */
     174                 :             :                                         "com.example.Frob",             /* interface name */
     175                 :             :                                         "HelloWorld",                   /* method name */
     176                 :             :                                         g_variant_new ("(s)", request), /* parameters */
     177                 :             :                                         G_VARIANT_TYPE ("(s)"),         /* return type */
     178                 :             :                                         G_DBUS_CALL_FLAGS_NONE,
     179                 :             :                                         G_MAXINT,
     180                 :             :                                         NULL,
     181                 :             :                                         &error);
     182                 :           1 :   g_assert_no_error (error);
     183                 :           1 :   g_assert (result != NULL);
     184                 :           1 :   g_variant_get (result, "(&s)", &reply);
     185                 :           1 :   g_assert_cmpint (strlen (reply), >, LARGE_MESSAGE_STRING_LENGTH);
     186                 :           1 :   g_assert (g_str_has_prefix (reply, "You greeted me with '01234567890123456789012"));
     187                 :           1 :   g_assert (g_str_has_suffix (reply, "6789'. Thanks!"));
     188                 :           1 :   g_variant_unref (result);
     189                 :             : 
     190                 :           1 :   g_free (request);
     191                 :             : 
     192                 :           1 :   g_main_loop_quit (loop);
     193                 :           1 : }
     194                 :             : 
     195                 :             : static void
     196                 :           1 : large_message_on_name_vanished (GDBusConnection *connection,
     197                 :             :                                 const gchar *name,
     198                 :             :                                 gpointer user_data)
     199                 :             : {
     200                 :           1 : }
     201                 :             : 
     202                 :             : static void
     203                 :           1 : test_connection_large_message (void)
     204                 :             : {
     205                 :             :   guint watcher_id;
     206                 :             :   guint timeout_id;
     207                 :             : 
     208                 :           1 :   session_bus_up ();
     209                 :             : 
     210                 :             :   /* this is safe; testserver will exit once the bus goes away */
     211                 :           1 :   g_assert (g_spawn_command_line_async (g_test_get_filename (G_TEST_BUILT, "gdbus-testserver", NULL), NULL));
     212                 :             : 
     213                 :           1 :   timeout_id = g_timeout_add_seconds_once (LARGE_MESSAGE_TIMEOUT_SECONDS,
     214                 :             :                                            large_message_timeout_cb,
     215                 :             :                                            NULL);
     216                 :             : 
     217                 :           1 :   watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION,
     218                 :             :                                  "com.example.TestService",
     219                 :             :                                  G_BUS_NAME_WATCHER_FLAGS_NONE,
     220                 :             :                                  large_message_on_name_appeared,
     221                 :             :                                  large_message_on_name_vanished,
     222                 :           1 :                                  GUINT_TO_POINTER (timeout_id),  /* user_data */
     223                 :             :                                  NULL); /* GDestroyNotify */
     224                 :           1 :   g_main_loop_run (loop);
     225                 :           1 :   g_bus_unwatch_name (watcher_id);
     226                 :             : 
     227                 :           1 :   session_bus_down ();
     228                 :           1 : }
     229                 :             : 
     230                 :             : /* ---------------------------------------------------------------------------------------------------- */
     231                 :             : 
     232                 :             : int
     233                 :           1 : main (int   argc,
     234                 :             :       char *argv[])
     235                 :             : {
     236                 :             :   gint ret;
     237                 :             : 
     238                 :           1 :   g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
     239                 :             : 
     240                 :             :   /* all the tests rely on a shared main loop */
     241                 :           1 :   loop = g_main_loop_new (NULL, FALSE);
     242                 :             : 
     243                 :           1 :   g_test_dbus_unset ();
     244                 :             : 
     245                 :           1 :   g_test_add_func ("/gdbus/connection/flush", test_connection_flush);
     246                 :           1 :   g_test_add_func ("/gdbus/connection/large_message", test_connection_large_message);
     247                 :             : 
     248                 :           1 :   ret = g_test_run();
     249                 :           1 :   g_main_loop_unref (loop);
     250                 :           1 :   return ret;
     251                 :             : }
        

Generated by: LCOV version 2.0-1