LCOV - code coverage report
Current view: top level - glib/gio/tests - gdbus-exit-on-close.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 66 68 97.1 %
Date: 2024-04-23 05:16:05 Functions: 6 6 100.0 %
Branches: 13 16 81.2 %

           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 <stdlib.h>
      25                 :            : #include <unistd.h>
      26                 :            : #include <string.h>
      27                 :            : 
      28                 :            : #include "gdbus-tests.h"
      29                 :            : 
      30                 :            : /* all tests rely on a shared mainloop */
      31                 :            : static GMainLoop *loop = NULL;
      32                 :            : 
      33                 :            : /* ---------------------------------------------------------------------------------------------------- */
      34                 :            : 
      35                 :            : typedef struct {
      36                 :            :     const gchar *name;
      37                 :            :     const gchar *bug;
      38                 :            :     enum {
      39                 :            :         EXPLICITLY_FALSE = FALSE,
      40                 :            :         EXPLICITLY_TRUE = TRUE,
      41                 :            :         IMPLICITLY_TRUE
      42                 :            :     } exit_on_close;
      43                 :            :     enum {
      44                 :            :         LOCAL,
      45                 :            :         REMOTE
      46                 :            :     } who_closes;
      47                 :            : } TestData;
      48                 :            : 
      49                 :            : static const TestData cases[] = {
      50                 :            :       { "default",  NULL,     IMPLICITLY_TRUE,  REMOTE },
      51                 :            :       { "true",     NULL,     EXPLICITLY_TRUE,  REMOTE },
      52                 :            :       { "false",    NULL,     EXPLICITLY_FALSE, REMOTE },
      53                 :            :       { "we-close", "662100", EXPLICITLY_TRUE,  LOCAL  },
      54                 :            :       { NULL, NULL, 0, 0 }
      55                 :            : };
      56                 :            : 
      57                 :            : static gboolean
      58                 :          4 : quit_later_cb (gpointer data G_GNUC_UNUSED)
      59                 :            : {
      60                 :          4 :   g_main_loop_quit (loop);
      61                 :            : 
      62                 :          4 :   return G_SOURCE_REMOVE;
      63                 :            : }
      64                 :            : 
      65                 :            : static void
      66                 :          2 : closed_cb (GDBusConnection  *c G_GNUC_UNUSED,
      67                 :            :            gboolean          remote_peer_vanished,
      68                 :            :            GError           *error,
      69                 :            :            gpointer          test_data)
      70                 :            : {
      71                 :          2 :   const TestData *td = test_data;
      72                 :            : 
      73         [ +  + ]:          2 :   if (error == NULL)
      74                 :          1 :     g_debug ("closed (%d, no error)", remote_peer_vanished);
      75                 :            :   else
      76                 :          1 :     g_debug ("closed (%d, %s %d \"%s\")", remote_peer_vanished,
      77                 :            :              g_quark_to_string (error->domain), error->code, error->message);
      78                 :            : 
      79                 :          2 :   g_assert_cmpint (remote_peer_vanished, ==, (td->who_closes == REMOTE));
      80                 :          2 :   g_assert_cmpint ((error == NULL), ==, (td->who_closes == LOCAL));
      81                 :            : 
      82                 :            :   /* we delay this so that if exit-on-close was going to happen, it will
      83                 :            :    * win the race
      84                 :            :    */
      85                 :          2 :   g_timeout_add (50, quit_later_cb, NULL);
      86                 :          2 : }
      87                 :            : 
      88                 :            : static void
      89                 :          1 : close_async_cb (GObject *source G_GNUC_UNUSED,
      90                 :            :                 GAsyncResult *res G_GNUC_UNUSED,
      91                 :            :                 gpointer nil G_GNUC_UNUSED)
      92                 :            : {
      93                 :          1 :   GError *error = NULL;
      94                 :            : 
      95         [ +  - ]:          1 :   if (g_dbus_connection_close_finish (G_DBUS_CONNECTION (source),
      96                 :            :                                       res,
      97                 :            :                                       &error))
      98                 :            :     {
      99                 :          1 :       g_debug ("closed connection");
     100                 :            :     }
     101                 :            :   else
     102                 :            :     {
     103                 :          0 :       g_warning ("failed to close connection: %s (%s #%d)",
     104                 :            :                  error->message, g_quark_to_string (error->domain),
     105                 :            :                  error->code);
     106                 :            :     }
     107                 :          1 : }
     108                 :            : 
     109                 :            : static void
     110                 :          2 : test_exit_on_close_subprocess (gconstpointer test_data)
     111                 :            : {
     112                 :          2 :   const TestData *td = test_data;
     113                 :            :   GDBusConnection *c;
     114                 :            : 
     115                 :          2 :   loop = g_main_loop_new (NULL, FALSE);
     116                 :            : 
     117                 :          2 :   session_bus_up ();
     118                 :          2 :   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
     119                 :            : 
     120                 :          2 :   g_assert (c != NULL);
     121                 :            : 
     122                 :            :   /* the default is meant to be TRUE */
     123         [ +  - ]:          2 :   if (td->exit_on_close != IMPLICITLY_TRUE)
     124                 :          2 :     g_dbus_connection_set_exit_on_close (c, td->exit_on_close);
     125                 :            : 
     126                 :          2 :   g_assert_cmpint (g_dbus_connection_get_exit_on_close (c), ==,
     127                 :            :                    (td->exit_on_close != EXPLICITLY_FALSE));
     128                 :          2 :   g_assert (!g_dbus_connection_is_closed (c));
     129                 :            : 
     130                 :          2 :   g_timeout_add (50, quit_later_cb, NULL);
     131                 :          2 :   g_main_loop_run (loop);
     132                 :            : 
     133                 :          2 :   g_signal_connect (c, "closed", G_CALLBACK (closed_cb), (gpointer) td);
     134                 :            : 
     135         [ +  + ]:          2 :   if (td->who_closes == LOCAL)
     136                 :            :     {
     137                 :            :       GVariant *v;
     138                 :          1 :       GError *error = NULL;
     139                 :            : 
     140                 :          1 :       v = g_dbus_connection_call_sync (c, "org.freedesktop.DBus",
     141                 :            :                                        "/org/freedesktop/DBus",
     142                 :            :                                        "org.freedesktop.DBus",
     143                 :            :                                        "ListNames",
     144                 :            :                                        NULL,
     145                 :            :                                        G_VARIANT_TYPE ("(as)"),
     146                 :            :                                        G_DBUS_CALL_FLAGS_NONE,
     147                 :            :                                        -1,
     148                 :            :                                        NULL,
     149                 :            :                                        &error);
     150                 :          1 :       g_assert_no_error (error);
     151                 :          1 :       g_assert (v != NULL);
     152                 :          1 :       g_variant_unref (v);
     153                 :            : 
     154                 :          1 :       g_dbus_connection_close (c, NULL, close_async_cb, NULL);
     155                 :            :     }
     156                 :            :   else
     157                 :            :     {
     158                 :          1 :       session_bus_stop ();
     159                 :            :     }
     160                 :            : 
     161                 :          2 :   g_main_loop_run (loop);
     162                 :            :   /* this is only reached when we turn off exit-on-close */
     163                 :          2 :   g_main_loop_unref (loop);
     164                 :          2 :   g_object_unref (c);
     165                 :            : 
     166                 :          2 :   session_bus_down ();
     167                 :            : 
     168                 :          2 :   exit (0);
     169                 :            : }
     170                 :            : 
     171                 :            : static void
     172                 :          4 : test_exit_on_close (gconstpointer test_data)
     173                 :            : {
     174                 :          4 :   const TestData *td = test_data;
     175                 :            :   GTestSubprocessFlags flags;
     176                 :            :   char *child_name;
     177                 :            : 
     178                 :          4 :   g_test_dbus_unset ();
     179                 :            : 
     180         [ -  + ]:          4 :   if (g_test_verbose ())
     181                 :          0 :     flags = G_TEST_SUBPROCESS_INHERIT_STDOUT | G_TEST_SUBPROCESS_INHERIT_STDERR;
     182                 :            :   else
     183                 :          4 :     flags = 0;
     184                 :            : 
     185                 :          4 :   child_name = g_strdup_printf ("/gdbus/exit-on-close/%s/subprocess", td->name);
     186                 :          4 :   g_test_trap_subprocess (child_name, 0, flags);
     187                 :          4 :   g_free (child_name);
     188                 :            : 
     189         [ +  + ]:          4 :   if (td->exit_on_close == EXPLICITLY_FALSE ||
     190         [ +  + ]:          3 :       td->who_closes == LOCAL)
     191                 :          2 :     g_test_trap_assert_passed ();
     192                 :            :   else
     193                 :          2 :     g_test_trap_assert_failed();
     194                 :          4 : }
     195                 :            : 
     196                 :            : /* ---------------------------------------------------------------------------------------------------- */
     197                 :            : 
     198                 :            : int
     199                 :          3 : main (int   argc,
     200                 :            :       char *argv[])
     201                 :            : {
     202                 :            :   gint i;
     203                 :            : 
     204                 :          3 :   g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
     205                 :            : 
     206         [ +  + ]:         15 :   for (i = 0; cases[i].name != NULL; i++)
     207                 :            :     {
     208                 :            :       gchar *name;
     209                 :            : 
     210                 :         12 :       name = g_strdup_printf ("/gdbus/exit-on-close/%s", cases[i].name);
     211                 :         12 :       g_test_add_data_func (name, &cases[i], test_exit_on_close);
     212                 :         12 :       g_free (name);
     213                 :            : 
     214                 :         12 :       name = g_strdup_printf ("/gdbus/exit-on-close/%s/subprocess", cases[i].name);
     215                 :         12 :       g_test_add_data_func (name, &cases[i], test_exit_on_close_subprocess);
     216                 :         12 :       g_free (name);
     217                 :            :     }
     218                 :            : 
     219                 :          3 :   return g_test_run();
     220                 :            : }

Generated by: LCOV version 1.14