gio/tests: Add tests for flatpak infos

Support testing flatpak infos, we do it by faking /.flatpak-info file in
case we're building in test mode.
This commit is contained in:
Marco Trevisan (Treviño) 2022-11-24 19:07:27 +01:00
parent e6eebfd9c3
commit 92fae633a0
10 changed files with 313 additions and 2 deletions

View File

@ -77,13 +77,21 @@ sandbox_info_read (void)
case G_SANDBOX_TYPE_FLATPAK:
{
GKeyFile *keyfile;
const char *keyfile_path = "/.flatpak-info";
use_portal = TRUE;
network_available = FALSE;
dconf_access = FALSE;
keyfile = g_key_file_new ();
if (g_key_file_load_from_file (keyfile, "/.flatpak-info", G_KEY_FILE_NONE, NULL))
#ifdef G_PORTAL_SUPPORT_TEST
char *test_key_file =
g_build_filename (g_get_user_runtime_dir (), keyfile_path, NULL);
keyfile_path = test_key_file;
#endif
if (g_key_file_load_from_file (keyfile, keyfile_path, G_KEY_FILE_NONE, NULL))
{
char **shared = NULL;
char *dconf_policy = NULL;
@ -104,6 +112,10 @@ sandbox_info_read (void)
}
}
#ifdef G_PORTAL_SUPPORT_TEST
g_clear_pointer (&test_key_file, g_free);
#endif
g_key_file_unref (keyfile);
}
break;

View File

@ -29,7 +29,22 @@
static gboolean
is_flatpak (void)
{
return g_file_test ("/.flatpak-info", G_FILE_TEST_EXISTS);
const char *flatpak_info = "/.flatpak-info";
gboolean found;
#ifdef G_PORTAL_SUPPORT_TEST
char *test_key_file =
g_build_filename (g_get_user_runtime_dir (), flatpak_info, NULL);
flatpak_info = test_key_file;
#endif
found = g_file_test (flatpak_info, G_FILE_TEST_EXISTS);
#ifdef G_PORTAL_SUPPORT_TEST
g_clear_pointer (&test_key_file, g_free);
#endif
return found;
}
static gchar *

View File

@ -109,6 +109,7 @@ gio_tests = {
'readwrite' : {},
'sandbox' : {
'extra_sources': ['../gsandbox.c', 'portal-support-utils.c'],
'c_args': ['-DG_PORTAL_SUPPORT_TEST'],
},
'simple-async-result' : {},
'simple-proxy' : {},
@ -227,6 +228,26 @@ if host_machine.system() != 'windows'
'gdbus-peer-object-manager' : {},
'gdbus-sasl' : {},
'live-g-file' : {},
'portal-support-flatpak-none' : {
'extra_sources': ['../gportalsupport.c', '../gsandbox.c', 'portal-support-utils.c'],
'c_args': ['-DG_PORTAL_SUPPORT_TEST'],
'suite': ['portal-support'],
},
'portal-support-flatpak-full' : {
'extra_sources': ['../gportalsupport.c', '../gsandbox.c', 'portal-support-utils.c'],
'c_args': ['-DG_PORTAL_SUPPORT_TEST'],
'suite': ['portal-support'],
},
'portal-support-flatpak-network-only' : {
'extra_sources': ['../gportalsupport.c', '../gsandbox.c', 'portal-support-utils.c'],
'c_args': ['-DG_PORTAL_SUPPORT_TEST'],
'suite': ['portal-support'],
},
'portal-support-flatpak-gsettings-only' : {
'extra_sources': ['../gportalsupport.c', '../gsandbox.c', 'portal-support-utils.c'],
'c_args': ['-DG_PORTAL_SUPPORT_TEST'],
'suite': ['portal-support'],
},
'portal-support-none' : {
'extra_sources': ['../gportalsupport.c', '../gsandbox.c'],
'c_args': ['-DG_PORTAL_SUPPORT_TEST'],

View File

@ -0,0 +1,49 @@
/*
* GIO - GLib Input, Output and Streaming Library
*
* Copyright (C) 2022 Canonical Ltd.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Author: Marco Trevisan <marco.trevisan@canonical.com>
*/
#include "portal-support-utils.h"
#include "../gportalsupport.h"
#include <gio/gio.h>
static void
test_portal_support_flatpak_full (void)
{
create_fake_flatpak_info (g_get_user_runtime_dir (),
(GStrv)(const char* []) {"foo", "bar", "network", "more", NULL},
"talk");
g_assert_true (glib_should_use_portal ());
g_assert_true (glib_network_available_in_sandbox ());
g_assert_true (glib_has_dconf_access_in_sandbox ());
}
int
main (int argc, char **argv)
{
g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
g_test_add_func ("/portal-support/flatpak/full", test_portal_support_flatpak_full);
return g_test_run ();
}

View File

@ -0,0 +1,49 @@
/*
* GIO - GLib Input, Output and Streaming Library
*
* Copyright (C) 2022 Canonical Ltd.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Author: Marco Trevisan <marco.trevisan@canonical.com>
*/
#include "portal-support-utils.h"
#include "../gportalsupport.h"
#include <gio/gio.h>
static void
test_portal_support_flatpak_gsettings_only (void)
{
create_fake_flatpak_info (g_get_user_runtime_dir (),
(GStrv)(const char* []) {"unsupported-stuff", NULL},
"talk");
g_assert_true (glib_should_use_portal ());
g_assert_false (glib_network_available_in_sandbox ());
g_assert_true (glib_has_dconf_access_in_sandbox ());
}
int
main (int argc, char **argv)
{
g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
g_test_add_func ("/portal-support/flatpak/gsettings", test_portal_support_flatpak_gsettings_only);
return g_test_run ();
}

View File

@ -0,0 +1,49 @@
/*
* GIO - GLib Input, Output and Streaming Library
*
* Copyright (C) 2022 Canonical Ltd.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Author: Marco Trevisan <marco.trevisan@canonical.com>
*/
#include "portal-support-utils.h"
#include "../gportalsupport.h"
#include <gio/gio.h>
static void
test_portal_support_flatpak_network (void)
{
create_fake_flatpak_info (g_get_user_runtime_dir (),
(GStrv)(const char* []) {"foo", "bar", "network", "more", NULL},
"do-not-talk");
g_assert_true (glib_should_use_portal ());
g_assert_true (glib_network_available_in_sandbox ());
g_assert_false (glib_has_dconf_access_in_sandbox ());
}
int
main (int argc, char **argv)
{
g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
g_test_add_func ("/portal-support/flatpak/network", test_portal_support_flatpak_network);
return g_test_run ();
}

View File

@ -0,0 +1,47 @@
/*
* GIO - GLib Input, Output and Streaming Library
*
* Copyright (C) 2022 Canonical Ltd.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Author: Marco Trevisan <marco.trevisan@canonical.com>
*/
#include "portal-support-utils.h"
#include "../gportalsupport.h"
#include <gio/gio.h>
static void
test_portal_support_flatpak_none (void)
{
create_fake_flatpak_info (g_get_user_runtime_dir (), NULL, NULL);
g_assert_true (glib_should_use_portal ());
g_assert_false (glib_network_available_in_sandbox ());
g_assert_false (glib_has_dconf_access_in_sandbox ());
}
int
main (int argc, char **argv)
{
g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
g_test_add_func ("/portal-support/flatpak/none", test_portal_support_flatpak_none);
return g_test_run ();
}

View File

@ -85,3 +85,57 @@ create_fake_snap_yaml (const char *snap_path,
g_free (yaml_path);
g_free (yaml_contents);
}
void
create_fake_flatpak_info_from_key_file (const char *root_path,
GKeyFile *key_file)
{
GError *error = NULL;
char *key_file_path;
g_assert_nonnull (root_path);
key_file_path = g_build_filename (root_path, ".flatpak-info", NULL);
g_test_message ("Creating .flatpak-info in %s", key_file_path);
g_key_file_save_to_file (key_file, key_file_path, &error);
g_assert_no_error (error);
g_free (key_file_path);
}
void
create_fake_flatpak_info (const char *root_path,
const GStrv shared_context,
const char *dconf_dbus_policy)
{
GKeyFile *key_file;
key_file = g_key_file_new ();
/* File format is defined at:
* https://docs.flatpak.org/en/latest/flatpak-command-reference.html
*/
g_key_file_set_string (key_file, "Application", "name",
"org.gnome.GLib.Test.Flatpak");
g_key_file_set_string (key_file, "Application", "runtime",
"org.gnome.Platform/x86_64/44");
g_key_file_set_string (key_file, "Application", "sdk",
"org.gnome.Sdk/x86_64/44");
if (shared_context)
{
g_key_file_set_string_list (key_file, "Context", "shared",
(const char * const *) shared_context,
g_strv_length (shared_context));
}
if (dconf_dbus_policy)
{
g_key_file_set_string (key_file, "Session Bus Policy", "ca.desrt.dconf",
dconf_dbus_policy);
}
create_fake_flatpak_info_from_key_file (root_path, key_file);
g_key_file_free (key_file);
}

View File

@ -28,3 +28,10 @@ void create_fake_snap_yaml (const char *snap_path,
void create_fake_snapctl (const char *path,
const char *supported_op);
void create_fake_flatpak_info (const char *root_path,
const GStrv shared_context,
const char *dconf_dbus_policy);
void create_fake_flatpak_info_from_key_file (const char *root_path,
GKeyFile *key_file);

View File

@ -67,6 +67,13 @@ test_sandbox_snap_classic (void)
g_free (snap_path);
}
static void
test_sandbox_flatpak (void)
{
create_fake_flatpak_info (g_get_user_runtime_dir (), NULL, NULL);
g_assert_cmpint (glib_get_sandbox_type (), ==, G_SANDBOX_TYPE_FLATPAK);
}
int
main (int argc, char **argv)
{
@ -75,6 +82,7 @@ main (int argc, char **argv)
g_test_add_func ("/sandbox/none", test_sandbox_none);
g_test_add_func ("/sandbox/snap", test_sandbox_snap);
g_test_add_func ("/sandbox/classic-snap", test_sandbox_snap_classic);
g_test_add_func ("/sandbox/flatpak", test_sandbox_flatpak);
return g_test_run ();
}