summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon McVittie <smcv@collabora.com>2021-07-27 11:45:31 +0100
committerAlexander Larsson <alexander.larsson@gmail.com>2021-08-02 08:37:25 +0200
commit7fbb165c8143f40c13a634a83759b2b9dd8d4a1c (patch)
tree17f4bb32ce74d8fd0b04c4eaf14bce59f0d92dee
parentf59d28b4cf4728445d2793cce898ba3329781370 (diff)
downloadflatpak-7fbb165c8143f40c13a634a83759b2b9dd8d4a1c.tar.gz
tests: Exercise failure to export a broken autofs
To achieve this, add a flag to FlatpakExports to make it fake a broken autofs. Signed-off-by: Simon McVittie <smcv@collabora.com>
-rw-r--r--common/flatpak-exports-private.h15
-rw-r--r--common/flatpak-exports.c18
-rw-r--r--tests/test-exports.c43
3 files changed, 67 insertions, 9 deletions
diff --git a/common/flatpak-exports-private.h b/common/flatpak-exports-private.h
index 5266e44b..d7e22e7e 100644
--- a/common/flatpak-exports-private.h
+++ b/common/flatpak-exports-private.h
@@ -61,8 +61,21 @@ FlatpakFilesystemMode flatpak_exports_path_get_mode (FlatpakExports *exports,
G_DEFINE_AUTOPTR_CLEANUP_FUNC (FlatpakExports, flatpak_exports_free);
+/*
+ * FlatpakExportsTestFlags:
+ * @FLATPAK_EXPORTS_TEST_FLAGS_AUTOFS: Pretend everything is an autofs.
+ *
+ * Flags used to provide mock behaviour during unit testing.
+ */
+typedef enum
+{
+ FLATPAK_EXPORTS_TEST_FLAGS_AUTOFS = (1 << 0),
+ FLATPAK_EXPORTS_TEST_FLAGS_NONE = 0
+} FlatpakExportsTestFlags;
+
void flatpak_exports_take_host_fd (FlatpakExports *exports,
int fd);
-
+void flatpak_exports_set_test_flags (FlatpakExports *exports,
+ FlatpakExportsTestFlags flags);
#endif /* __FLATPAK_EXPORTS_H__ */
diff --git a/common/flatpak-exports.c b/common/flatpak-exports.c
index 8156cca5..7e26d034 100644
--- a/common/flatpak-exports.c
+++ b/common/flatpak-exports.c
@@ -106,6 +106,7 @@ struct _FlatpakExports
FlatpakFilesystemMode host_etc;
FlatpakFilesystemMode host_os;
int host_fd;
+ FlatpakExportsTestFlags test_flags;
};
/*
@@ -122,6 +123,13 @@ flatpak_exports_take_host_fd (FlatpakExports *exports,
exports->host_fd = fd;
}
+void
+flatpak_exports_set_test_flags (FlatpakExports *exports,
+ FlatpakExportsTestFlags flags)
+{
+ exports->test_flags = flags;
+}
+
static gboolean
flatpak_exports_stat_in_host (FlatpakExports *exports,
const char *abs_path,
@@ -752,6 +760,12 @@ check_if_autofs_works (FlatpakExports *exports,
if (!WIFEXITED (wstatus) || WEXITSTATUS (wstatus) != 0)
return FALSE;
+ if (G_UNLIKELY (exports->test_flags & FLATPAK_EXPORTS_TEST_FLAGS_AUTOFS))
+ {
+ if (strcmp (path, "/broken-autofs") == 0)
+ return FALSE;
+ }
+
return TRUE;
}
@@ -802,7 +816,9 @@ _exports_path_expose (FlatpakExports *exports,
if (fstatfs (o_path_fd, &stfs) != 0)
return FALSE;
- if (stfs.f_type == AUTOFS_SUPER_MAGIC)
+ if (stfs.f_type == AUTOFS_SUPER_MAGIC ||
+ (G_UNLIKELY (exports->test_flags & FLATPAK_EXPORTS_TEST_FLAGS_AUTOFS) &&
+ S_ISDIR (st.st_mode)))
{
if (!check_if_autofs_works (exports, path))
{
diff --git a/tests/test-exports.c b/tests/test-exports.c
index 20fe815c..3d35eacd 100644
--- a/tests/test-exports.c
+++ b/tests/test-exports.c
@@ -927,11 +927,10 @@ create_fake_files (const FakeFile *files)
}
}
-static void
-test_host_exports (const FakeFile *files,
- FlatpakBwrap *bwrap,
- FlatpakFilesystemMode etc_mode,
- FlatpakFilesystemMode os_mode)
+static FlatpakExports *
+test_host_exports_setup (const FakeFile *files,
+ FlatpakFilesystemMode etc_mode,
+ FlatpakFilesystemMode os_mode)
{
g_autoptr(FlatpakExports) exports = flatpak_exports_new ();
g_autoptr(GError) error = NULL;
@@ -958,6 +957,16 @@ test_host_exports (const FakeFile *files,
if (os_mode > FLATPAK_FILESYSTEM_MODE_NONE)
flatpak_exports_add_host_os_expose (exports, os_mode);
+ return g_steal_pointer (&exports);
+}
+
+static void
+test_host_exports_finish (FlatpakExports *exports,
+ FlatpakBwrap *bwrap)
+{
+ g_autofree gchar *host = g_build_filename (isolated_test_dir, "host", NULL);
+ g_autoptr(GError) error = NULL;
+
flatpak_bwrap_add_arg (bwrap, "bwrap");
flatpak_exports_append_bwrap_args (exports, bwrap);
flatpak_bwrap_finish (bwrap);
@@ -972,6 +981,18 @@ test_host_exports (const FakeFile *files,
}
}
+static void
+test_host_exports (const FakeFile *files,
+ FlatpakBwrap *bwrap,
+ FlatpakFilesystemMode etc_mode,
+ FlatpakFilesystemMode os_mode)
+{
+ g_autoptr(FlatpakExports) exports = NULL;
+
+ exports = test_host_exports_setup (files, etc_mode, os_mode);
+ test_host_exports_finish (exports, bwrap);
+}
+
/*
* Test --filesystem=host-os with an OS that looks like Arch Linux.
*/
@@ -1273,6 +1294,7 @@ test_exports_unusual (void)
{ "etc/ld.so.conf", FAKE_FILE },
{ "etc/ld.so.conf.d", FAKE_DIR },
{ "bin", FAKE_SYMLINK, "usr/bin" },
+ { "broken-autofs", FAKE_DIR },
{ "lib", FAKE_SYMLINK, "usr/lib" },
{ "usr/bin", FAKE_DIR },
{ "usr/lib", FAKE_DIR },
@@ -1280,10 +1302,17 @@ test_exports_unusual (void)
{ NULL }
};
g_autoptr(FlatpakBwrap) bwrap = flatpak_bwrap_new (NULL);
+ g_autoptr(FlatpakExports) exports = NULL;
gsize i;
- test_host_exports (files, bwrap, FLATPAK_FILESYSTEM_MODE_NONE,
- FLATPAK_FILESYSTEM_MODE_READ_ONLY);
+ exports = test_host_exports_setup (files,
+ FLATPAK_FILESYSTEM_MODE_NONE,
+ FLATPAK_FILESYSTEM_MODE_READ_ONLY);
+ flatpak_exports_set_test_flags (exports, FLATPAK_EXPORTS_TEST_FLAGS_AUTOFS);
+ flatpak_exports_add_path_expose (exports,
+ FLATPAK_FILESYSTEM_MODE_READ_ONLY,
+ "/broken-autofs");
+ test_host_exports_finish (exports, bwrap);
i = 0;
g_assert_cmpuint (i, <, bwrap->argv->len);