// Installed-files manifest: installer-written persistence files whose content // still matches are recognized; anything else is not. #include "strawberry/hash_utils.hpp" #include "strawberry/installed_files.hpp" #include #include #include #include #include #include namespace fs = std::filesystem; using strawberry::InstalledFiles; int main() { const fs::path root = fs::temp_directory_path() / ("sb-installed-" + std::to_string(::getpid())); fs::remove_all(root); fs::create_directories(root / "units"); const std::string unit = (root / "units" / "strawberry.service").string(); std::ofstream(unit) << "[Unit]\nDescription=Strawberry\n"; const std::string hash = strawberry::HashUtils::sha256_file(unit); const std::string manifest = (root / "installed-files.sha256").string(); { std::ofstream m(manifest); m << hash << " " << unit << "\n"; m << "not-a-hash /etc/x\n"; // ignored m << std::string(64, 'a') << " relative/path\n"; // ignored: not absolute m << std::string(64, 'b') << " *" << (root / "units" / "other.timer").string() << "\n"; // binary-mode form } InstalledFiles files(manifest, /*require_root_owned=*/false); assert(files.size() == 2); assert(files.matches(unit)); assert(!files.matches((root / "units" / "unlisted.service").string())); assert(!files.matches((root / "units" / "other.timer").string())); // listed, file missing // Content changed after install: no longer the shipped file. std::ofstream(unit, std::ios::app) << "ExecStart=/tmp/implant\n"; assert(!files.matches(unit)); // A manifest that is not root-owned is ignored when root ownership is // required (the daemon runs as root); tests run unprivileged. if (::geteuid() != 0) { InstalledFiles strict(manifest, /*require_root_owned=*/true); assert(strict.size() == 0); } InstalledFiles missing((root / "nope").string(), false); assert(missing.size() == 0 && !missing.matches(unit)); fs::remove_all(root); std::cout << "installed files: PASSED" << std::endl; return 0; }