1 /*
2 * Copyright 2022 Canonical Ltd
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "portal-support-utils.h"
21
22 #include "../gsandbox.h"
23 #include <gio/gio.h>
24 #include <glib/gstdio.h>
25
26 static void
27 test_sandbox_none (void)
28 {
29 g_assert_cmpint (glib_get_sandbox_type (), ==, G_SANDBOX_TYPE_UNKNOWN);
30 }
31
32 static void
33 test_sandbox_snap (void)
34 {
35 const char *temp_dir;
36 gchar *snap_path;
37
38 temp_dir = g_getenv ("G_TEST_TMPDIR");
39 g_assert_nonnull (temp_dir);
40
41 snap_path = g_build_filename (temp_dir, "snap", "current", NULL);
42 create_fake_snap_yaml (snap_path, FALSE);
43 g_setenv ("SNAP", snap_path, TRUE);
44
45 g_assert_cmpint (glib_get_sandbox_type (), ==, G_SANDBOX_TYPE_SNAP);
46
47 g_unsetenv ("SNAP");
48 g_free (snap_path);
49 }
50
51 static void
52 test_sandbox_snap_classic (void)
53 {
54 const char *temp_dir;
55 char *snap_path;
56
57 temp_dir = g_getenv ("G_TEST_TMPDIR");
58 g_assert_nonnull (temp_dir);
59
60 snap_path = g_build_filename (temp_dir, "snap", "current", NULL);
61 create_fake_snap_yaml (snap_path, TRUE);
62 g_setenv ("SNAP", snap_path, TRUE);
63
64 g_assert_cmpint (glib_get_sandbox_type (), ==, G_SANDBOX_TYPE_UNKNOWN);
65
66 g_unsetenv ("SNAP");
67 g_free (snap_path);
68 }
69
70 static void
71 test_sandbox_flatpak (void)
72 {
73 create_fake_flatpak_info (g_get_user_runtime_dir (), NULL, NULL);
74 g_assert_cmpint (glib_get_sandbox_type (), ==, G_SANDBOX_TYPE_FLATPAK);
75 }
76
77 int
78 main (int argc, char **argv)
79 {
80 g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
81
82 g_test_add_func ("/sandbox/none", test_sandbox_none);
83 g_test_add_func ("/sandbox/snap", test_sandbox_snap);
84 g_test_add_func ("/sandbox/classic-snap", test_sandbox_snap_classic);
85 g_test_add_func ("/sandbox/flatpak", test_sandbox_flatpak);
86
87 return g_test_run ();
88 }