From 0754c246fee9287d1989dca227cee3556d28c500 Mon Sep 17 00:00:00 2001 From: Lef Date: Thu, 19 Nov 2026 09:00:57 +0200 Subject: [PATCH] settings tests: refuse to run unless HOME is a temp fixture Running settings-core-tests directly used the real HOME and overwrote ~/.local/state/omarchy/current/theme. A static guard now exits before main() unless HOME is inside the temp dir and is not the user's home. --- packages/omamobile-settings/test/all | 6 +- .../omamobile-settings/tests/core_tests.cpp | 1 + .../omamobile-settings/tests/core_tests.pro | 2 +- .../omamobile-settings/tests/home_guard.h | 59 +++++++++++++++++++ 4 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 packages/omamobile-settings/tests/home_guard.h diff --git a/packages/omamobile-settings/test/all b/packages/omamobile-settings/test/all index a8623d42a..6fcd0b0ad 100755 --- a/packages/omamobile-settings/test/all +++ b/packages/omamobile-settings/test/all @@ -14,7 +14,7 @@ chmod 700 "$fixture/runtime" cd "$build_dir" qmake6 "$source_dir/tests/core_tests.pro" make -j1 -HOME="$fixture" XDG_CONFIG_HOME="$fixture/config" XDG_CACHE_HOME="$fixture/cache" \ +HOME="$fixture" XDG_STATE_HOME="$fixture/state" XDG_DATA_HOME="$fixture/data" XDG_CONFIG_HOME="$fixture/config" XDG_CACHE_HOME="$fixture/cache" \ XDG_RUNTIME_DIR="$fixture/runtime" QT_QPA_PLATFORM=offscreen QT_QUICK_BACKEND=software \ QT_QPA_PLATFORMTHEME= NO_AT_BRIDGE=1 GSETTINGS_BACKEND=memory ./settings-core-tests @@ -24,7 +24,7 @@ mkdir -p "$lifetime" cd "$lifetime" qmake6 "$source_dir/tests/deviceinfo_lifetime_tests.pro" make -j1 - HOME="$fixture" XDG_CONFIG_HOME="$fixture/config" XDG_RUNTIME_DIR="$fixture/runtime" \ + HOME="$fixture" XDG_STATE_HOME="$fixture/state" XDG_DATA_HOME="$fixture/data" XDG_CONFIG_HOME="$fixture/config" XDG_RUNTIME_DIR="$fixture/runtime" \ QT_QPA_PLATFORMTHEME= NO_AT_BRIDGE=1 ./settings-deviceinfo-lifetime-tests ) session="$build_dir/session" @@ -34,7 +34,7 @@ mkdir -p "$session" qmake6 "$source_dir/tests/session_app.pro" make -j1 ) -HOME="$fixture" XDG_CONFIG_HOME="$fixture/config" XDG_CACHE_HOME="$fixture/cache" \ +HOME="$fixture" XDG_STATE_HOME="$fixture/state" XDG_DATA_HOME="$fixture/data" XDG_CONFIG_HOME="$fixture/config" XDG_CACHE_HOME="$fixture/cache" \ XDG_RUNTIME_DIR="$fixture/runtime" QT_QPA_PLATFORM=offscreen QT_QUICK_BACKEND=software \ QT_QPA_PLATFORMTHEME= NO_AT_BRIDGE=1 GSETTINGS_BACKEND=memory dbus-run-session -- python3 "$source_dir/test/single-instance.py" \ "$session/settings-session-test-app" diff --git a/packages/omamobile-settings/tests/core_tests.cpp b/packages/omamobile-settings/tests/core_tests.cpp index b87a721ce..1fdb001af 100644 --- a/packages/omamobile-settings/tests/core_tests.cpp +++ b/packages/omamobile-settings/tests/core_tests.cpp @@ -3,6 +3,7 @@ #include "backend.h" #include "deviceinfocontroller.h" #include "actionrunner.h" +#include "home_guard.h" #include #include #include diff --git a/packages/omamobile-settings/tests/core_tests.pro b/packages/omamobile-settings/tests/core_tests.pro index 228e867ff..1420796bd 100644 --- a/packages/omamobile-settings/tests/core_tests.pro +++ b/packages/omamobile-settings/tests/core_tests.pro @@ -5,5 +5,5 @@ TARGET = settings-core-tests TEMPLATE = app INCLUDEPATH += ../app SOURCES += core_tests.cpp ../app/backend.cpp ../app/deviceinfocontroller.cpp ../app/actionrunner.cpp -HEADERS += ../app/backend.h ../app/deviceinfocontroller.h ../app/actionrunner.h +HEADERS += ../app/backend.h ../app/deviceinfocontroller.h ../app/actionrunner.h home_guard.h RESOURCES += ../app/resources.qrc diff --git a/packages/omamobile-settings/tests/home_guard.h b/packages/omamobile-settings/tests/home_guard.h new file mode 100644 index 000000000..6ee177630 --- /dev/null +++ b/packages/omamobile-settings/tests/home_guard.h @@ -0,0 +1,59 @@ +// SPDX-License-Identifier: MIT +// Refuse to run tests that write under $HOME unless HOME is a throwaway +// directory inside the temp dir and is not the user's real home. Runs from +// a static constructor, so it fires before main() and before any test. +#pragma once +#include +#include +#include +#include +#include +#include +#include + +namespace home_guard { +inline std::string canonical(const char *path) { + char buffer[PATH_MAX]; + return path && *path && realpath(path, buffer) ? std::string(buffer) : std::string(); +} +[[noreturn]] inline void refuse(const char *why) { + std::fprintf(stderr, "settings tests: refusing to run: %s.\n" + "Run them through packages/omamobile-settings/test/all (or the repo-wide tests/all), which sets HOME to a mktemp fixture.\n", why); + std::fflush(stderr); + _exit(2); +} +inline bool inside(const std::string &path, const std::string &dir) { + return !dir.empty() && path.rfind(dir + "/", 0) == 0; +} +// Pure decision, testable without touching the real home. Returns nullptr when +// HOME is safe, otherwise the reason. A fixture strictly inside the real home is +// fine (CI's RUNNER_TEMP, mktemp -p build/) as long as it is inside the temp dir. +inline const char *home_verdict(const std::string &home, const std::string &real, const std::string &temp) { + if (home.empty()) return "HOME is unset or does not exist"; + if (!real.empty() && (home == real || inside(real, home))) return "HOME is the real user home or contains it"; + if (!inside(home, temp)) return "HOME is not inside the temp directory"; + return nullptr; +} +inline const char *xdg_verdict(const std::string &path, const std::string &home, const std::string &real, const std::string &temp) { + if (!real.empty() && (path == real || inside(real, path))) return "an XDG_*_HOME variable is the real user home or contains it"; + if (path != home && !inside(path, home) && !inside(path, temp)) + return "an XDG_*_HOME variable points outside the fixture HOME and temp directory"; + return nullptr; +} +inline void check() { + const std::string home = canonical(std::getenv("HOME")); + const passwd *entry = getpwuid(getuid()); + const std::string real = canonical(entry ? entry->pw_dir : nullptr); + const char *tmpdir = std::getenv("TMPDIR"); + const std::string temp = canonical(tmpdir && *tmpdir ? tmpdir : "/tmp"); + if (const char *why = home_verdict(home, real, temp)) refuse(why); + for (const char *name : {"XDG_STATE_HOME", "XDG_CONFIG_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME"}) { + const char *value = std::getenv(name); + if (!value || !*value) continue; + const std::string path = canonical(value); + // A path that does not exist yet is checked as written. + if (const char *why = xdg_verdict(path.empty() ? std::string(value) : path, home, real, temp)) refuse(why); + } +} +inline const bool checked = (check(), true); +}