#include "echoion/standalone/RuntimePaths.hpp" #include #include #include #include #include namespace { class TemporaryTree { public: TemporaryTree() { std::random_device random; path_ = std::filesystem::temp_directory_path() / ("echoion-runtime-paths-" + std::to_string(static_cast(random()))); std::filesystem::create_directories(path_); } ~TemporaryTree() { std::error_code error; std::filesystem::remove_all(path_, error); } TemporaryTree(const TemporaryTree&) = delete; TemporaryTree& operator=(const TemporaryTree&) = delete; [[nodiscard]] const std::filesystem::path& path() const noexcept { return path_; } private: std::filesystem::path path_; }; bool writeFile(const std::filesystem::path& path) { std::filesystem::create_directories(path.parent_path()); std::ofstream output(path); output << "test\n"; return output.good(); } } // namespace int main() { int failures{}; const auto check = [&failures](bool condition, const std::string& message) { if (!condition) { std::cerr << message << '\n'; ++failures; } }; TemporaryTree tree; const auto installRoot = tree.path() / "echoion"; const auto executable = installRoot / "bin" / "echoiond"; const auto executableDirectory = executable.parent_path(); check(Echoion::Standalone::deriveInstallRoot(executable) == installRoot, "bin/echoiond did not resolve to its installation root"); check(Echoion::Standalone::deriveInstallRoot( executable, tree.path() / "override") == tree.path() / "override", "ECHOION_HOME-style override was not preferred"); const auto state = Echoion::Standalone::resolveStateDirectory( tree.path() / "cli-state", tree.path() / "environment-state", tree.path() / "xdg-state", tree.path() / "home", tree.path() / "fallback"); check(state == tree.path() / "cli-state", "command-line state directory did not win precedence"); check(Echoion::Standalone::resolveStateDirectory( std::nullopt, tree.path() / "environment-state", tree.path() / "xdg-state", tree.path() / "home", tree.path() / "fallback") == tree.path() / "environment-state", "environment state directory did not win precedence"); check(Echoion::Standalone::resolveStateDirectory( std::nullopt, std::nullopt, tree.path() / "xdg-state", tree.path() / "home", tree.path() / "fallback") == tree.path() / "xdg-state" / "echoion", "XDG state directory fallback is incorrect"); const auto packagedCore = installRoot / "lib" / "Echoion.ts"; check(writeFile(packagedCore), "failed to create packaged Core fixture"); check(Echoion::Standalone::resolveCorePath( installRoot, executableDirectory) == packagedCore, "packaged Core was not resolved from install-root/lib"); check(Echoion::Standalone::resolveCorePath( installRoot, executableDirectory, std::filesystem::path{"custom/Echoion.ts"}) == installRoot / "custom" / "Echoion.ts", "relative Core override was not anchored at the install root"); const auto packagedIndex = installRoot / "share" / "echoion" / "web" / "index.html"; check(writeFile(packagedIndex), "failed to create packaged web fixture"); check(Echoion::Standalone::runtimeWorkingDirectory( installRoot, executableDirectory) == installRoot, "packaged web root did not select the installation root as working " "directory"); const auto bundledCertificates = installRoot / "share" / "echoion" / "certs" / "ca-certificates.crt"; check(!Echoion::Standalone::bundledCertificateBundle(installRoot), "missing bundled certificate store was accepted"); check(writeFile(bundledCertificates), "failed to create bundled certificate store fixture"); check(Echoion::Standalone::bundledCertificateBundle(installRoot) == bundledCertificates, "bundled certificate store was not resolved from the install root"); std::filesystem::remove_all(installRoot / "share"); const auto developmentIndex = executableDirectory / "web" / "index.html"; check(writeFile(developmentIndex), "failed to create development web fixture"); check(Echoion::Standalone::runtimeWorkingDirectory( installRoot, executableDirectory) == executableDirectory, "development web root did not select the executable directory"); const auto avatarCache = tree.path() / "state" / "avatars"; std::filesystem::create_directories(avatarCache); check(Echoion::Standalone::resolveAvatarCacheDirectory(avatarCache) == avatarCache, "empty avatar directory request did not select the state cache"); check(Echoion::Standalone::resolveAvatarCacheDirectory( avatarCache, "legacy") == avatarCache / "legacy", "relative avatar directory was not rooted below state"); check(!Echoion::Standalone::resolveAvatarCacheDirectory(avatarCache, "../escape"), "traversing avatar directory was accepted"); check(!Echoion::Standalone::resolveAvatarCacheDirectory( avatarCache, tree.path() / "outside"), "absolute avatar directory outside state was accepted"); const auto avatarEscape = avatarCache / "escape"; std::error_code avatarError; std::filesystem::create_symlink(tree.path(), avatarEscape, avatarError); check(!avatarError, "failed to create avatar symlink escape fixture"); check(!Echoion::Standalone::resolveAvatarCacheDirectory(avatarCache, "escape/cache"), "avatar directory escaped state through a symbolic link"); const auto runningExecutable = Echoion::Standalone::runningExecutablePath(); check(runningExecutable.has_value() && std::filesystem::is_regular_file(*runningExecutable), "running executable could not be resolved through /proc/self/exe"); return failures == 0 ? 0 : 1; }