summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon McVittie <smcv@collabora.com>2021-01-28 15:24:59 +0000
committerAlexander Larsson <alexander.larsson@gmail.com>2021-02-09 09:33:57 +0100
commita926776cf4fcacc2d096e10bf1578a0e7c626cc7 (patch)
treeef0c7538045ef1cf1a4a9e76f5163d8065e9f2f9
parenta8cc112dfaf63ff2eace53cce422848c7def0f90 (diff)
downloadflatpak-a926776cf4fcacc2d096e10bf1578a0e7c626cc7.tar.gz
tests: Disable revokefs if FUSE doesn't work
There are various reasons why distro autobuilder environments might not be able to use FUSE in build-time tests: they might not have fusermount(1), they might be locked-down to be unable to load the FUSE kernel module as a result of security hardening, they might not have the /etc/mtab symlink, they might not have CAP_SYS_ADMIN in their capability bounding set, or they might not have write access to /dev/fuse. Try to check for all of these conditions. Heavily based on code that I previously contributed to xdg-desktop-portal. Disabling revokefs, instead of skipping the test that needs it, is based on the patches applied in @alexlarsson's PPA. Signed-off-by: Simon McVittie <smcv@collabora.com>
-rw-r--r--tests/Makefile.am.inc8
-rw-r--r--tests/can-use-fuse.c100
-rw-r--r--tests/can-use-fuse.h12
-rw-r--r--tests/testlibrary.c14
4 files changed, 127 insertions, 7 deletions
diff --git a/tests/Makefile.am.inc b/tests/Makefile.am.inc
index 01c3431d..2458445c 100644
--- a/tests/Makefile.am.inc
+++ b/tests/Makefile.am.inc
@@ -23,17 +23,23 @@ endif
testlibrary_CFLAGS = \
$(AM_CFLAGS) \
$(BASE_CFLAGS) \
+ $(FUSE_CFLAGS) \
$(OSTREE_CFLAGS) \
-DFLATPAK_COMPILATION \
$(NULL)
testlibrary_LDADD = \
$(AM_LDADD) \
$(BASE_LIBS) \
+ $(FUSE_LIBS) \
$(OSTREE_LIBS) \
libglnx.la \
libflatpak.la \
$(NULL)
-testlibrary_SOURCES = tests/testlibrary.c
+testlibrary_SOURCES = \
+ tests/can-use-fuse.c \
+ tests/can-use-fuse.h \
+ tests/testlibrary.c \
+ $(NULL)
testcommon_CFLAGS = \
$(AM_CFLAGS) \
diff --git a/tests/can-use-fuse.c b/tests/can-use-fuse.c
new file mode 100644
index 00000000..25f3936e
--- /dev/null
+++ b/tests/can-use-fuse.c
@@ -0,0 +1,100 @@
+/*
+ * Copyright 2019-2021 Collabora Ltd.
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ */
+
+#include "config.h"
+
+#include "can-use-fuse.h"
+
+#include <unistd.h>
+
+#include <glib/gstdio.h>
+
+#include "libglnx/libglnx.h"
+
+#define FUSE_USE_VERSION 26
+#include <fuse_lowlevel.h>
+
+gchar *cannot_use_fuse = NULL;
+
+/*
+ * If we cannot use FUSE, set cannot_use_fuse and return %FALSE.
+ */
+gboolean
+check_fuse (void)
+{
+ g_autofree gchar *fusermount = NULL;
+ g_autofree gchar *path = NULL;
+ char *argv[] = { "flatpak-fuse-test" };
+ struct fuse_args args = FUSE_ARGS_INIT (G_N_ELEMENTS (argv), argv);
+ struct fuse_chan *chan = NULL;
+ g_autoptr(GError) error = NULL;
+
+ if (cannot_use_fuse != NULL)
+ return FALSE;
+
+ if (access ("/dev/fuse", W_OK) != 0)
+ {
+ cannot_use_fuse = g_strdup_printf ("access /dev/fuse: %s",
+ g_strerror (errno));
+ return FALSE;
+ }
+
+ fusermount = g_find_program_in_path ("fusermount");
+
+ if (fusermount == NULL)
+ {
+ cannot_use_fuse = g_strdup ("fusermount not found in PATH");
+ return FALSE;
+ }
+
+ if (!g_file_test (fusermount, G_FILE_TEST_IS_EXECUTABLE))
+ {
+ cannot_use_fuse = g_strdup_printf ("%s not executable", fusermount);
+ return FALSE;
+ }
+
+ if (!g_file_test ("/etc/mtab", G_FILE_TEST_EXISTS))
+ {
+ cannot_use_fuse = g_strdup ("fusermount won't work without /etc/mtab");
+ return FALSE;
+ }
+
+ path = g_dir_make_tmp ("flatpak-test.XXXXXX", &error);
+ g_assert_no_error (error);
+
+ chan = fuse_mount (path, &args);
+
+ if (chan == NULL)
+ {
+ fuse_opt_free_args (&args);
+ cannot_use_fuse = g_strdup_printf ("fuse_mount: %s",
+ g_strerror (errno));
+ return FALSE;
+ }
+
+ g_test_message ("Successfully set up test FUSE fs on %s", path);
+
+ fuse_unmount (path, chan);
+
+ if (g_rmdir (path) != 0)
+ g_error ("rmdir %s: %s", path, g_strerror (errno));
+
+ fuse_opt_free_args (&args);
+
+ return TRUE;
+}
+
+gboolean
+check_fuse_or_skip_test (void)
+{
+ if (!check_fuse ())
+ {
+ g_assert (cannot_use_fuse != NULL);
+ g_test_skip (cannot_use_fuse);
+ return FALSE;
+ }
+
+ return TRUE;
+}
diff --git a/tests/can-use-fuse.h b/tests/can-use-fuse.h
new file mode 100644
index 00000000..bfc47e35
--- /dev/null
+++ b/tests/can-use-fuse.h
@@ -0,0 +1,12 @@
+/*
+ * Copyright 2019-2021 Collabora Ltd.
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ */
+
+#pragma once
+
+#include <glib.h>
+
+extern gchar *cannot_use_fuse;
+gboolean check_fuse (void);
+gboolean check_fuse_or_skip_test (void);
diff --git a/tests/testlibrary.c b/tests/testlibrary.c
index 718f37cb..64dff194 100644
--- a/tests/testlibrary.c
+++ b/tests/testlibrary.c
@@ -10,6 +10,8 @@
#include "libglnx/libglnx.h"
#include "flatpak.h"
+#include "can-use-fuse.h"
+
static char *testdir;
static char *flatpak_runtimedir;
static char *flatpak_systemdir;
@@ -2611,6 +2613,12 @@ global_setup (void)
g_setenv ("FLATPAK_SYSTEM_HELPER_ON_SESSION", "1", TRUE);
g_setenv ("LANGUAGE", "en", TRUE);
+ if (!check_fuse ())
+ {
+ g_test_message ("FUSE doesn't seem to work? Disabling revokefs");
+ g_setenv ("FLATPAK_DISABLE_REVOKEFS", "1", TRUE);
+ }
+
test_bus = g_test_dbus_new (G_TEST_DBUS_NONE);
services_dir = g_test_build_filename (G_TEST_BUILT, "services", NULL);
@@ -4459,12 +4467,6 @@ test_installation_unused_refs_across_installations (void)
FlatpakInstalledRef *unused_ref;
gboolean res;
- if (!g_file_test ("/etc/mtab", G_FILE_TEST_EXISTS))
- {
- g_test_skip ("fusermount won't work without /etc/mtab");
- return;
- }
-
runtime = g_strdup_printf ("runtime/org.test.Platform/%s/master",
flatpak_get_default_arch ());
app = g_strdup_printf ("app/org.test.Hello/%s/master",