feat(provenance,daemon): add Ed25519 signing primitives and bounded daemon describe op
This commit is contained in:
1 parent
0bde74f739
commit
21babb8c31
16 files changed
+1232
-20
No files matched your search
@@ -4,15 +4,18 @@
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
#include <thread>
|
||||
#include <utility>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
|
||||
@@ -63,6 +66,42 @@ std::string str_field(const JsonObject &object, std::string_view name) {
|
||||
return text != nullptr ? *text : std::string{};
|
||||
}
|
||||
|
||||
std::optional<JsonObject>
|
||||
request_object(const std::filesystem::path &socket, std::string_view request_json,
|
||||
std::chrono::milliseconds timeout = std::chrono::seconds{120}) {
|
||||
auto response = Void::Build::Daemon::request(socket, request_json, timeout);
|
||||
if (!response) {
|
||||
return std::nullopt;
|
||||
}
|
||||
auto parsed = parse_json(*response);
|
||||
if (!parsed) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const auto *object = as_object(*parsed);
|
||||
if (object == nullptr) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return *object;
|
||||
}
|
||||
|
||||
std::vector<std::string> string_array_field(const JsonObject &object, std::string_view name) {
|
||||
std::vector<std::string> result;
|
||||
const auto *value = field(object, name);
|
||||
if (value == nullptr) {
|
||||
return result;
|
||||
}
|
||||
const auto *array = std::get_if<JsonValue::Array>(&value->value);
|
||||
if (array == nullptr) {
|
||||
return result;
|
||||
}
|
||||
for (const auto &entry : *array) {
|
||||
if (const auto *text = std::get_if<std::string>(&entry.value)) {
|
||||
result.push_back(*text);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool wait_for_path(const std::filesystem::path &path, std::chrono::milliseconds timeout) {
|
||||
const auto deadline = std::chrono::steady_clock::now() + timeout;
|
||||
while (std::chrono::steady_clock::now() < deadline) {
|
||||
@@ -180,3 +219,220 @@ VOID_TEST(Daemon, second_create_reports_already_running) {
|
||||
Void::Testing::expect(run_result.has_value());
|
||||
Void::Testing::expect(!std::filesystem::exists(socket));
|
||||
}
|
||||
|
||||
VOID_TEST(Daemon, describe_evaluates_project_graph) {
|
||||
auto temp = Void::FileSystem::TempDir::open("vbs_daemon_");
|
||||
Void::Testing::require(temp.has_value());
|
||||
const auto options = make_options(temp->path());
|
||||
|
||||
{
|
||||
std::ofstream descriptor{options.project_root / "build.cpp"};
|
||||
descriptor << R"cpp(#include <Void/Build.hpp>
|
||||
|
||||
void build(Void::Build::Project &project) {
|
||||
auto app = project.executable("app");
|
||||
app.sources({"main.cpp"});
|
||||
}
|
||||
)cpp";
|
||||
Void::Testing::require(descriptor.good());
|
||||
}
|
||||
{
|
||||
std::ofstream source{options.project_root / "main.cpp"};
|
||||
source << "int main() { return 0; }\n";
|
||||
Void::Testing::require(source.good());
|
||||
}
|
||||
|
||||
auto server = Void::Build::Daemon::DaemonServer::create(options);
|
||||
Void::Testing::require(server.has_value());
|
||||
const auto socket = server->socket_path();
|
||||
|
||||
Void::Result::Result<void, Void::Result::Error> run_result;
|
||||
std::jthread worker{[&] { run_result = server->run(); }};
|
||||
Void::Testing::require(wait_for_path(socket, std::chrono::seconds{5}));
|
||||
|
||||
auto describe =
|
||||
Void::Build::Daemon::request(socket, R"({"op":"describe"})", std::chrono::seconds{120});
|
||||
Void::Testing::require(describe.has_value());
|
||||
const auto describe_json = parse_json(*describe);
|
||||
Void::Testing::require(describe_json.has_value());
|
||||
const auto *describe_object = as_object(*describe_json);
|
||||
Void::Testing::require(describe_object != nullptr);
|
||||
Void::Testing::expect(bool_field(*describe_object, "ok"));
|
||||
Void::Testing::expect_eq(str_field(*describe_object, "op"), std::string{"describe"});
|
||||
Void::Testing::expect_eq(str_field(*describe_object, "profile"), std::string{"debug"});
|
||||
Void::Testing::expect(int_field(*describe_object, "targets") >= 1);
|
||||
Void::Testing::expect(int_field(*describe_object, "actions") >= 1);
|
||||
const auto target_names = string_array_field(*describe_object, "target_names");
|
||||
Void::Testing::expect(std::ranges::find(target_names, std::string{"app"}) !=
|
||||
target_names.end());
|
||||
|
||||
auto bad_profile = request_object(socket, R"({"op":"describe","profile":"does-not-exist"})");
|
||||
Void::Testing::require(bad_profile.has_value());
|
||||
Void::Testing::expect(!bool_field(*bad_profile, "ok", true));
|
||||
Void::Testing::expect(field(*bad_profile, "error") != nullptr);
|
||||
|
||||
auto numeric_profile = request_object(socket, R"({"op":"describe","profile":123})");
|
||||
Void::Testing::require(numeric_profile.has_value());
|
||||
Void::Testing::expect(!bool_field(*numeric_profile, "ok", true));
|
||||
Void::Testing::expect(str_field(*numeric_profile, "error").find("must be a string") !=
|
||||
std::string::npos);
|
||||
|
||||
auto empty_profile = request_object(socket, R"({"op":"describe","profile":""})");
|
||||
Void::Testing::require(empty_profile.has_value());
|
||||
Void::Testing::expect(!bool_field(*empty_profile, "ok", true));
|
||||
Void::Testing::expect(str_field(*empty_profile, "error").find("must not be empty") !=
|
||||
std::string::npos);
|
||||
|
||||
const std::string oversized_profile_request{R"({"op":"describe","profile":")" +
|
||||
std::string(129, 'p') + "\"}"};
|
||||
auto oversized_profile = request_object(socket, oversized_profile_request);
|
||||
Void::Testing::require(oversized_profile.has_value());
|
||||
Void::Testing::expect(!bool_field(*oversized_profile, "ok", true));
|
||||
Void::Testing::expect(str_field(*oversized_profile, "error").find("too long") !=
|
||||
std::string::npos);
|
||||
|
||||
auto numeric_compiler = request_object(socket, R"({"op":"describe","compiler":5})");
|
||||
Void::Testing::require(numeric_compiler.has_value());
|
||||
Void::Testing::expect(!bool_field(*numeric_compiler, "ok", true));
|
||||
Void::Testing::expect(str_field(*numeric_compiler, "error").find("must be a string") !=
|
||||
std::string::npos);
|
||||
|
||||
auto invalid_compiler = request_object(socket, R"({"op":"describe","compiler":"msvc"})");
|
||||
Void::Testing::require(invalid_compiler.has_value());
|
||||
Void::Testing::expect(!bool_field(*invalid_compiler, "ok", true));
|
||||
Void::Testing::expect(str_field(*invalid_compiler, "error").find("unknown compiler") !=
|
||||
std::string::npos);
|
||||
|
||||
auto shutdown = Void::Build::Daemon::request(socket, R"({"op":"shutdown"})");
|
||||
Void::Testing::expect(shutdown.has_value());
|
||||
worker.join();
|
||||
Void::Testing::expect(run_result.has_value());
|
||||
Void::Testing::expect(!std::filesystem::exists(socket));
|
||||
}
|
||||
|
||||
VOID_TEST(Daemon, describe_empty_project_reports_zero_targets) {
|
||||
auto temp = Void::FileSystem::TempDir::open("vbs_daemon_");
|
||||
Void::Testing::require(temp.has_value());
|
||||
const auto options = make_options(temp->path());
|
||||
|
||||
{
|
||||
std::ofstream descriptor{options.project_root / "build.cpp"};
|
||||
descriptor << R"cpp(#include <Void/Build.hpp>
|
||||
|
||||
void build(Void::Build::Project &project) {}
|
||||
)cpp";
|
||||
Void::Testing::require(descriptor.good());
|
||||
}
|
||||
|
||||
auto server = Void::Build::Daemon::DaemonServer::create(options);
|
||||
Void::Testing::require(server.has_value());
|
||||
const auto socket = server->socket_path();
|
||||
|
||||
Void::Result::Result<void, Void::Result::Error> run_result;
|
||||
std::jthread worker{[&] { run_result = server->run(); }};
|
||||
Void::Testing::require(wait_for_path(socket, std::chrono::seconds{5}));
|
||||
|
||||
auto describe = request_object(socket, R"({"op":"describe"})");
|
||||
Void::Testing::require(describe.has_value());
|
||||
Void::Testing::expect(bool_field(*describe, "ok"));
|
||||
Void::Testing::expect_eq(int_field(*describe, "targets"), std::int64_t{0});
|
||||
Void::Testing::expect_eq(int_field(*describe, "actions"), std::int64_t{0});
|
||||
Void::Testing::expect(string_array_field(*describe, "target_names").empty());
|
||||
Void::Testing::expect(field(*describe, "target_names_truncated") == nullptr);
|
||||
|
||||
auto shutdown = Void::Build::Daemon::request(socket, R"({"op":"shutdown"})");
|
||||
Void::Testing::expect(shutdown.has_value());
|
||||
worker.join();
|
||||
Void::Testing::expect(run_result.has_value());
|
||||
Void::Testing::expect(!std::filesystem::exists(socket));
|
||||
}
|
||||
|
||||
VOID_TEST(Daemon, describe_bounds_target_names_for_large_projects) {
|
||||
auto temp = Void::FileSystem::TempDir::open("vbs_daemon_");
|
||||
Void::Testing::require(temp.has_value());
|
||||
const auto options = make_options(temp->path());
|
||||
|
||||
{
|
||||
std::ofstream descriptor{options.project_root / "build.cpp"};
|
||||
descriptor << R"cpp(#include <string>
|
||||
#include <Void/Build.hpp>
|
||||
|
||||
void build(Void::Build::Project &project) {
|
||||
for (int i = 0; i < 140; ++i) {
|
||||
auto target = project.executable(std::to_string(i) + std::string(240, 'a'));
|
||||
target.sources({"main.cpp"});
|
||||
}
|
||||
}
|
||||
)cpp";
|
||||
Void::Testing::require(descriptor.good());
|
||||
}
|
||||
{
|
||||
std::ofstream source{options.project_root / "main.cpp"};
|
||||
source << "int main() { return 0; }\n";
|
||||
Void::Testing::require(source.good());
|
||||
}
|
||||
|
||||
auto server = Void::Build::Daemon::DaemonServer::create(options);
|
||||
Void::Testing::require(server.has_value());
|
||||
const auto socket = server->socket_path();
|
||||
|
||||
Void::Result::Result<void, Void::Result::Error> run_result;
|
||||
std::jthread worker{[&] { run_result = server->run(); }};
|
||||
Void::Testing::require(wait_for_path(socket, std::chrono::seconds{5}));
|
||||
|
||||
auto raw =
|
||||
Void::Build::Daemon::request(socket, R"({"op":"describe"})", std::chrono::seconds{120});
|
||||
Void::Testing::require(raw.has_value());
|
||||
// 128 names x 200 bytes plus JSON overhead stays well under 32 KiB.
|
||||
Void::Testing::expect(raw->size() < 32U * 1024U);
|
||||
const auto parsed = parse_json(*raw);
|
||||
Void::Testing::require(parsed.has_value());
|
||||
const auto *object = as_object(*parsed);
|
||||
Void::Testing::require(object != nullptr);
|
||||
Void::Testing::expect(bool_field(*object, "ok"));
|
||||
Void::Testing::expect_eq(int_field(*object, "targets"), std::int64_t{140});
|
||||
Void::Testing::expect(int_field(*object, "actions") >= 140);
|
||||
const auto names = string_array_field(*object, "target_names");
|
||||
Void::Testing::expect_eq(names.size(), std::size_t{128});
|
||||
for (const auto &name : names) {
|
||||
Void::Testing::expect(name.size() <= 200U);
|
||||
}
|
||||
Void::Testing::expect(bool_field(*object, "target_names_truncated"));
|
||||
|
||||
auto shutdown = Void::Build::Daemon::request(socket, R"({"op":"shutdown"})");
|
||||
Void::Testing::expect(shutdown.has_value());
|
||||
worker.join();
|
||||
Void::Testing::expect(run_result.has_value());
|
||||
Void::Testing::expect(!std::filesystem::exists(socket));
|
||||
}
|
||||
|
||||
VOID_TEST(Daemon, describe_rejects_workspace_root) {
|
||||
auto temp = Void::FileSystem::TempDir::open("vbs_daemon_");
|
||||
Void::Testing::require(temp.has_value());
|
||||
const auto options = make_options(temp->path());
|
||||
|
||||
{
|
||||
std::ofstream workspace{options.project_root / "workspace.cpp"};
|
||||
workspace << "void workspace(Void::Build::Workspace &workspace) {}\n";
|
||||
Void::Testing::require(workspace.good());
|
||||
}
|
||||
|
||||
auto server = Void::Build::Daemon::DaemonServer::create(options);
|
||||
Void::Testing::require(server.has_value());
|
||||
const auto socket = server->socket_path();
|
||||
|
||||
Void::Result::Result<void, Void::Result::Error> run_result;
|
||||
std::jthread worker{[&] { run_result = server->run(); }};
|
||||
Void::Testing::require(wait_for_path(socket, std::chrono::seconds{5}));
|
||||
|
||||
auto describe = request_object(socket, R"({"op":"describe"})");
|
||||
Void::Testing::require(describe.has_value());
|
||||
Void::Testing::expect(!bool_field(*describe, "ok", true));
|
||||
Void::Testing::expect(str_field(*describe, "error").find("workspace") != std::string::npos);
|
||||
|
||||
auto shutdown = Void::Build::Daemon::request(socket, R"({"op":"shutdown"})");
|
||||
Void::Testing::expect(shutdown.has_value());
|
||||
worker.join();
|
||||
Void::Testing::expect(run_result.has_value());
|
||||
Void::Testing::expect(!std::filesystem::exists(socket));
|
||||
}
|
||||
Reference in new issue
Block a user