LCOV - code coverage report
Current view: top level - glib/gio/tests - portal-support-snap.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 102 104 98.1 %
Date: 2024-04-23 05:16:05 Functions: 10 10 100.0 %
Branches: 2 4 50.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * GIO - GLib Input, Output and Streaming Library
       3                 :            :  *
       4                 :            :  * Copyright (C) 2022 Canonical Ltd.
       5                 :            :  *
       6                 :            :  * SPDX-License-Identifier: LGPL-2.1-or-later
       7                 :            :  *
       8                 :            :  * This library is free software; you can redistribute it and/or
       9                 :            :  * modify it under the terms of the GNU Lesser General Public
      10                 :            :  * License as published by the Free Software Foundation; either
      11                 :            :  * version 2.1 of the License, or (at your option) any later version.
      12                 :            :  *
      13                 :            :  * This library is distributed in the hope that it will be useful,
      14                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      15                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      16                 :            :  * Lesser General Public License for more details.
      17                 :            :  *
      18                 :            :  * You should have received a copy of the GNU Lesser General
      19                 :            :  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      20                 :            :  *
      21                 :            :  * Author: Marco Trevisan <marco.trevisan@canonical.com>
      22                 :            :  */
      23                 :            : 
      24                 :            : #include "portal-support-utils.h"
      25                 :            : 
      26                 :            : #include "../gportalsupport.h"
      27                 :            : #include <gio/gio.h>
      28                 :            : #include <glib/gstdio.h>
      29                 :            : 
      30                 :            : typedef struct
      31                 :            : {
      32                 :            :   char *old_path;
      33                 :            :   char *old_snap;
      34                 :            : 
      35                 :            :   const char *bin_path;
      36                 :            :   const char *snap_path;
      37                 :            : } SetupData;
      38                 :            : 
      39                 :            : static void
      40                 :          7 : tests_setup (SetupData *setup_data,
      41                 :            :              gconstpointer data)
      42                 :            : {
      43                 :          7 :   setup_data->old_path = g_strdup (g_getenv ("PATH"));
      44                 :          7 :   setup_data->old_snap = g_strdup (g_getenv ("SNAP"));
      45                 :            : 
      46                 :          7 :   setup_data->bin_path = g_get_user_runtime_dir ();
      47                 :          7 :   setup_data->snap_path = g_getenv ("G_TEST_TMPDIR");
      48                 :            : 
      49                 :          7 :   g_assert_nonnull (setup_data->bin_path);
      50                 :          7 :   g_assert_nonnull (setup_data->snap_path);
      51                 :            : 
      52                 :          7 :   g_setenv ("PATH", setup_data->bin_path, TRUE);
      53                 :          7 :   g_setenv ("SNAP", setup_data->snap_path, TRUE);
      54                 :          7 : }
      55                 :            : 
      56                 :            : static void
      57                 :          7 : tests_teardown (SetupData *setup_data,
      58                 :            :                 gconstpointer data)
      59                 :            : {
      60         [ +  - ]:          7 :   if (setup_data->old_path)
      61                 :          7 :     g_setenv ("PATH", setup_data->old_path, TRUE);
      62                 :            :   else
      63                 :          0 :     g_unsetenv ("PATH");
      64                 :            : 
      65         [ -  + ]:          7 :   if (setup_data->old_snap)
      66                 :          0 :     g_setenv ("SNAP", setup_data->old_snap, TRUE);
      67                 :            :   else
      68                 :          7 :     g_unsetenv ("SNAP");
      69                 :            : 
      70                 :          7 :   cleanup_snapfiles (setup_data->snap_path);
      71                 :          7 :   cleanup_snapfiles (setup_data->bin_path);
      72                 :            : 
      73                 :          7 :   g_clear_pointer (&setup_data->old_path, g_free);
      74                 :          7 :   g_clear_pointer (&setup_data->old_snap, g_free);
      75                 :          7 : }
      76                 :            : 
      77                 :            : static void
      78                 :          1 : test_portal_support_snap_no_snapctl (SetupData *setup,
      79                 :            :                                      gconstpointer data)
      80                 :            : {
      81                 :          1 :   create_fake_snap_yaml (setup->snap_path, FALSE);
      82                 :            : 
      83                 :          1 :   g_assert_false (glib_should_use_portal ());
      84                 :          1 :   g_assert_false (glib_network_available_in_sandbox ());
      85                 :          1 :   g_assert_false (glib_has_dconf_access_in_sandbox ());
      86                 :          1 : }
      87                 :            : 
      88                 :            : static void
      89                 :          1 : test_portal_support_snap_none (SetupData *setup,
      90                 :            :                                gconstpointer data)
      91                 :            : {
      92                 :          1 :   create_fake_snap_yaml (setup->snap_path, FALSE);
      93                 :          1 :   create_fake_snapctl (setup->bin_path, NULL);
      94                 :            : 
      95                 :          1 :   g_assert_false (glib_should_use_portal ());
      96                 :          1 :   g_assert_false (glib_network_available_in_sandbox ());
      97                 :          1 :   g_assert_false (glib_has_dconf_access_in_sandbox ());
      98                 :          1 : }
      99                 :            : 
     100                 :            : static void
     101                 :          1 : test_portal_support_snap_all (SetupData *setup,
     102                 :            :                               gconstpointer data)
     103                 :            : {
     104                 :          1 :   create_fake_snap_yaml (setup->snap_path, FALSE);
     105                 :          1 :   create_fake_snapctl (setup->bin_path, "desktop|network-status|gsettings");
     106                 :            : 
     107                 :          1 :   g_assert_true (glib_should_use_portal ());
     108                 :          1 :   g_assert_true (glib_network_available_in_sandbox ());
     109                 :          1 :   g_assert_true (glib_has_dconf_access_in_sandbox ());
     110                 :          1 : }
     111                 :            : 
     112                 :            : static void
     113                 :          1 : test_portal_support_snap_desktop_only (SetupData *setup,
     114                 :            :                                        gconstpointer data)
     115                 :            : {
     116                 :          1 :   create_fake_snap_yaml (setup->snap_path, FALSE);
     117                 :          1 :   create_fake_snapctl (setup->bin_path, "desktop");
     118                 :            : 
     119                 :          1 :   g_assert_true (glib_should_use_portal ());
     120                 :          1 :   g_assert_true (glib_network_available_in_sandbox ());
     121                 :          1 :   g_assert_false (glib_has_dconf_access_in_sandbox ());
     122                 :          1 : }
     123                 :            : 
     124                 :            : static void
     125                 :          1 : test_portal_support_snap_network_only (SetupData *setup,
     126                 :            :                                        gconstpointer data)
     127                 :            : {
     128                 :          1 :   create_fake_snap_yaml (setup->snap_path, FALSE);
     129                 :          1 :   create_fake_snapctl (setup->bin_path, "network-status");
     130                 :            : 
     131                 :          1 :   g_assert_false (glib_should_use_portal ());
     132                 :          1 :   g_assert_true (glib_network_available_in_sandbox ());
     133                 :          1 :   g_assert_false (glib_has_dconf_access_in_sandbox ());
     134                 :          1 : }
     135                 :            : 
     136                 :            : static void
     137                 :          1 : test_portal_support_snap_gsettings_only (SetupData *setup,
     138                 :            :                                          gconstpointer data)
     139                 :            : {
     140                 :          1 :   create_fake_snap_yaml (setup->snap_path, FALSE);
     141                 :          1 :   create_fake_snapctl (setup->bin_path, "gsettings");
     142                 :            : 
     143                 :          1 :   g_assert_false (glib_should_use_portal ());
     144                 :          1 :   g_assert_false (glib_network_available_in_sandbox ());
     145                 :          1 :   g_assert_true (glib_has_dconf_access_in_sandbox ());
     146                 :          1 : }
     147                 :            : 
     148                 :            : static void
     149                 :          1 : test_portal_support_snap_updates_dynamically (SetupData *setup,
     150                 :            :                                               gconstpointer data)
     151                 :            : {
     152                 :          1 :   create_fake_snap_yaml (setup->snap_path, FALSE);
     153                 :          1 :   create_fake_snapctl (setup->bin_path, NULL);
     154                 :            : 
     155                 :          1 :   g_assert_false (glib_should_use_portal ());
     156                 :          1 :   g_assert_false (glib_network_available_in_sandbox ());
     157                 :          1 :   g_assert_false (glib_has_dconf_access_in_sandbox ());
     158                 :            : 
     159                 :          1 :   create_fake_snapctl (setup->bin_path, "desktop");
     160                 :          1 :   g_assert_true (glib_should_use_portal ());
     161                 :          1 :   g_assert_true (glib_network_available_in_sandbox ());
     162                 :          1 :   g_assert_false (glib_has_dconf_access_in_sandbox ());
     163                 :            : 
     164                 :          1 :   create_fake_snapctl (setup->bin_path, "network-status|gsettings");
     165                 :          1 :   g_assert_false (glib_should_use_portal ());
     166                 :          1 :   g_assert_true (glib_network_available_in_sandbox ());
     167                 :          1 :   g_assert_true (glib_has_dconf_access_in_sandbox ());
     168                 :            : 
     169                 :          1 :   create_fake_snapctl (setup->bin_path, "desktop|network-status|gsettings");
     170                 :          1 :   g_assert_true (glib_should_use_portal ());
     171                 :          1 :   g_assert_true (glib_network_available_in_sandbox ());
     172                 :          1 :   g_assert_true (glib_has_dconf_access_in_sandbox ());
     173                 :            : 
     174                 :          1 :   create_fake_snapctl (setup->bin_path, "desktop|gsettings");
     175                 :          1 :   g_assert_true (glib_should_use_portal ());
     176                 :          1 :   g_assert_true (glib_network_available_in_sandbox ());
     177                 :          1 :   g_assert_true (glib_has_dconf_access_in_sandbox ());
     178                 :            : 
     179                 :          1 :   create_fake_snapctl (setup->bin_path, "gsettings");
     180                 :          1 :   g_assert_false (glib_should_use_portal ());
     181                 :          1 :   g_assert_false (glib_network_available_in_sandbox ());
     182                 :          1 :   g_assert_true (glib_has_dconf_access_in_sandbox ());
     183                 :            : 
     184                 :          1 :   create_fake_snapctl (setup->bin_path, NULL);
     185                 :          1 :   g_assert_false (glib_should_use_portal ());
     186                 :          1 :   g_assert_false (glib_network_available_in_sandbox ());
     187                 :          1 :   g_assert_false (glib_has_dconf_access_in_sandbox ());
     188                 :          1 : }
     189                 :            : 
     190                 :            : int
     191                 :          1 : main (int argc, char **argv)
     192                 :            : {
     193                 :          1 :   g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
     194                 :            : 
     195                 :          1 :   g_test_add ("/portal-support/snap/no-snapctl", SetupData, NULL,
     196                 :            :     tests_setup, test_portal_support_snap_no_snapctl, tests_teardown);
     197                 :          1 :   g_test_add ("/portal-support/snap/none", SetupData, NULL,
     198                 :            :     tests_setup, test_portal_support_snap_none, tests_teardown);
     199                 :          1 :   g_test_add ("/portal-support/snap/all", SetupData, NULL,
     200                 :            :     tests_setup, test_portal_support_snap_all, tests_teardown);
     201                 :          1 :   g_test_add ("/portal-support/snap/desktop-only", SetupData, NULL,
     202                 :            :     tests_setup, test_portal_support_snap_desktop_only, tests_teardown);
     203                 :          1 :   g_test_add ("/portal-support/snap/network-only", SetupData, NULL,
     204                 :            :     tests_setup, test_portal_support_snap_network_only, tests_teardown);
     205                 :          1 :   g_test_add ("/portal-support/snap/gsettings-only", SetupData, NULL,
     206                 :            :     tests_setup, test_portal_support_snap_gsettings_only, tests_teardown);
     207                 :          1 :   g_test_add ("/portal-support/snap/updates-dynamically", SetupData, NULL,
     208                 :            :     tests_setup, test_portal_support_snap_updates_dynamically, tests_teardown);
     209                 :            : 
     210                 :          1 :   return g_test_run ();
     211                 :            : }

Generated by: LCOV version 1.14