Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/omamobile-settings/test/all
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"
Expand All @@ -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"
1 change: 1 addition & 0 deletions packages/omamobile-settings/tests/core_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "backend.h"
#include "deviceinfocontroller.h"
#include "actionrunner.h"
#include "home_guard.h"
#include <QtTest>
#include <QQmlComponent>
#include <memory>
Expand Down
2 changes: 1 addition & 1 deletion packages/omamobile-settings/tests/core_tests.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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
59 changes: 59 additions & 0 deletions packages/omamobile-settings/tests/home_guard.h
Original file line number Diff line number Diff line change
@@ -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 <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <pwd.h>
#include <string>
#include <unistd.h>

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);
}