summaryrefslogtreecommitdiff
path: root/tests/test-instance.c
diff options
context:
space:
mode:
authorSimon McVittie <smcv@collabora.com>2021-04-07 18:55:07 +0100
committerAlexander Larsson <alexander.larsson@gmail.com>2021-04-15 18:00:47 +0200
commit712efd3156e62ac175544848898bbc2bd0430ae6 (patch)
tree1934434a6a10b3fd6798d4e4ff659f33ad378e0c /tests/test-instance.c
parentc8a4248eb481448bb319373ff3a75708151896da (diff)
downloadflatpak-712efd3156e62ac175544848898bbc2bd0430ae6.tar.gz
instance: Create and destroy per-app-ID subdirs of XRD/.flatpak
If we want to provide a per-app-ID XDG_RUNTIME_DIR (#4120) or a per-app-ID /tmp or /dev/shm (#4093) then we'll need somewhere to put them. Unlike $XDG_RUNTIME_DIR/app/$FLATPAK_ID, this should be somewhere that is *not* accessible to the app, so that we can trust its contents. Signed-off-by: Simon McVittie <smcv@collabora.com>
Diffstat (limited to 'tests/test-instance.c')
-rw-r--r--tests/test-instance.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/tests/test-instance.c b/tests/test-instance.c
index c73369d7..2ea23e3e 100644
--- a/tests/test-instance.c
+++ b/tests/test-instance.c
@@ -37,19 +37,42 @@
#include "testlib.h"
static void
+populate_with_files (const char *dir)
+{
+ static const char * const names[] = { "one", "two", "three" };
+ gsize i;
+
+ for (i = 0; i < G_N_ELEMENTS (names); i++)
+ {
+ g_autoptr(GError) error = NULL;
+ g_autofree char *path = g_build_filename (dir, names[i], NULL);
+
+ g_file_set_contents (path, "hello", -1, &error);
+ g_assert_no_error (error);
+ }
+}
+
+static void
test_gc (void)
{
g_autoptr(GBytes) bytes = NULL;
g_autoptr(GError) error = NULL;
g_autoptr(GPtrArray) instances = NULL;
g_autofree char *instances_dir = flatpak_instance_get_instances_directory ();
+ g_autofree char *apps_dir = flatpak_instance_get_instances_directory ();
g_autofree char *hold_lock = g_test_build_filename (G_TEST_BUILT, "hold-lock", NULL);
+ g_autofree char *alive_app_dir = NULL;
+ g_autofree char *alive_app_lock = NULL;
+ g_autofree char *alive_app_test_cleanup = NULL;
g_autofree char *alive_instance_dir = NULL;
g_autofree char *alive_instance_info = NULL;
g_autofree char *alive_instance_lock = NULL;
g_autofree char *alive_dead_instance_dir = NULL;
g_autofree char *alive_dead_instance_info = NULL;
g_autofree char *alive_dead_instance_lock = NULL;
+ g_autofree char *dead_app_dir = NULL;
+ g_autofree char *dead_app_lock = NULL;
+ g_autofree char *dead_app_test_cleanup = NULL;
g_autofree char *dead_instance_dir = NULL;
g_autofree char *dead_instance_info = NULL;
g_autofree char *dead_instance_lock = NULL;
@@ -59,6 +82,8 @@ test_gc (void)
"<BUILT>/hold-lock",
"--lock-file",
"<instance>/.ref",
+ "--lock-file",
+ "<appID>/.ref",
NULL
};
GPid pid = -1;
@@ -69,6 +94,14 @@ test_gc (void)
/* com.example.Alive has one instance, #1, running.
* A second instance, #2, was running until recently but has exited. */
+ alive_app_dir = g_build_filename (apps_dir, "com.example.Alive", NULL);
+ g_assert_no_errno (g_mkdir_with_parents (alive_app_dir, 0700));
+ alive_app_test_cleanup = g_build_filename (alive_app_dir, "test-cleanup", NULL);
+ g_assert_no_errno (g_mkdir_with_parents (alive_app_test_cleanup, 0700));
+ populate_with_files (alive_app_test_cleanup);
+ alive_app_lock = g_build_filename (alive_app_dir, ".ref", NULL);
+ g_file_set_contents (alive_app_lock, "", 0, &error);
+ g_assert_no_error (error);
alive_instance_dir = g_build_filename (instances_dir, "1", NULL);
g_assert_no_errno (g_mkdir_with_parents (alive_instance_dir, 0700));
@@ -100,6 +133,7 @@ test_gc (void)
* by our own process. */
hold_lock_argv[0] = hold_lock;
hold_lock_argv[2] = alive_instance_lock;
+ hold_lock_argv[4] = alive_app_lock;
g_spawn_async_with_pipes (NULL,
(gchar **) hold_lock_argv,
NULL,
@@ -117,6 +151,14 @@ test_gc (void)
/* com.example.Dead has no instances running.
* Instance #4 was running until recently but has exited. */
+ dead_app_dir = g_build_filename (apps_dir, "com.example.Dead", NULL);
+ g_assert_no_errno (g_mkdir_with_parents (dead_app_dir, 0700));
+ dead_app_test_cleanup = g_build_filename (dead_app_dir, "test-cleanup", NULL);
+ g_assert_no_errno (g_mkdir_with_parents (dead_app_test_cleanup, 0700));
+ populate_with_files (dead_app_test_cleanup);
+ dead_app_lock = g_build_filename (dead_app_dir, ".ref", NULL);
+ g_file_set_contents (dead_app_lock, "", 0, &error);
+ g_assert_no_error (error);
dead_instance_dir = g_build_filename (instances_dir, "4", NULL);
g_assert_no_errno (g_mkdir_with_parents (dead_instance_dir, 0700));
@@ -136,8 +178,10 @@ test_gc (void)
/* Pretend the locks were created in early 1970, to bypass the workaround
* for a race */
+ g_assert_no_errno (g_utime (alive_app_lock, &a_while_ago));
g_assert_no_errno (g_utime (alive_instance_lock, &a_while_ago));
g_assert_no_errno (g_utime (alive_dead_instance_lock, &a_while_ago));
+ g_assert_no_errno (g_utime (dead_app_lock, &a_while_ago));
g_assert_no_errno (g_utime (dead_instance_lock, &a_while_ago));
/* This has the side-effect of GC'ing instances */
@@ -148,6 +192,19 @@ test_gc (void)
g_assert_cmpint (stat (alive_dead_instance_dir, &stat_buf) == 0 ? 0 : errno, ==, ENOENT);
g_assert_cmpint (stat (dead_instance_dir, &stat_buf) == 0 ? 0 : errno, ==, ENOENT);
+ /* We don't GC the per-app directories themselves, or their lock files */
+ g_assert_no_errno (stat (alive_app_dir, &stat_buf));
+ g_assert_no_errno (stat (alive_app_lock, &stat_buf));
+ g_assert_no_errno (stat (dead_app_dir, &stat_buf));
+ g_assert_no_errno (stat (dead_app_lock, &stat_buf));
+
+ /* Until we implement something that actively uses this directory,
+ * use the test-cleanup subdirectory to check whether GC took place.
+ * We GC the test-cleanup subdirectory if there is no instance alive.
+ * We do not GC it if there is still an instance holding the lock. */
+ g_assert_no_errno (stat (alive_app_test_cleanup, &stat_buf));
+ g_assert_cmpint (stat (dead_app_test_cleanup, &stat_buf) == 0 ? 0 : errno, ==, ENOENT);
+
g_assert_cmpuint (instances->len, ==, 1);
instance = g_ptr_array_index (instances, 0);
g_assert_true (FLATPAK_IS_INSTANCE (instance));