| 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 <[email protected]> |
| 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 |
tests_setup (SetupData *setup_data, |
| 41 |
gconstpointer data) |
| 42 |
{ |
| 43 |
setup_data->old_path = g_strdup (g_getenv ("PATH")); |
| 44 |
setup_data->old_snap = g_strdup (g_getenv ("SNAP")); |
| 45 |
|
| 46 |
setup_data->bin_path = g_get_user_runtime_dir (); |
| 47 |
setup_data->snap_path = g_getenv ("G_TEST_TMPDIR"); |
| 48 |
|
| 49 |
g_assert_nonnull (setup_data->bin_path); |
| 50 |
g_assert_nonnull (setup_data->snap_path); |
| 51 |
|
| 52 |
g_setenv ("PATH", setup_data->bin_path, TRUE); |
| 53 |
g_setenv ("SNAP", setup_data->snap_path, TRUE); |
| 54 |
} |
| 55 |
|
| 56 |
static void |
| 57 |
tests_teardown (SetupData *setup_data, |
| 58 |
gconstpointer data) |
| 59 |
{ |
| 60 |
if (setup_data->old_path) |
| 61 |
g_setenv ("PATH", setup_data->old_path, TRUE); |
| 62 |
else |
| 63 |
g_unsetenv ("PATH"); |
| 64 |
|
| 65 |
if (setup_data->old_snap) |
| 66 |
g_setenv ("SNAP", setup_data->old_snap, TRUE); |
| 67 |
else |
| 68 |
g_unsetenv ("SNAP"); |
| 69 |
|
| 70 |
cleanup_snapfiles (setup_data->snap_path); |
| 71 |
cleanup_snapfiles (setup_data->bin_path); |
| 72 |
|
| 73 |
g_clear_pointer (&setup_data->old_path, g_free); |
| 74 |
g_clear_pointer (&setup_data->old_snap, g_free); |
| 75 |
} |
| 76 |
|
| 77 |
static void |
| 78 |
test_portal_support_snap_no_snapctl (SetupData *setup, |
| 79 |
gconstpointer data) |
| 80 |
{ |
| 81 |
create_fake_snap_yaml (setup->snap_path, FALSE); |
| 82 |
|
| 83 |
g_assert_false (glib_should_use_portal ()); |
| 84 |
g_assert_false (glib_network_available_in_sandbox ()); |
| 85 |
g_assert_false (glib_has_dconf_access_in_sandbox ()); |
| 86 |
} |
| 87 |
|
| 88 |
static void |
| 89 |
test_portal_support_snap_none (SetupData *setup, |
| 90 |
gconstpointer data) |
| 91 |
{ |
| 92 |
create_fake_snap_yaml (setup->snap_path, FALSE); |
| 93 |
create_fake_snapctl (setup->bin_path, NULL); |
| 94 |
|
| 95 |
g_assert_false (glib_should_use_portal ()); |
| 96 |
g_assert_false (glib_network_available_in_sandbox ()); |
| 97 |
g_assert_false (glib_has_dconf_access_in_sandbox ()); |
| 98 |
} |
| 99 |
|
| 100 |
static void |
| 101 |
test_portal_support_snap_all (SetupData *setup, |
| 102 |
gconstpointer data) |
| 103 |
{ |
| 104 |
create_fake_snap_yaml (setup->snap_path, FALSE); |
| 105 |
create_fake_snapctl (setup->bin_path, "desktop|network-status|gsettings"); |
| 106 |
|
| 107 |
g_assert_true (glib_should_use_portal ()); |
| 108 |
g_assert_true (glib_network_available_in_sandbox ()); |
| 109 |
g_assert_true (glib_has_dconf_access_in_sandbox ()); |
| 110 |
} |
| 111 |
|
| 112 |
static void |
| 113 |
test_portal_support_snap_desktop_only (SetupData *setup, |
| 114 |
gconstpointer data) |
| 115 |
{ |
| 116 |
create_fake_snap_yaml (setup->snap_path, FALSE); |
| 117 |
create_fake_snapctl (setup->bin_path, "desktop"); |
| 118 |
|
| 119 |
g_assert_true (glib_should_use_portal ()); |
| 120 |
g_assert_true (glib_network_available_in_sandbox ()); |
| 121 |
g_assert_false (glib_has_dconf_access_in_sandbox ()); |
| 122 |
} |
| 123 |
|
| 124 |
static void |
| 125 |
test_portal_support_snap_network_only (SetupData *setup, |
| 126 |
gconstpointer data) |
| 127 |
{ |
| 128 |
create_fake_snap_yaml (setup->snap_path, FALSE); |
| 129 |
create_fake_snapctl (setup->bin_path, "network-status"); |
| 130 |
|
| 131 |
g_assert_false (glib_should_use_portal ()); |
| 132 |
g_assert_true (glib_network_available_in_sandbox ()); |
| 133 |
g_assert_false (glib_has_dconf_access_in_sandbox ()); |
| 134 |
} |
| 135 |
|
| 136 |
static void |
| 137 |
test_portal_support_snap_gsettings_only (SetupData *setup, |
| 138 |
gconstpointer data) |
| 139 |
{ |
| 140 |
create_fake_snap_yaml (setup->snap_path, FALSE); |
| 141 |
create_fake_snapctl (setup->bin_path, "gsettings"); |
| 142 |
|
| 143 |
g_assert_false (glib_should_use_portal ()); |
| 144 |
g_assert_false (glib_network_available_in_sandbox ()); |
| 145 |
g_assert_true (glib_has_dconf_access_in_sandbox ()); |
| 146 |
} |
| 147 |
|
| 148 |
static void |
| 149 |
test_portal_support_snap_updates_dynamically (SetupData *setup, |
| 150 |
gconstpointer data) |
| 151 |
{ |
| 152 |
create_fake_snap_yaml (setup->snap_path, FALSE); |
| 153 |
create_fake_snapctl (setup->bin_path, NULL); |
| 154 |
|
| 155 |
g_assert_false (glib_should_use_portal ()); |
| 156 |
g_assert_false (glib_network_available_in_sandbox ()); |
| 157 |
g_assert_false (glib_has_dconf_access_in_sandbox ()); |
| 158 |
|
| 159 |
create_fake_snapctl (setup->bin_path, "desktop"); |
| 160 |
g_assert_true (glib_should_use_portal ()); |
| 161 |
g_assert_true (glib_network_available_in_sandbox ()); |
| 162 |
g_assert_false (glib_has_dconf_access_in_sandbox ()); |
| 163 |
|
| 164 |
create_fake_snapctl (setup->bin_path, "network-status|gsettings"); |
| 165 |
g_assert_false (glib_should_use_portal ()); |
| 166 |
g_assert_true (glib_network_available_in_sandbox ()); |
| 167 |
g_assert_true (glib_has_dconf_access_in_sandbox ()); |
| 168 |
|
| 169 |
create_fake_snapctl (setup->bin_path, "desktop|network-status|gsettings"); |
| 170 |
g_assert_true (glib_should_use_portal ()); |
| 171 |
g_assert_true (glib_network_available_in_sandbox ()); |
| 172 |
g_assert_true (glib_has_dconf_access_in_sandbox ()); |
| 173 |
|
| 174 |
create_fake_snapctl (setup->bin_path, "desktop|gsettings"); |
| 175 |
g_assert_true (glib_should_use_portal ()); |
| 176 |
g_assert_true (glib_network_available_in_sandbox ()); |
| 177 |
g_assert_true (glib_has_dconf_access_in_sandbox ()); |
| 178 |
|
| 179 |
create_fake_snapctl (setup->bin_path, "gsettings"); |
| 180 |
g_assert_false (glib_should_use_portal ()); |
| 181 |
g_assert_false (glib_network_available_in_sandbox ()); |
| 182 |
g_assert_true (glib_has_dconf_access_in_sandbox ()); |
| 183 |
|
| 184 |
create_fake_snapctl (setup->bin_path, NULL); |
| 185 |
g_assert_false (glib_should_use_portal ()); |
| 186 |
g_assert_false (glib_network_available_in_sandbox ()); |
| 187 |
g_assert_false (glib_has_dconf_access_in_sandbox ()); |
| 188 |
} |
| 189 |
|
| 190 |
int |
| 191 |
main (int argc, char **argv) |
| 192 |
{ |
| 193 |
g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL); |
| 194 |
|
| 195 |
g_test_add ("/portal-support/snap/no-snapctl", SetupData, NULL, |
| 196 |
tests_setup, test_portal_support_snap_no_snapctl, tests_teardown); |
| 197 |
g_test_add ("/portal-support/snap/none", SetupData, NULL, |
| 198 |
tests_setup, test_portal_support_snap_none, tests_teardown); |
| 199 |
g_test_add ("/portal-support/snap/all", SetupData, NULL, |
| 200 |
tests_setup, test_portal_support_snap_all, tests_teardown); |
| 201 |
g_test_add ("/portal-support/snap/desktop-only", SetupData, NULL, |
| 202 |
tests_setup, test_portal_support_snap_desktop_only, tests_teardown); |
| 203 |
g_test_add ("/portal-support/snap/network-only", SetupData, NULL, |
| 204 |
tests_setup, test_portal_support_snap_network_only, tests_teardown); |
| 205 |
g_test_add ("/portal-support/snap/gsettings-only", SetupData, NULL, |
| 206 |
tests_setup, test_portal_support_snap_gsettings_only, tests_teardown); |
| 207 |
g_test_add ("/portal-support/snap/updates-dynamically", SetupData, NULL, |
| 208 |
tests_setup, test_portal_support_snap_updates_dynamically, tests_teardown); |
| 209 |
|
| 210 |
return g_test_run (); |
| 211 |
} |