Files
EchoionStandalone/tests/test_release_manifest.cpp
faumaray 11850b1a8d Update
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-16 19:59:57 +03:00

290 lines
11 KiB
C++

#include "echoion/standalone/ReleaseManifest.hpp"
#include <Void/System/Path.hpp>
#include <Void/System/TempDir.hpp>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <string_view>
#include <sys/stat.h>
#include <vector>
namespace
{
constexpr std::string_view kAbcSha256 =
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad";
std::string fileEntry(std::string_view path, std::uintmax_t size,
std::string_view mode,
std::string_view sha256 = kAbcSha256)
{
return "{\"path\":\"" + std::string(path) +
"\",\"type\":\"file\",\"size\":" + std::to_string(size) +
",\"mode\":\"" + std::string(mode) + "\",\"sha256\":\"" +
std::string(sha256) + "\"}";
}
std::string symlinkEntry(std::string_view path, std::string_view target)
{
return "{\"path\":\"" + std::string(path) +
"\",\"type\":\"symlink\",\"target\":\"" + std::string(target) +
"\"}";
}
std::string manifest(std::vector<std::string> entries,
std::string_view executable = "bin/echoiond",
std::string_view core = "lib/Echoion.ts")
{
std::string files;
for (const auto& entry : entries) {
if (!files.empty()) {
files += ',';
}
files += entry;
}
return "{\"schemaVersion\":2,\"version\":\"1.2.3\","
"\"executable\":\"" +
std::string(executable) + "\",\"core\":\"" + std::string(core) +
"\",\"files\":[" + files + "]}";
}
bool writeFile(const std::filesystem::path& path, std::string_view contents,
std::filesystem::perms permissions)
{
std::filesystem::create_directories(path.parent_path());
if (auto result = Void::FileSystem::atomic_write(path, contents);
!result) {
std::cerr << result.error().message() << '\n';
return false;
}
std::error_code error;
std::filesystem::permissions(
path, permissions, std::filesystem::perm_options::replace, error);
if (error) {
std::cerr << error.message() << '\n';
return false;
}
return true;
}
} // namespace
int main()
{
int failures{};
const auto check = [&failures](bool condition, std::string_view message) {
if (!condition) {
std::cerr << message << '\n';
++failures;
}
};
auto directory = Void::FileSystem::TempDir::open("echoion-manifest-test");
if (!directory) {
std::cerr << directory.error().message() << '\n';
return 1;
}
const auto root = directory->path();
const auto manifestPath = root / "manifest.json";
const auto executablePath = root / "bin" / "echoiond";
const auto corePath = root / "lib" / "Echoion.ts";
const auto webPath = root / "share" / "echoion" / "web" / "index.html";
const auto servicePath = root / "service" / "echoion.service.in";
const auto aliasPath = root / "lib" / "libEchoionCore.so";
const auto otherPath = root / "lib" / "Other.so";
const auto executableMode = std::filesystem::perms::owner_read |
std::filesystem::perms::owner_write |
std::filesystem::perms::owner_exec |
std::filesystem::perms::group_read |
std::filesystem::perms::group_exec |
std::filesystem::perms::others_read |
std::filesystem::perms::others_exec;
const auto regularMode = std::filesystem::perms::owner_read |
std::filesystem::perms::owner_write |
std::filesystem::perms::group_read |
std::filesystem::perms::others_read;
if (!writeFile(executablePath, "abc", executableMode) ||
!writeFile(corePath, "abc", regularMode) ||
!writeFile(webPath, "abc", regularMode) ||
!writeFile(servicePath, "abc", regularMode) ||
!writeFile(otherPath, "abc", regularMode)) {
return 1;
}
std::error_code error;
std::filesystem::create_symlink("Echoion.ts", aliasPath, error);
if (error) {
std::cerr << error.message() << '\n';
return 1;
}
const auto validManifest = manifest({
fileEntry("bin/echoiond", 3, "0755"),
fileEntry("lib/Echoion.ts", 3, "0644"),
fileEntry("lib/Other.so", 3, "0644"),
fileEntry("service/echoion.service.in", 3, "0644"),
fileEntry("share/echoion/web/index.html", 3, "0644"),
symlinkEntry("lib/libEchoionCore.so", "Echoion.ts"),
});
check(
Void::FileSystem::atomic_write(manifestPath, validManifest).has_value(),
"failed to write valid manifest");
auto loaded = Echoion::Standalone::ReleaseManifest::load(manifestPath);
check(loaded.has_value(), "valid v2 manifest was rejected while loading");
if (loaded) {
check(loaded->validate().has_value(),
"valid v2 manifest payload was rejected");
check(loaded->validateInstallLayout().has_value(),
"valid canonical install layout was rejected");
const auto undeclaredFile = root / "lib" / "undeclared.so";
check(writeFile(undeclaredFile, "abc", regularMode),
"failed to create undeclared regular-file fixture");
check(!loaded->validate(),
"undeclared regular file in immutable payload was accepted");
std::filesystem::remove(undeclaredFile, error);
const auto undeclaredLink = root / "lib" / "undeclared-link.so";
std::filesystem::create_symlink("Echoion.ts", undeclaredLink, error);
check(!error, "failed to create undeclared symbolic-link fixture");
check(!loaded->validate(),
"undeclared symbolic link in immutable payload was accepted");
std::filesystem::remove(undeclaredLink, error);
const auto specialFile = root / "lib" / "unexpected.fifo";
check(::mkfifo(specialFile.c_str(), 0600) == 0,
"failed to create special-file fixture");
check(!loaded->validate(),
"special file in immutable payload was accepted");
std::filesystem::remove(specialFile, error);
check(writeFile(root / "data" / "mutable-state", "abc", regularMode),
"failed to create mutable-root fixture");
check(loaded->validate().has_value(),
"mutable file outside immutable payload roots was rejected");
std::filesystem::permissions(root / "lib", std::filesystem::perms::all,
std::filesystem::perm_options::replace,
error);
check(!error, "failed to make immutable directory writable");
check(!loaded->validate(),
"world-writable immutable directory was accepted");
std::filesystem::permissions(root / "lib",
std::filesystem::perms::owner_all |
std::filesystem::perms::group_read |
std::filesystem::perms::group_exec |
std::filesystem::perms::others_read |
std::filesystem::perms::others_exec,
std::filesystem::perm_options::replace,
error);
check(!error, "failed to restore immutable directory mode");
}
const auto expectLoadFailure = [&](const std::string& contents,
std::string_view message) {
check(
Void::FileSystem::atomic_write(manifestPath, contents).has_value(),
"failed to write invalid manifest fixture");
check(!Echoion::Standalone::ReleaseManifest::load(manifestPath),
message);
};
expectLoadFailure(manifest({fileEntry("../echoiond", 3, "0755"),
fileEntry("lib/Echoion.ts", 3, "0644")},
"../echoiond"),
"path traversal manifest was accepted");
expectLoadFailure(manifest({fileEntry("bin/echoiond", 3, "0755"),
fileEntry("bin/echoiond", 3, "0755"),
fileEntry("lib/Echoion.ts", 3, "0644")}),
"duplicate payload path was accepted");
expectLoadFailure(
manifest({fileEntry("bin/echoiond", 3, "0755"),
fileEntry("lib/Echoion.ts", 3, "0644"),
symlinkEntry("lib/escape.so", "../outside.so")}),
"traversing symbolic link target was accepted");
expectLoadFailure(manifest({fileEntry("bin/echoiond", 3, "0777"),
fileEntry("lib/Echoion.ts", 3, "0644")}),
"world-writable executable metadata was accepted");
expectLoadFailure(manifest({fileEntry("bin/echoiond", 3, "0755"),
fileEntry("lib/Echoion.ts", 3, "0666")}),
"world-writable non-executable metadata was accepted");
check(writeFile(root / "outside.dat", "abc", regularMode),
"failed to create outside-root manifest fixture");
const auto outsideManifest = manifest({
fileEntry("bin/echoiond", 3, "0755"),
fileEntry("lib/Echoion.ts", 3, "0644"),
fileEntry("lib/Other.so", 3, "0644"),
fileEntry("service/echoion.service.in", 3, "0644"),
fileEntry("share/echoion/web/index.html", 3, "0644"),
symlinkEntry("lib/libEchoionCore.so", "Echoion.ts"),
fileEntry("outside.dat", 3, "0644"),
});
check(Void::FileSystem::atomic_write(manifestPath, outsideManifest)
.has_value(),
"failed to write outside-root manifest");
const auto outside =
Echoion::Standalone::ReleaseManifest::load(manifestPath);
check(outside.has_value() && !outside->validate(),
"manifest entry outside immutable roots was accepted");
expectLoadFailure("{\"schemaVersion\":3,\"version\":\"future\"}",
"unsupported schema version was accepted");
check(
Void::FileSystem::atomic_write(manifestPath, validManifest).has_value(),
"failed to restore valid manifest");
loaded = Echoion::Standalone::ReleaseManifest::load(manifestPath);
if (!loaded) {
std::cerr << "could not reload valid manifest\n";
return 1;
}
{
std::ofstream output(corePath, std::ios::trunc);
output << "xyz";
}
check(!loaded->validate(), "tampered payload hash was accepted");
check(writeFile(corePath, "abc", regularMode),
"failed to restore Core fixture");
std::filesystem::permissions(corePath, std::filesystem::perms::owner_read,
std::filesystem::perm_options::replace, error);
check(!error, "failed to modify Core permissions fixture");
check(!loaded->validate(), "payload mode mismatch was accepted");
std::filesystem::permissions(corePath, regularMode,
std::filesystem::perm_options::replace, error);
std::filesystem::remove(aliasPath, error);
std::filesystem::create_symlink("Other.so", aliasPath, error);
check(!error, "failed to replace symbolic link fixture");
check(!loaded->validate(), "symbolic link target mismatch was accepted");
std::filesystem::remove(aliasPath, error);
check(writeFile(aliasPath, "abc", regularMode),
"failed to create wrong-type fixture");
check(!loaded->validate(), "unexpected regular-file type was accepted");
std::filesystem::remove(aliasPath, error);
std::filesystem::create_symlink("Echoion.ts", aliasPath, error);
std::filesystem::remove(webPath, error);
check(!loaded->validate(), "missing payload entry was accepted");
check(writeFile(webPath, "abc", regularMode),
"failed to restore web fixture");
const std::string legacyManifest =
"{\"version\":\"legacy\",\"executable\":\"bin/echoiond\","
"\"core\":\"lib/Echoion.ts\",\"sha256\":{\"executable\":\"" +
std::string(kAbcSha256) + "\",\"core\":\"" + std::string(kAbcSha256) +
"\"}}";
check(Void::FileSystem::atomic_write(manifestPath, legacyManifest)
.has_value(),
"failed to write legacy manifest");
auto legacy = Echoion::Standalone::ReleaseManifest::load(manifestPath);
check(legacy.has_value(), "legacy release manifest was rejected");
if (legacy) {
check(legacy->validate().has_value(),
"legacy release payload was rejected");
check(!legacy->validateInstallLayout(),
"legacy manifest was accepted for full install validation");
}
return failures == 0 ? 0 : 1;
}