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  tests_setup (SetupData *setup_data,
      40               gconstpointer data)
      41  {
      42    setup_data->old_path = g_strdup (g_getenv ("PATH"));
      43    setup_data->old_snap = g_strdup (g_getenv ("SNAP"));
      44  
      45    setup_data->bin_path = g_get_user_runtime_dir ();
      46    setup_data->snap_path = g_getenv ("G_TEST_TMPDIR");
      47  
      48    g_assert_nonnull (setup_data->bin_path);
      49    g_assert_nonnull (setup_data->snap_path);
      50  
      51    g_setenv ("PATH", setup_data->bin_path, TRUE);
      52    g_setenv ("SNAP", setup_data->snap_path, TRUE);
      53  }
      54  
      55  static void
      56  tests_teardown (SetupData *setup_data,
      57                  gconstpointer data)
      58  {
      59    if (setup_data->old_path)
      60      g_setenv ("PATH", setup_data->old_path, TRUE);
      61    else
      62      g_unsetenv ("PATH");
      63  
      64    if (setup_data->old_snap)
      65      g_setenv ("SNAP", setup_data->old_snap, TRUE);
      66    else
      67      g_unsetenv ("SNAP");
      68  
      69    cleanup_snapfiles (setup_data->snap_path);
      70    cleanup_snapfiles (setup_data->bin_path);
      71  
      72    g_clear_pointer (&setup_data->old_path, g_free);
      73    g_clear_pointer (&setup_data->old_snap, g_free);
      74  }
      75  
      76  static void
      77  test_portal_support_snap_no_snapctl (SetupData *setup,
      78                                       gconstpointer data)
      79  {
      80    g_assert_false (glib_should_use_portal ());
      81    g_assert_true (glib_network_available_in_sandbox ());
      82    g_assert_true (glib_has_dconf_access_in_sandbox ());
      83  }
      84  
      85  static void
      86  test_portal_support_snap_none (SetupData *setup,
      87                                 gconstpointer data)
      88  {
      89    create_fake_snap_yaml (setup->snap_path, TRUE);
      90    create_fake_snapctl (setup->bin_path, NULL);
      91  
      92    g_assert_false (glib_should_use_portal ());
      93    g_assert_true (glib_network_available_in_sandbox ());
      94    g_assert_true (glib_has_dconf_access_in_sandbox ());
      95  }
      96  
      97  static void
      98  test_portal_support_snap_all (SetupData *setup,
      99                                gconstpointer data)
     100  {
     101    create_fake_snap_yaml (setup->snap_path, TRUE);
     102    create_fake_snapctl (setup->bin_path, "desktop|network-status|gsettings");
     103  
     104    g_assert_false (glib_should_use_portal ());
     105    g_assert_true (glib_network_available_in_sandbox ());
     106    g_assert_true (glib_has_dconf_access_in_sandbox ());
     107  }
     108  
     109  int
     110  main (int argc, char **argv)
     111  {
     112    g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
     113  
     114    g_test_add ("/portal-support/snap-classic/no-snapctl", SetupData, NULL,
     115      tests_setup, test_portal_support_snap_no_snapctl, tests_teardown);
     116    g_test_add ("/portal-support/snap-classic/none", SetupData, NULL,
     117      tests_setup, test_portal_support_snap_none, tests_teardown);
     118    g_test_add ("/portal-support/snap-classic/all", SetupData, NULL,
     119      tests_setup, test_portal_support_snap_all, tests_teardown);
     120  
     121    return g_test_run ();
     122  }