Files
Build-System/tests/RegistryTests.cpp
T
faumaray 663215f7f3
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 3s
VoidBuildSystem CI / signing-openssl-gcc16 (push) Failing after 49s
feat(registry): store and serve signed binary variants
2026-12-03 20:58:04 +03:00

783 lines
36 KiB
C++

#include <Void/Build/BootstrapConfig.hpp>
#include <Void/Build/Package/Archive.hpp>
#include <Void/Build/Package/Fetch.hpp>
#include <Void/Build/Platform/FileOps.hpp>
#include <Void/Build/Platform/ProcessSupervisor.hpp>
#include <Void/Build/Provenance/Signing.hpp>
#include <Void/Build/Provenance/Verification.hpp>
#include <Void/Build/Registry/HttpServer.hpp>
#include <Void/Build/Registry/RegistryService.hpp>
#include <Void/Build/State/StateStore.hpp>
#include <Void/Build/Util/Sha256.hpp>
#include <Void/Data/StrictJson.hpp>
#include <Void/System/Path.hpp>
#include <Void/System/TempDir.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <cstring>
#include <fstream>
#include <span>
using namespace Void::Build;
using namespace Void::Build::Registry;
namespace {
std::string extract_session_cookie(const std::vector<std::string> &set_cookies) {
for (const auto &sc : set_cookies) {
if (sc.starts_with("vbs_session=")) {
const auto semi = sc.find(';');
return sc.substr(0, semi);
}
}
return {};
}
std::string extract_json_str(std::string_view json, const std::string &key) {
auto parsed = Void::Json::Strict::parse(json);
if (!parsed)
return {};
const auto *obj = std::get_if<Void::Json::Strict::Value::Object>(&parsed->value);
if (!obj)
return {};
const auto it = obj->find(key);
if (it == obj->end())
return {};
const auto *s = std::get_if<std::string>(&it->second.value);
return s ? *s : std::string{};
}
std::filesystem::path locate_vbs_binary() {
std::error_code ec;
if (const auto self = std::filesystem::read_symlink("/proc/self/exe", ec);
!ec && !self.empty()) {
const auto sibling = self.parent_path() / "vbs";
if (std::filesystem::exists(sibling, ec)) {
return sibling;
}
}
if (!Bootstrap::binary_dir.empty()) {
return std::filesystem::path{Bootstrap::binary_dir} / "bin" / "vbs";
}
return {};
}
} // namespace
VOID_TEST(RegistryTests, registry_rbac_immutability_csrf_xss_web_and_backup_restore) {
Void::FileSystem::TempDir tmp{"vbs_reg_"};
RegistryConfig cfg;
cfg.data_dir = tmp / "data";
cfg.web_root = std::filesystem::path{Bootstrap::source_dir} / "registry" / "web";
cfg.bind_address = "127.0.0.1";
cfg.port = 0; // Ephemeral port!
cfg.bootstrap_admin_user = "admin";
cfg.bootstrap_admin_password = "admin_secret_password";
auto svc = RegistryService::open(cfg);
Void::Testing::expect(svc.has_value());
if (!svc)
return;
Void::Testing::expect(
(*svc)->create_user("pub_alice", "alice_pass", Role::Publisher).has_value());
Void::Testing::expect((*svc)->create_user("read_bob", "bob_pass", Role::Reader).has_value());
auto alice_tok = (*svc)->create_token("pub_alice", "ci", {"read", "publish"});
auto bob_tok = (*svc)->create_token("read_bob", "ro", {"read"});
Void::Testing::expect(alice_tok.has_value() && bob_tok.has_value());
if (!alice_tok || !bob_tok)
return;
Void::Testing::expect((*svc)->start_http().has_value());
const auto port = (*svc)->bound_port();
Void::Testing::expect(port > 0);
// 1. Static web UI assets served with security headers; path traversal blocked!
auto idx = send_loopback_http(port, "GET", "/");
Void::Testing::expect(idx.has_value() && idx->status == 200);
if (idx) {
Void::Testing::expect(idx->headers.contains("content-security-policy"));
Void::Testing::expect_eq(idx->headers.at("x-frame-options"), std::string{"DENY"});
Void::Testing::expect(idx->body.find("VoidBuildSystem Registry") != std::string::npos);
}
auto trav = send_loopback_http(port, "GET", "/../CMakeLists.txt");
Void::Testing::expect(trav.has_value() && trav->status == 403);
// 2. Build a valid deterministic package tar and publish as Publisher Alice.
const auto pkg_src = tmp / "pkg_src";
(void)Platform::atomic_write_file(pkg_src / "build.cpp",
"void build(Void::Build::Project&){}\n", false);
const auto tar1 = tmp / "p1.tar";
auto sha1 = Package::create_source_package(pkg_src, tar1, 0);
Void::Testing::expect(sha1.has_value());
auto bytes1 = Void::FileSystem::read_file(tar1);
Void::Testing::expect(bytes1.has_value());
if (!sha1 || !bytes1)
return;
// Reader Bob CANNOT publish (403)!
auto bob_pub = send_loopback_http(
port, "PUT", "/api/v1/packages/demo-pkg/1.0.0",
{{"Authorization", "Bearer " + *bob_tok}, {"X-Content-SHA256", *sha1}}, *bytes1);
Void::Testing::expect(bob_pub.has_value() && bob_pub->status == 403);
// Wrong SHA-256 rejected (400)!
auto bad_sha = send_loopback_http(
port, "PUT", "/api/v1/packages/demo-pkg/1.0.0",
{{"Authorization", "Bearer " + *alice_tok}, {"X-Content-SHA256", std::string(64, '0')}},
*bytes1);
Void::Testing::expect(bad_sha.has_value() && bad_sha->status == 400);
// Malicious non-tar / traversal upload rejected (422)!
auto bad_tar =
send_loopback_http(port, "PUT", "/api/v1/packages/demo-pkg/1.0.0",
{{"Authorization", "Bearer " + *alice_tok}}, "not a valid tar archive");
Void::Testing::expect(bad_tar.has_value() && bad_tar->status == 422);
// Valid private package publish by Alice -> 201 Created!
auto pub_ok = send_loopback_http(port, "PUT", "/api/v1/packages/secret-pkg/1.0.0",
{{"Authorization", "Bearer " + *alice_tok},
{"X-Content-SHA256", *sha1},
{"X-Package-Private", "true"},
{"X-Package-Description", "<script>alert('xss')</script>"}},
*bytes1);
Void::Testing::expect(pub_ok.has_value() && pub_ok->status == 201);
// Signature metadata is stored and served verbatim (the registry is not a trust authority).
const std::string release_signature(128, 'a');
const std::string release_public_key(64, 'b');
auto signed_pub = send_loopback_http(port, "PUT", "/api/v1/packages/signed-pkg/1.0.0",
{{"Authorization", "Bearer " + *alice_tok},
{"X-Content-SHA256", *sha1},
{"X-Package-Signature", release_signature},
{"X-Package-Public-Key", release_public_key}},
*bytes1);
Void::Testing::expect(signed_pub.has_value() && signed_pub->status == 201);
auto signed_detail = send_loopback_http(port, "GET", "/api/v1/packages/signed-pkg",
{{"Authorization", "Bearer " + *alice_tok}});
Void::Testing::expect(signed_detail.has_value() && signed_detail->status == 200);
if (signed_detail) {
Void::Testing::expect(signed_detail->body.find(release_signature) != std::string::npos);
Void::Testing::expect(signed_detail->body.find(release_public_key) != std::string::npos);
}
auto bad_sig_pub = send_loopback_http(port, "PUT", "/api/v1/packages/bad-sig-pkg/1.0.0",
{{"Authorization", "Bearer " + *alice_tok},
{"X-Content-SHA256", *sha1},
{"X-Package-Signature", std::string(127, 'a')},
{"X-Package-Public-Key", release_public_key}},
*bytes1);
Void::Testing::expect(bad_sig_pub.has_value() && bad_sig_pub->status == 400);
auto half_sig_pub = send_loopback_http(port, "PUT", "/api/v1/packages/half-sig-pkg/1.0.0",
{{"Authorization", "Bearer " + *alice_tok},
{"X-Content-SHA256", *sha1},
{"X-Package-Signature", release_signature}},
*bytes1);
Void::Testing::expect(half_sig_pub.has_value() && half_sig_pub->status == 400);
// Unauthenticated access to private package MUST fail (401)!
auto anon_priv = send_loopback_http(port, "GET", "/api/v1/packages/secret-pkg");
Void::Testing::expect(anon_priv.has_value() && anon_priv->status == 401);
auto anon_dl = send_loopback_http(port, "GET", "/api/v1/packages/secret-pkg/1.0.0/download");
Void::Testing::expect(anon_dl.has_value() && anon_dl->status == 401);
// Idempotent same-bytes republish -> 200 OK!
auto idemp = send_loopback_http(
port, "PUT", "/api/v1/packages/secret-pkg/1.0.0",
{{"Authorization", "Bearer " + *alice_tok}, {"X-Content-SHA256", *sha1}}, *bytes1);
Void::Testing::expect(idemp.has_value() && idemp->status == 200);
// Different bytes for SAME immutable release -> 409 Conflict!
(void)Platform::atomic_write_file(pkg_src / "extra.txt", "changed", false);
const auto tar2 = tmp / "p2.tar";
auto sha2 = Package::create_source_package(pkg_src, tar2, 0);
auto bytes2 = Void::FileSystem::read_file(tar2);
auto conflict = send_loopback_http(
port, "PUT", "/api/v1/packages/secret-pkg/1.0.0",
{{"Authorization", "Bearer " + *alice_tok}, {"X-Content-SHA256", *sha2}}, *bytes2);
Void::Testing::expect(conflict.has_value() && conflict->status == 409);
// 3. Browser session login (HttpOnly cookie) + CSRF rejection without header + valid CSRF yank!
auto login = send_loopback_http(
port, "POST", "/api/v1/auth/login", {{"Content-Type", "application/json"}},
"{\"username\":\"admin\",\"password\":\"admin_secret_password\"}");
Void::Testing::expect(login.has_value() && login->status == 200);
if (!login)
return;
const auto cookie = extract_session_cookie(login->set_cookies);
const auto csrf = extract_json_str(login->body, "csrf_token");
Void::Testing::expect(!cookie.empty() && !csrf.empty());
Void::Testing::expect(login->set_cookies[0].find("HttpOnly") != std::string::npos);
Void::Testing::expect(login->set_cookies[0].find("SameSite=Strict") != std::string::npos);
// Mutation with session cookie WITHOUT X-CSRF-Token -> 403 CSRF rejected!
auto no_csrf = send_loopback_http(port, "POST", "/api/v1/packages/secret-pkg/1.0.0/yank",
{{"Cookie", cookie}}, "{\"yanked\":true}");
Void::Testing::expect(no_csrf.has_value() && no_csrf->status == 403);
// Mutation WITH valid X-CSRF-Token -> 200 OK!
auto with_csrf =
send_loopback_http(port, "POST", "/api/v1/packages/secret-pkg/1.0.0/yank",
{{"Cookie", cookie}, {"X-CSRF-Token", csrf}}, "{\"yanked\":true}");
Void::Testing::expect(with_csrf.has_value() && with_csrf->status == 200);
// Audit log contains zero secrets (no passwords or bearer tokens)!
auto audit = send_loopback_http(port, "GET", "/api/v1/audit", {{"Cookie", cookie}});
Void::Testing::expect(audit.has_value() && audit->status == 200);
if (audit) {
Void::Testing::expect(audit->body.find("admin_secret_password") == std::string::npos);
Void::Testing::expect(audit->body.find(*alice_tok) == std::string::npos);
}
// 4. Consistent backup & restore into a brand-new registry instance preserves releases &
// download!
const auto backup_dir = tmp / "backup";
Void::Testing::expect((*svc)->backup_to(backup_dir).has_value());
(*svc)->stop_http();
RegistryConfig cfg2 = cfg;
cfg2.data_dir = tmp / "restored_data";
cfg2.bootstrap_admin_password.clear();
auto svc2 = RegistryService::open(cfg2);
Void::Testing::expect(svc2.has_value());
if (svc2) {
Void::Testing::expect((*svc2)->restore_from(backup_dir).has_value());
Void::Testing::expect((*svc2)->start_http().has_value());
auto dl = send_loopback_http((*svc2)->bound_port(), "GET",
"/api/v1/packages/secret-pkg/1.0.0/download",
{{"Authorization", "Bearer " + *alice_tok}});
Void::Testing::expect(dl.has_value() && dl->status == 200 && dl->body == *bytes1);
(*svc2)->stop_http();
}
}
// Track D1: shared HTTP content-addressed action cache blob/record round-trip across stores.
VOID_TEST(RegistryTests, shared_remote_action_cache_blob_and_record_roundtrip) {
Void::FileSystem::TempDir tmp{"vbs_remote_cas_"};
RegistryConfig cfg;
cfg.data_dir = tmp / "reg_data";
cfg.bind_address = "127.0.0.1";
cfg.port = 0;
cfg.bootstrap_admin_user = "admin";
cfg.bootstrap_admin_password = "admin_pw";
auto svc = RegistryService::open(cfg);
Void::Testing::expect(svc.has_value());
if (!svc) {
return;
}
Void::Testing::expect((*svc)->create_user("ci_cache", "ci_pass", Role::Publisher).has_value());
auto tok = (*svc)->create_token("ci_cache", "cas", {"read", "publish"});
Void::Testing::expect(tok.has_value());
if (!tok) {
return;
}
Void::Testing::expect((*svc)->start_http().has_value());
const auto port = (*svc)->bound_port();
State::RemoteCacheConfig remote;
remote.url = "http://127.0.0.1:" + std::to_string(port);
remote.bearer_token = *tok;
Void::FileSystem::TempDir store_tmp{"vbs_remote_store_"};
auto store = State::StateStore::open(store_tmp / ".vbs");
Void::Testing::expect(store.has_value());
if (!store) {
return;
}
store->configure_remote_cache(remote);
const auto artifact = tmp / "remote_obj.o";
(void)Platform::atomic_write_file(artifact, "REMOTE_CACHE_OBJECT_BYTES", false);
auto blob = store->store_blob_from_file(artifact);
Void::Testing::expect(blob.has_value());
if (!blob) {
return;
}
Void::Testing::expect(store->publish_remote_blob(blob->sha256).has_value());
// A fresh, empty store downloads and SHA-256 verifies the blob from the shared cache.
Void::FileSystem::TempDir store2_tmp{"vbs_remote_store2_"};
auto store2 = State::StateStore::open(store2_tmp / ".vbs");
Void::Testing::expect(store2.has_value());
if (!store2) {
return;
}
store2->configure_remote_cache(remote);
Void::Testing::expect(!store2->verify_blob(blob->sha256).value_or(true));
auto ensured = store2->ensure_remote_blob(blob->sha256);
Void::Testing::expect(ensured.has_value() && *ensured);
Void::Testing::expect(store2->verify_blob(blob->sha256).value_or(false));
// Action record round-trip by portable key.
const std::string key = Util::sha256_hex("portable-action-key");
const std::string record = "{\"schema_version\":1,\"action_key\":\"" + key + "\"}";
Void::Testing::expect(store->publish_remote_action(key, record).has_value());
auto fetched = store2->fetch_remote_action(key);
Void::Testing::expect(fetched.has_value() && fetched->has_value() && **fetched == record);
// Unauthenticated CAS reads are rejected.
auto anon = send_loopback_http(port, "GET", "/api/v1/cas/blobs/" + blob->sha256);
Void::Testing::expect(anon.has_value() && anon->status == 401);
(*svc)->stop_http();
}
// Track D1 acceptance: a cold build populates the shared CAS, then a second clean build
// directory is a 100% verified cache hit and its binary runs.
VOID_TEST(RegistryTests, shared_remote_cache_cold_populates_then_clean_build_all_hits) {
const auto vbs_bin = locate_vbs_binary();
Void::Testing::expect(!vbs_bin.empty() && std::filesystem::exists(vbs_bin));
if (vbs_bin.empty() || !std::filesystem::exists(vbs_bin)) {
return;
}
Void::FileSystem::TempDir tmp{"vbs_remote_accept_"};
RegistryConfig cfg;
cfg.data_dir = tmp / "reg_data";
cfg.bind_address = "127.0.0.1";
cfg.port = 0;
cfg.bootstrap_admin_user = "admin";
cfg.bootstrap_admin_password = "admin_pw";
auto svc = RegistryService::open(cfg);
Void::Testing::expect(svc.has_value());
if (!svc) {
return;
}
Void::Testing::expect((*svc)->create_user("ci_cache", "ci_pass", Role::Publisher).has_value());
auto tok = (*svc)->create_token("ci_cache", "accept", {"read", "publish"});
Void::Testing::expect(tok.has_value());
if (!tok) {
return;
}
Void::Testing::expect((*svc)->start_http().has_value());
const std::string reg_url = "http://127.0.0.1:" + std::to_string((*svc)->bound_port());
const auto proj = tmp / "proj";
(void)Platform::atomic_write_file(proj / "build.cpp", R"cpp(#include <Void/Build.hpp>
void build(Void::Build::Project &p) {
p.executable("cache_app").sources({"src/main.cpp"});
}
)cpp",
false);
(void)Platform::atomic_write_file(proj / "src" / "main.cpp", "int main() { return 0; }\n",
false);
auto run_build = [&](const std::filesystem::path &build_dir) {
Platform::ProcessRequest req;
req.executable = vbs_bin;
req.arguments = {"build", "--project", proj.string(),
"--build-dir", build_dir.string(), "--remote-cache",
reg_url, "--jobs", "4"};
req.environment.set_vars["VBS_REMOTE_CACHE_TOKEN"] = *tok;
return Platform::run_supervised(req);
};
auto first = run_build(tmp / "build_a");
Void::Testing::expect(first.has_value() && first->succeeded());
if (first) {
Void::Testing::expect(first->stdout_data.find("built (0 executed") == std::string::npos);
}
auto second = run_build(tmp / "build_b");
Void::Testing::expect(second.has_value() && second->succeeded());
if (second) {
Void::Testing::expect(second->stdout_data.find("built (0 executed") != std::string::npos);
}
Platform::ProcessRequest run_req;
run_req.executable = tmp / "build_b" / "profiles" / "debug" / "out" / "bin" / "cache_app";
auto run_res = Platform::run_supervised(run_req);
Void::Testing::expect(run_res.has_value() && run_res->succeeded());
(*svc)->stop_http();
}
VOID_TEST(RegistryTests, binary_variant_distribution_downloads_and_verifies) {
if (!Provenance::signing_available()) {
return;
}
Void::FileSystem::TempDir tmp{"vbs_bin_dist_"};
RegistryConfig cfg;
cfg.data_dir = tmp / "reg_data";
cfg.web_root = std::filesystem::path{Bootstrap::source_dir} / "registry" / "web";
cfg.bind_address = "127.0.0.1";
cfg.port = 0;
cfg.bootstrap_admin_user = "admin";
cfg.bootstrap_admin_password = "admin_pw";
auto svc = RegistryService::open(cfg);
Void::Testing::expect(svc.has_value());
if (!svc) {
return;
}
Void::Testing::expect((*svc)->create_user("bin_pub", "bin_pass", Role::Publisher).has_value());
auto tok = (*svc)->create_token("bin_pub", "publish", {"read", "publish"});
Void::Testing::expect(tok.has_value());
if (!tok) {
return;
}
Void::Testing::expect((*svc)->start_http().has_value());
const auto port = (*svc)->bound_port();
const std::string reg_url = "http://127.0.0.1:" + std::to_string(port);
const auto pkg_src = tmp / "bin_pkg";
(void)Platform::atomic_write_file(pkg_src / "payload.bin", "BINARY_VARIANT_PAYLOAD", false);
const auto tar = tmp / "bin.tar";
auto sha = Package::create_source_package(pkg_src, tar, 0);
Void::Testing::require(sha.has_value());
auto bytes = Void::FileSystem::read_file(tar);
Void::Testing::require(bytes.has_value());
auto published = send_loopback_http(
port, "PUT", "/api/v1/packages/demo-bin/1.0.0",
{{"Authorization", "Bearer " + *tok}, {"X-Content-SHA256", *sha}}, *bytes);
Void::Testing::require(published.has_value() && published->status == 201);
const auto seed_path = tmp / "binary.seed";
Void::Testing::require(Provenance::generate_signing_seed(seed_path).has_value());
auto public_key = Provenance::public_key_from_seed(seed_path);
Void::Testing::require(public_key.has_value());
Package::BinaryVariantMeta variant;
variant.variant_id = "demo-bin-1.0.0-linux-x86_64";
variant.os = "Linux";
variant.architecture = "x86_64";
variant.compiler_family = "gcc";
variant.compiler_abi = "gcc-16";
variant.stdlib_abi = "libstdc++";
variant.linkage = "static";
variant.lto_mode = "off";
variant.dependency_closure_hash = "closure";
variant.sha256 = *sha;
variant.download_url = reg_url + "/api/v1/packages/demo-bin/1.0.0/download";
const std::string message =
Provenance::canonical_binary_message(variant.variant_id, variant.sha256);
auto signature = Provenance::sign_message(
std::as_bytes(std::span<const char>{message.data(), message.size()}), seed_path);
Void::Testing::require(signature.has_value());
variant.signature = *signature;
variant.public_key = *public_key;
Package::BinaryCompatibilityKey key;
key.os = variant.os;
key.architecture = variant.architecture;
key.compiler_family = variant.compiler_family;
key.compiler_abi = variant.compiler_abi;
key.stdlib_abi = variant.stdlib_abi;
key.linkage = variant.linkage;
key.lto_mode = variant.lto_mode;
key.dependency_closure_hash = variant.dependency_closure_hash;
Package::BinaryFetchOptions options{tmp / "binary_cache", {*public_key}};
auto fetched = Package::materialize_binary_variant(key, {variant}, options);
Void::Testing::require(fetched.has_value());
auto downloaded =
Void::FileSystem::read_file(tmp / "binary_cache" / "binaries" / variant.sha256);
Void::Testing::require(downloaded.has_value());
Void::Testing::expect(*downloaded == *bytes);
// Cached artifacts are reused without contacting the registry.
(*svc)->stop_http();
auto cached = Package::materialize_binary_variant(key, {variant}, options);
Void::Testing::expect(cached.has_value());
// A digest mismatch fails closed instead of materializing unverified bytes.
auto tampered = variant;
tampered.sha256 = std::string(64, '0');
auto mismatched = Package::materialize_binary_variant(key, {tampered}, options);
Void::Testing::expect(!mismatched.has_value());
}
VOID_TEST(RegistryTests, registry_binary_variant_publish_download_and_materialize) {
if (!Provenance::signing_available()) {
return;
}
Void::FileSystem::TempDir tmp{"vbs_reg_binary_"};
RegistryConfig cfg;
cfg.data_dir = tmp / "reg_data";
cfg.web_root = std::filesystem::path{Bootstrap::source_dir} / "registry" / "web";
cfg.bind_address = "127.0.0.1";
cfg.port = 0;
cfg.bootstrap_admin_user = "admin";
cfg.bootstrap_admin_password = "admin_pw";
auto svc = RegistryService::open(cfg);
Void::Testing::expect(svc.has_value());
if (!svc) {
return;
}
Void::Testing::expect((*svc)->create_user("bin_pub", "bin_pass", Role::Publisher).has_value());
auto tok = (*svc)->create_token("bin_pub", "publish", {"read", "publish"});
Void::Testing::expect(tok.has_value());
if (!tok) {
return;
}
Void::Testing::expect((*svc)->start_http().has_value());
const auto port = (*svc)->bound_port();
const std::string reg_url = "http://127.0.0.1:" + std::to_string(port);
// The binary variant belongs to an existing source release.
const auto pkg_src = tmp / "src_pkg";
(void)Platform::atomic_write_file(pkg_src / "payload.txt", "SOURCE_PAYLOAD", false);
const auto tar = tmp / "src.tar";
auto source_sha = Package::create_source_package(pkg_src, tar, 0);
Void::Testing::require(source_sha.has_value());
auto source_bytes = Void::FileSystem::read_file(tar);
Void::Testing::require(source_bytes.has_value());
auto src_pub = send_loopback_http(
port, "PUT", "/api/v1/packages/demo-bin/1.0.0",
{{"Authorization", "Bearer " + *tok}, {"X-Content-SHA256", *source_sha}}, *source_bytes);
Void::Testing::require(src_pub.has_value() && src_pub->status == 201);
const std::string artifact = "PREBUILT_BINARY_VARIANT_BYTES";
const std::string artifact_sha = Util::sha256_hex(artifact);
const std::string variant_id = "linux-x86_64-gcc16-static";
const std::string binary_path = "/api/v1/packages/demo-bin/1.0.0/binaries/" + variant_id;
auto unauthorized = send_loopback_http(port, "PUT", binary_path,
{{"X-Content-SHA256", artifact_sha}}, artifact);
Void::Testing::expect(unauthorized.has_value() && unauthorized->status == 401);
const auto seed_path = tmp / "binary.seed";
Void::Testing::require(Provenance::generate_signing_seed(seed_path).has_value());
auto public_key = Provenance::public_key_from_seed(seed_path);
Void::Testing::require(public_key.has_value());
const std::string message = Provenance::canonical_binary_message(variant_id, artifact_sha);
auto signature = Provenance::sign_message(
std::as_bytes(std::span<const char>{message.data(), message.size()}), seed_path);
Void::Testing::require(signature.has_value());
auto bad_signature = send_loopback_http(port, "PUT", binary_path,
{{"Authorization", "Bearer " + *tok},
{"X-Content-SHA256", artifact_sha},
{"X-Binary-Signature", std::string(127, 'a')},
{"X-Binary-Public-Key", *public_key}},
artifact);
Void::Testing::expect(bad_signature.has_value() && bad_signature->status == 400);
auto published = send_loopback_http(port, "PUT", binary_path,
{{"Authorization", "Bearer " + *tok},
{"X-Content-SHA256", artifact_sha},
{"X-Binary-Signature", *signature},
{"X-Binary-Public-Key", *public_key}},
artifact);
Void::Testing::expect(published.has_value() && published->status == 201);
auto detail = send_loopback_http(port, "GET", "/api/v1/packages/demo-bin");
Void::Testing::expect(detail.has_value() && detail->status == 200);
if (detail) {
Void::Testing::expect(detail->body.find(variant_id) != std::string::npos);
Void::Testing::expect(detail->body.find(artifact_sha) != std::string::npos);
}
auto downloaded = send_loopback_http(port, "GET", binary_path + "/download");
Void::Testing::expect(downloaded.has_value() && downloaded->status == 200);
if (downloaded) {
Void::Testing::expect(downloaded->body == artifact);
}
auto conflict = send_loopback_http(
port, "PUT", binary_path,
{{"Authorization", "Bearer " + *tok}, {"X-Content-SHA256", Util::sha256_hex("different")}},
"different");
Void::Testing::expect(conflict.has_value() && conflict->status == 409);
// The registry-served artifact materializes through the signed binary-variant path.
Package::BinaryVariantMeta variant;
variant.variant_id = variant_id;
variant.os = "Linux";
variant.architecture = "x86_64";
variant.compiler_family = "gcc";
variant.compiler_abi = "gcc-16";
variant.stdlib_abi = "libstdc++";
variant.linkage = "static";
variant.lto_mode = "off";
variant.dependency_closure_hash = "closure";
variant.sha256 = artifact_sha;
variant.download_url = reg_url + binary_path + "/download";
variant.signature = *signature;
variant.public_key = *public_key;
Package::BinaryCompatibilityKey key;
key.os = variant.os;
key.architecture = variant.architecture;
key.compiler_family = variant.compiler_family;
key.compiler_abi = variant.compiler_abi;
key.stdlib_abi = variant.stdlib_abi;
key.linkage = variant.linkage;
key.lto_mode = variant.lto_mode;
key.dependency_closure_hash = variant.dependency_closure_hash;
Package::BinaryFetchOptions options{tmp / "binary_cache", {*public_key}};
auto materialized = Package::materialize_binary_variant(key, {variant}, options);
Void::Testing::require(materialized.has_value());
auto cached = Void::FileSystem::read_file(tmp / "binary_cache" / "binaries" / artifact_sha);
Void::Testing::require(cached.has_value());
Void::Testing::expect(*cached == artifact);
(*svc)->stop_http();
}
// End-to-end live client <-> server test: `vbs publish` -> `vbs deps resolve` -> `vbs build
// --locked`!
VOID_TEST(RegistryTests, cli_publish_deps_resolve_and_locked_build_against_live_registry) {
const auto vbs_bin = locate_vbs_binary();
Void::Testing::expect(!vbs_bin.empty() && std::filesystem::exists(vbs_bin));
if (vbs_bin.empty() || !std::filesystem::exists(vbs_bin)) {
return;
}
Void::FileSystem::TempDir tmp{"vbs_reg_e2e_"};
RegistryConfig cfg;
cfg.data_dir = tmp / "reg_data";
cfg.web_root = std::filesystem::path{Bootstrap::source_dir} / "registry" / "web";
cfg.bind_address = "127.0.0.1";
cfg.port = 0;
cfg.bootstrap_admin_user = "admin";
cfg.bootstrap_admin_password = "admin_e2e_password";
auto svc = RegistryService::open(cfg);
Void::Testing::expect(svc.has_value());
if (!svc)
return;
Void::Testing::expect((*svc)->create_user("ci_pub", "ci_pass", Role::Publisher).has_value());
auto tok = (*svc)->create_token("ci_pub", "publish", {"read", "publish"});
Void::Testing::expect(tok.has_value());
if (!tok)
return;
Void::Testing::expect((*svc)->start_http().has_value());
const std::string reg_url = "http://127.0.0.1:" + std::to_string((*svc)->bound_port());
// 1. Create a real CMake library package and publish 1.0.0 and 1.1.0 via `vbs publish` CLI!
const auto pkg_dir = tmp / "hello_pkg";
(void)Platform::atomic_write_file(pkg_dir / "CMakeLists.txt", R"cmake(
cmake_minimum_required(VERSION 3.20)
project(hello_pkg LANGUAGES CXX)
add_library(hello_pkg STATIC src/hello.cpp)
target_include_directories(hello_pkg PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
)cmake",
false);
(void)Platform::atomic_write_file(pkg_dir / "include" / "hello_pkg.hpp",
"#pragma once\nint hello_pkg_value();\n", false);
(void)Platform::atomic_write_file(
pkg_dir / "src" / "hello.cpp",
"#include \"hello_pkg.hpp\"\nint hello_pkg_value() { return 10; }\n", false);
Platform::ProcessRequest pub1;
pub1.executable = vbs_bin;
pub1.arguments = {"publish", "--project", pkg_dir.string(), "--registry",
reg_url, "--name", "hello-pkg", "--version",
"1.0.0", "--description", "Live E2E package"};
pub1.environment.set_vars["VBS_REGISTRY_TOKEN"] = *tok;
auto r1 = Platform::run_supervised(pub1);
Void::Testing::expect(r1.has_value() && r1->succeeded());
// Publish 1.1.0 returning 42 so solver (^1.0) selects 1.1.0!
(void)Platform::atomic_write_file(
pkg_dir / "src" / "hello.cpp",
"#include \"hello_pkg.hpp\"\nint hello_pkg_value() { return 42; }\n", false);
Platform::ProcessRequest pub2 = pub1;
pub2.arguments[8] = "1.1.0";
auto r2 = Platform::run_supervised(pub2);
Void::Testing::expect(r2.has_value() && r2->succeeded());
// 2. Create a consumer project declaring `.package("hello-pkg", "^1.0").registry(reg_url)`.
const auto cons_dir = tmp / "consumer";
const auto cons_build = tmp / "consumer_build";
(void)Platform::atomic_write_file(cons_dir / "build.cpp",
"#include <Void/Build.hpp>\n"
"void build(Void::Build::Project &p) {\n"
" auto dep = p.dependency(\"hello\")\n"
" .package(\"hello-pkg\", \"^1.0\")\n"
" .registry(\"" +
reg_url +
"\");\n"
" p.executable(\"reg_consumer\")\n"
" .sources({\"src/main.cpp\"})\n"
" .link(dep.target(\"hello_pkg\"));\n"
"}\n",
false);
(void)Platform::atomic_write_file(
cons_dir / "src" / "main.cpp",
"#include \"hello_pkg.hpp\"\nint main() { return hello_pkg_value() == 42 ? 0 : 1; }\n",
false);
// 3. Resolve dependencies into vbs.lock against the live registry!
Platform::ProcessRequest deps_req;
deps_req.executable = vbs_bin;
deps_req.arguments = {"deps", "resolve", "--project", cons_dir.string(),
"--build-dir", cons_build.string()};
deps_req.environment.set_vars["VBS_REGISTRY_TOKEN"] = *tok;
auto deps_res = Platform::run_supervised(deps_req);
Void::Testing::expect(deps_res.has_value() && deps_res->succeeded());
auto lock_text = Void::FileSystem::read_file(cons_dir / "vbs.lock");
Void::Testing::expect(lock_text.has_value());
if (lock_text) {
Void::Testing::expect(lock_text->find("\"version\":\"1.1.0\"") != std::string::npos);
Void::Testing::expect(lock_text->find("\"source_kind\":\"package\"") != std::string::npos);
Void::Testing::expect(lock_text->find(*tok) == std::string::npos); // Zero token leaks!
}
// 4. Build in --locked mode and execute the consumer binary!
Platform::ProcessRequest bld_req;
bld_req.executable = vbs_bin;
bld_req.arguments = {"build", "--project", cons_dir.string(),
"--build-dir", cons_build.string(), "--locked"};
auto bld_res = Platform::run_supervised(bld_req);
Void::Testing::expect(bld_res.has_value() && bld_res->succeeded());
Platform::ProcessRequest run_req;
run_req.executable = cons_build / "profiles" / "debug" / "out" / "bin" / "reg_consumer";
auto run_res = Platform::run_supervised(run_req);
Void::Testing::expect(run_res.has_value() && run_res->succeeded());
// 5. Requiring a trusted key must fail closed for the unsigned locked package.
Platform::ProcessRequest trusted_req = bld_req;
trusted_req.arguments.push_back("--trusted-public-key");
trusted_req.arguments.push_back(std::string(64, 'a'));
auto trusted_res = Platform::run_supervised(trusted_req);
Void::Testing::expect(trusted_res.has_value() && !trusted_res->succeeded());
if (trusted_res) {
Void::Testing::expect(trusted_res->stderr_data.find("not signed by a trusted key") !=
std::string::npos);
}
// 6. With the OpenSSL backend, publish a signed release and require its trusted key.
if (Provenance::signing_available()) {
const auto seed_path = tmp / "publish.seed";
Void::Testing::require(Provenance::generate_signing_seed(seed_path).has_value());
auto public_key = Provenance::public_key_from_seed(seed_path);
Void::Testing::require(public_key.has_value());
(void)Platform::atomic_write_file(
pkg_dir / "src" / "hello.cpp",
"#include \"hello_pkg.hpp\"\nint hello_pkg_value() { return 7; }\n", false);
Platform::ProcessRequest pub3 = pub1;
pub3.arguments[8] = "1.2.0";
pub3.arguments.push_back("--signing-key");
pub3.arguments.push_back(seed_path.string());
auto r3 = Platform::run_supervised(pub3);
Void::Testing::expect(r3.has_value() && r3->succeeded());
Platform::ProcessRequest update_req = deps_req;
update_req.arguments[1] = "update";
auto update_res = Platform::run_supervised(update_req);
Void::Testing::expect(update_res.has_value() && update_res->succeeded());
Platform::ProcessRequest signed_build = bld_req;
signed_build.arguments.push_back("--trusted-public-key");
signed_build.arguments.push_back(*public_key);
auto signed_res = Platform::run_supervised(signed_build);
Void::Testing::expect(signed_res.has_value() && signed_res->succeeded());
Platform::ProcessRequest wrong_key_build = bld_req;
wrong_key_build.arguments.push_back("--trusted-public-key");
wrong_key_build.arguments.push_back(std::string(64, 'a'));
auto wrong_key_res = Platform::run_supervised(wrong_key_build);
Void::Testing::expect(wrong_key_res.has_value() && !wrong_key_res->succeeded());
}
(*svc)->stop_http();
}