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

Generated by: LCOV version 1.14