LCOV - code coverage report
Current view: top level - gio/tests - portal-support-snap-classic.c (source / functions) Coverage Total Hit
Test: unnamed Lines: 95.7 % 47 45
Test Date: 2024-11-26 05:23:01 Functions: 100.0 % 6 6
Branches: - 0 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                 :             : 
      29                 :             : typedef struct
      30                 :             : {
      31                 :             :   char *old_path;
      32                 :             :   char *old_snap;
      33                 :             : 
      34                 :             :   const char *bin_path;
      35                 :             :   const char *snap_path;
      36                 :             : } SetupData;
      37                 :             : 
      38                 :             : static void
      39                 :           3 : tests_setup (SetupData *setup_data,
      40                 :             :              gconstpointer data)
      41                 :             : {
      42                 :           3 :   setup_data->old_path = g_strdup (g_getenv ("PATH"));
      43                 :           3 :   setup_data->old_snap = g_strdup (g_getenv ("SNAP"));
      44                 :             : 
      45                 :           3 :   setup_data->bin_path = g_get_user_runtime_dir ();
      46                 :           3 :   setup_data->snap_path = g_getenv ("G_TEST_TMPDIR");
      47                 :             : 
      48                 :           3 :   g_assert_nonnull (setup_data->bin_path);
      49                 :           3 :   g_assert_nonnull (setup_data->snap_path);
      50                 :             : 
      51                 :           3 :   g_setenv ("PATH", setup_data->bin_path, TRUE);
      52                 :           3 :   g_setenv ("SNAP", setup_data->snap_path, TRUE);
      53                 :           3 : }
      54                 :             : 
      55                 :             : static void
      56                 :           3 : tests_teardown (SetupData *setup_data,
      57                 :             :                 gconstpointer data)
      58                 :             : {
      59                 :           3 :   if (setup_data->old_path)
      60                 :           3 :     g_setenv ("PATH", setup_data->old_path, TRUE);
      61                 :             :   else
      62                 :           0 :     g_unsetenv ("PATH");
      63                 :             : 
      64                 :           3 :   if (setup_data->old_snap)
      65                 :           0 :     g_setenv ("SNAP", setup_data->old_snap, TRUE);
      66                 :             :   else
      67                 :           3 :     g_unsetenv ("SNAP");
      68                 :             : 
      69                 :           3 :   cleanup_snapfiles (setup_data->snap_path);
      70                 :           3 :   cleanup_snapfiles (setup_data->bin_path);
      71                 :             : 
      72                 :           3 :   g_clear_pointer (&setup_data->old_path, g_free);
      73                 :           3 :   g_clear_pointer (&setup_data->old_snap, g_free);
      74                 :           3 : }
      75                 :             : 
      76                 :             : static void
      77                 :           1 : test_portal_support_snap_no_snapctl (SetupData *setup,
      78                 :             :                                      gconstpointer data)
      79                 :             : {
      80                 :           1 :   g_assert_false (glib_should_use_portal ());
      81                 :           1 :   g_assert_true (glib_network_available_in_sandbox ());
      82                 :           1 :   g_assert_true (glib_has_dconf_access_in_sandbox ());
      83                 :           1 : }
      84                 :             : 
      85                 :             : static void
      86                 :           1 : test_portal_support_snap_none (SetupData *setup,
      87                 :             :                                gconstpointer data)
      88                 :             : {
      89                 :           1 :   create_fake_snap_yaml (setup->snap_path, TRUE);
      90                 :           1 :   create_fake_snapctl (setup->bin_path, NULL);
      91                 :             : 
      92                 :           1 :   g_assert_false (glib_should_use_portal ());
      93                 :           1 :   g_assert_true (glib_network_available_in_sandbox ());
      94                 :           1 :   g_assert_true (glib_has_dconf_access_in_sandbox ());
      95                 :           1 : }
      96                 :             : 
      97                 :             : static void
      98                 :           1 : test_portal_support_snap_all (SetupData *setup,
      99                 :             :                               gconstpointer data)
     100                 :             : {
     101                 :           1 :   create_fake_snap_yaml (setup->snap_path, TRUE);
     102                 :           1 :   create_fake_snapctl (setup->bin_path, "desktop|network-status|gsettings");
     103                 :             : 
     104                 :           1 :   g_assert_false (glib_should_use_portal ());
     105                 :           1 :   g_assert_true (glib_network_available_in_sandbox ());
     106                 :           1 :   g_assert_true (glib_has_dconf_access_in_sandbox ());
     107                 :           1 : }
     108                 :             : 
     109                 :             : int
     110                 :           1 : main (int argc, char **argv)
     111                 :             : {
     112                 :           1 :   g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
     113                 :             : 
     114                 :           1 :   g_test_add ("/portal-support/snap-classic/no-snapctl", SetupData, NULL,
     115                 :             :     tests_setup, test_portal_support_snap_no_snapctl, tests_teardown);
     116                 :           1 :   g_test_add ("/portal-support/snap-classic/none", SetupData, NULL,
     117                 :             :     tests_setup, test_portal_support_snap_none, tests_teardown);
     118                 :           1 :   g_test_add ("/portal-support/snap-classic/all", SetupData, NULL,
     119                 :             :     tests_setup, test_portal_support_snap_all, tests_teardown);
     120                 :             : 
     121                 :           1 :   return g_test_run ();
     122                 :             : }
        

Generated by: LCOV version 2.0-1