Files
Language-Server/tests/workspace/WorkspaceTests.cpp
T
faumaray da8003f6d4 Implement the VLS roadmap on the native VBS build
Native VBS and CMake builds of the server and every registered suite:
protocol and document sync, VBS/compdb providers, stock GCC diagnostics
(including in-process default profile preparation), the stock occurrence
observer and hover/definition, the bounded Scheduler, C++ module graph,
scan and artifact cache with real CMI production, the context-aware
symbol index with schema-2 snapshot migration, navigation and guarded
rename, reflection provenance with splice-origin extraction, index-backed
completion and signature help, live GCC worker control with a registered
DAP suite, and the Neovim and VS Code clients.

VoidLibrary follows main. The private GCC 16.2.0 frontend is reproduced
from the versioned patches and verified stepping reflection/template code
with projected values 17/34 and 41/82.
2026-12-05 14:06:50 +03:00

612 lines
30 KiB
C++

#include <Void/LanguageServer/Workspace/Trust.hpp>
#include <Void/System/TempDir.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <algorithm>
#include <barrier>
#include <fstream>
#include <future>
#include <poll.h>
#include <sys/inotify.h>
#include <sys/stat.h>
namespace {
using namespace Void::LanguageServer::Workspace;
using Void::Testing::expect;
using Void::Testing::require;
using enum AuthorityLevel;
namespace fs = std::filesystem;
namespace Protocol = Void::LanguageServer::Protocol;
void write(const fs::path &path, std::string_view bytes) {
std::ofstream output(path, std::ios::binary);
output.write(bytes.data(), static_cast<std::streamsize>(bytes.size()));
output.close();
require(output.good());
}
void make_tool(const fs::path &path, std::string_view bytes = "abc") {
write(path, bytes);
fs::permissions(path, fs::perms::owner_read | fs::perms::owner_write | fs::perms::owner_exec);
}
PartialWorkspaceSettings workspace(const fs::path &root, std::string_view json = "{}") {
auto payload = Protocol::parse_json(json);
require(payload.has_value());
return parse_workspace_settings(*payload, root);
}
MergedConfiguration selected(const fs::path &root, std::vector<ToolSelection> selections) {
SettingsOverrides flags;
flags.tool_approvals = std::move(selections);
auto cli = settings_from_cli(std::move(flags), root);
return merge_configuration(workspace(root), nullptr, &cli);
}
bool diagnosed(const MergedConfiguration &merged, std::string_view key, ConfigIssue code) {
return std::ranges::any_of(merged.diagnostics(),
[&](const auto &d) { return d.key == key && d.issue == code; });
}
template <typename T> bool refused(const std::expected<T, TrustError> &value, TrustError error) {
return !value && value.error() == error;
}
struct Fixture {
Void::FileSystem::TempDir temp{"vls_workspace_"};
fs::path root = temp.path() / "one", other = temp.path() / "one-more";
fs::path tool = temp.path() / "g++";
Fixture() {
fs::create_directories(root / "out");
fs::create_directories(other);
make_tool(tool);
}
std::uint64_t install(TrustPolicy &policy, AuthorityLevel purpose) const {
auto version = policy.replace_configuration(selected(root, {{root, purpose, tool}}));
require(version.has_value());
auto pin = policy.pin_approved_tool(root, purpose, tool);
require(pin.has_value());
return *version;
}
BuildInvocation action(std::uint64_t gen) const {
return {root,
tool,
root / "out",
{"build", "demo"},
"provider:1/inputs:a",
"environment:default",
gen};
}
};
} // namespace
VOID_TEST(WorkspaceTests, precedence_origins_and_owning_bases) {
Fixture f;
write(f.temp.path() / "user.json", R"({"voidLanguageServer":{
"provider":"compdb","compdbPath":"user-db","enableBackgroundIndexing":false,
"toolApprovals":[{"root":"one","operation":"compilerUse","executable":"g++"},
{"root":"one-more","operation":"metadataRefresh","executable":"g++"}]}})");
auto user = read_user_settings(f.temp.path() / "user.json");
require(user.has_value());
expect(user->origin().layer == ConfigLayer::user_runtime &&
user->origin().selected_file == f.temp.path() / "user.json");
auto local = workspace(
f.root,
R"({"provider":"vbs","vbsManifestPath":"local/export.json","compdbPath":"local-db"})");
SettingsOverrides flags;
flags.provider = ProviderPreference::none;
flags.selected_profile = "release";
flags.vbs_manifest_path = "cli/export.json";
auto cli = settings_from_cli(flags, f.other);
auto lower = merge_configuration(local, &*user);
expect(lower.effective().provider == ProviderPreference::compdb);
expect(lower.effective().vbs_manifest_path == f.root / "local/export.json");
auto merged = merge_configuration(local, &*user, &cli);
const auto &s = merged.effective();
expect(s.provider == ProviderPreference::none && !s.enable_background_indexing);
expect(s.compdb_path == f.temp.path() / "user-db" &&
s.vbs_manifest_path == f.other / "cli/export.json");
expect(s.compdb_origin.layer == ConfigLayer::user_runtime &&
s.compdb_origin.selected_file == f.temp.path() / "user.json");
expect(s.vbs_manifest_origin.layer == ConfigLayer::cli &&
s.vbs_manifest_origin.base == f.other);
expect(merge_configuration(local).effective().compdb_origin.layer ==
ConfigLayer::workspace_local);
expect(s.selected_profile == "release");
expect(s.tool_approvals_origin.layer == ConfigLayer::user_runtime);
expect(s.tool_approvals_origin.selected_file == f.temp.path() / "user.json");
require(s.tool_approvals.size() == 1);
expect(s.tool_approvals[0] == ToolSelection{f.root, compiler_use, f.tool});
TrustPolicy policy;
require(policy.replace_configuration(merged).has_value());
const auto observed = policy.pin_approved_tool(f.root, compiler_use, f.tool);
require(observed.has_value());
expect(observed->selection_origin.selected_file == s.tool_approvals_origin.selected_file);
auto second = merge_configuration(workspace(f.other), &*user);
require(second.effective().tool_approvals.size() == 1);
expect(second.effective().tool_approvals[0] ==
ToolSelection{f.other, trusted_metadata_refresh, f.tool});
}
VOID_TEST(WorkspaceTests, workspace_cannot_relabel_or_grant_authority) {
Fixture f;
auto local = workspace(
f.root, R"({"trustedRoots":["."],"approvedCompilers":["g++"],"approvedWrappers":["custom"],
"allowMetadataRefresh":true,"allowExplicitBuildActions":true,"allowCompilerPlugins":true,
"toolApprovals":[{"root":".","operation":"compilerUse","executable":"g++"}]})");
auto merged = merge_configuration(local, &local, &local);
for (auto key :
{"trustedRoots", "approvedCompilers", "approvedWrappers", "allowMetadataRefresh",
"allowExplicitBuildActions", "allowCompilerPlugins", "toolApprovals"})
expect(diagnosed(merged, key, ConfigIssue::untrusted_setting));
expect(diagnosed(merged, "", ConfigIssue::invalid_origin));
expect(merged.effective().tool_approvals.empty());
expect(!merged.effective().allow_metadata_refresh &&
!merged.effective().allow_explicit_build_actions);
TrustPolicy policy;
require(policy.replace_configuration(merged).has_value());
expect(refused(policy.pin_approved_tool(f.root, compiler_use, f.tool),
TrustError::untrusted_root));
write(f.temp.path() / "legacy.json",
R"({"trustedRoots":["one"],"approvedCompilers":["g++"],"allowMetadataRefresh":true})");
auto legacy = read_user_settings(f.temp.path() / "legacy.json");
require(legacy.has_value());
require(policy.replace_configuration(merge_configuration(local, &*legacy)).has_value());
expect(refused(policy.pin_approved_tool(f.root, trusted_metadata_refresh, f.tool),
TrustError::untrusted_root));
}
VOID_TEST(WorkspaceTests, malformed_fields_and_resource_ceilings) {
Fixture f;
const auto bad =
merge_configuration(workspace(f.root, R"({"provider":false,"vbsManifestPath":"",
"compdbPath":2,"enableBackgroundIndexing":"yes","selectedProfile":null,"workerMemoryLimitMib":0,
"maxSemanticWorkers":2.0,"maxResponseFileDepth":-1,"maxCompdbBytes":2147483649,
"maxManifestBytes":"100","maxResponseFileBytes":null,"unknownSetting":1})"));
for (auto key :
{"provider", "vbsManifestPath", "compdbPath", "enableBackgroundIndexing",
"selectedProfile", "workerMemoryLimitMib", "maxSemanticWorkers", "maxResponseFileDepth",
"maxCompdbBytes", "maxManifestBytes", "maxResponseFileBytes"})
expect(diagnosed(bad, key, ConfigIssue::invalid_value));
expect(diagnosed(bad, "unknownSetting", ConfigIssue::unsupported));
const auto local = workspace(f.root, R"({"maxSemanticWorkers":64,"workerMemoryLimitMib":65536,
"maxResponseFileDepth":32,"maxResponseFileBytes":16777216,"maxCompdbBytes":1073741824,"maxManifestBytes":1024})");
auto merged = merge_configuration(local);
for (auto key : {"maxSemanticWorkers", "workerMemoryLimitMib", "maxResponseFileDepth",
"maxResponseFileBytes", "maxCompdbBytes"})
expect(diagnosed(merged, key, ConfigIssue::resource_limit));
expect(merged.effective().limits.max_semantic_workers ==
ResourceLimitsConfig{}.max_semantic_workers);
expect(merged.effective().limits.max_manifest_bytes == 1024);
SettingsOverrides flags;
flags.max_semantic_workers = 8;
flags.worker_memory_limit_mib = 512;
flags.allow_compiler_plugins = true;
auto cli = settings_from_cli(flags, f.root);
auto runtime = merge_configuration(local, nullptr, &cli);
expect(runtime.effective().limits.max_semantic_workers == 8 &&
runtime.effective().limits.worker_memory_limit_mib == 512);
expect(!runtime.effective().allow_compiler_plugins &&
diagnosed(runtime, "allowCompilerPlugins", ConfigIssue::unsupported));
auto nested =
merge_configuration(workspace(f.root, R"({"voidLanguageServer":false,"provider":"none"})"));
expect(diagnosed(nested, "", ConfigIssue::invalid_value) &&
nested.effective().provider == ProviderPreference::auto_detect);
expect(diagnosed(merge_configuration(workspace("relative")), "", ConfigIssue::invalid_origin));
}
VOID_TEST(WorkspaceTests, trusted_replacements_are_all_or_nothing) {
Fixture f;
const auto file = f.temp.path() / "settings";
write(file,
R"({"toolApprovals":[{"root":"one","operation":"metadataRefresh","executable":"g++"}]})");
auto user = read_user_settings(file);
require(user.has_value());
const auto local = workspace(f.root);
SettingsOverrides flags;
flags.tool_approvals = std::vector<ToolSelection>{
{f.root, compiler_use, f.tool}, {f.temp.path() / "missing", compiler_use, f.tool}};
auto invalid = settings_from_cli(flags, f.root);
auto rejected = merge_configuration(local, &*user, &invalid);
expect(diagnosed(rejected, "toolApprovals", ConfigIssue::invalid_value));
expect(rejected.effective().tool_approvals.empty());
flags = {};
flags.allow_metadata_refresh = false;
auto veto = settings_from_cli(flags, f.root);
expect(merge_configuration(local, &*user, &veto).effective().tool_approvals.empty());
write(
file,
R"({"allowExplicitBuildActions":"yes","approvedWrappers":["unknown"],"allowCompilerPlugins":5,
"toolApprovals":[{"root":"one","operation":"compilerUse","executable":"g++"},{"operation":"shell"}]})");
user = read_user_settings(file);
require(user.has_value());
rejected = merge_configuration(local, &*user);
for (auto key :
{"allowExplicitBuildActions", "approvedWrappers", "allowCompilerPlugins", "toolApprovals"})
expect(diagnosed(rejected, key, ConfigIssue::invalid_value));
expect(rejected.effective().tool_approvals.empty() &&
rejected.effective().approved_wrappers.empty());
}
VOID_TEST(WorkspaceTests, explicit_user_file_and_executable_read_bounds) {
Fixture f;
expect(!read_user_settings("relative/settings"));
expect(!read_user_settings(f.root));
expect(refused(inspect_tool_identity(f.root), TrustError::not_regular));
expect(refused(inspect_tool_identity("g++"), TrustError::invalid_path));
expect(refused(inspect_tool_identity(f.root / "missing"), TrustError::unavailable_file));
const auto fifo = f.root / "pipe";
require(::mkfifo(fifo.c_str(), 0700) == 0);
expect(refused(inspect_tool_identity(fifo), TrustError::not_regular));
expect(!read_user_settings(fifo));
write(f.root / "settings", "{");
expect(!read_user_settings(f.root / "settings"));
fs::resize_file(f.root / "settings", 1024 * 1024 + 1);
expect(!read_user_settings(f.root / "settings"));
fs::resize_file(f.tool, max_tool_bytes + 1);
expect(refused(inspect_tool_identity(f.tool), TrustError::too_large));
}
VOID_TEST(WorkspaceTests, hash_revalidated_with_identical_length_and_mtime) {
Fixture f;
TrustPolicy policy;
const auto generation = f.install(policy, compiler_use);
const auto pinned = policy.check_tool(f.root, compiler_use, f.tool, generation);
require(pinned.has_value());
expect(pinned->identity.content_hash_hex ==
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
auto incomplete = pinned->identity;
incomplete.content_hash_hex.clear();
expect(refused(revalidate_tool_identity(incomplete), TrustError::digest_unavailable));
const auto mtime = fs::last_write_time(f.tool);
write(f.tool, "abd");
fs::last_write_time(f.tool, mtime);
expect(refused(policy.check_tool(f.root, compiler_use, f.tool, generation),
TrustError::identity_changed));
expect(policy.generation(f.root) != generation);
write(f.tool, "abc");
expect(!policy.pin_approved_tool(f.root, compiler_use, f.tool));
expect(f.install(policy, compiler_use) > generation);
}
VOID_TEST(WorkspaceTests, selected_symlink_permission_and_replacement_identity) {
Fixture f;
auto first = f.tool;
auto second = f.temp.path() / "same-bytes";
fs::create_hard_link(first, second);
f.tool = f.root / "compiler";
fs::create_symlink(first, f.tool);
TrustPolicy policy;
auto gen = f.install(policy, compiler_use);
auto pin = policy.check_tool(f.root, compiler_use, f.tool, gen);
require(pin.has_value());
expect(pin->identity.selected_path == f.tool && pin->identity.canonical_path == first);
fs::remove(f.tool);
fs::create_symlink(second, f.tool);
expect(refused(policy.check_tool(f.root, compiler_use, f.tool, gen),
TrustError::identity_changed));
gen = f.install(policy, compiler_use);
fs::permissions(second, fs::perms::group_exec, fs::perm_options::add);
expect(refused(policy.check_tool(f.root, compiler_use, f.tool, gen),
TrustError::identity_changed));
gen = f.install(policy, compiler_use);
fs::permissions(second, fs::perms::owner_read);
expect(
refused(policy.check_tool(f.root, compiler_use, f.tool, gen), TrustError::not_executable));
fs::permissions(second, fs::perms::owner_read | fs::perms::owner_write | fs::perms::owner_exec);
gen = f.install(policy, compiler_use);
make_tool(f.root / "replacement");
fs::rename(f.root / "replacement", second);
expect(refused(policy.check_tool(f.root, compiler_use, f.tool, gen),
TrustError::identity_changed));
}
VOID_TEST(WorkspaceTests, exact_roots_and_operation_specific_pins) {
Fixture f;
const auto nested = f.root / "out";
const auto alternate = f.other / "g++";
make_tool(alternate);
const std::vector<ToolSelection> selections{{f.root, compiler_use, f.tool},
{f.root, trusted_metadata_refresh, alternate},
{f.other, trusted_metadata_refresh, f.tool}};
TrustPolicy policy;
auto a = policy.replace_configuration(selected(f.root, selections));
auto b = policy.replace_configuration(selected(f.other, selections));
require(a.has_value() && b.has_value());
expect(
refused(policy.check_tool(f.root, compiler_use, f.tool, *a), TrustError::tool_not_pinned));
require(policy.pin_approved_tool(f.root, compiler_use, f.tool).has_value());
const auto alternate_spelling =
f.tool.parent_path().native() + "//" + f.tool.filename().native();
expect(refused(policy.check_tool(f.root, compiler_use, alternate_spelling, *a),
TrustError::tool_not_approved));
require(policy.pin_approved_tool(f.other, trusted_metadata_refresh, f.tool).has_value());
expect(refused(policy.pin_approved_tool(f.other, compiler_use, f.tool),
TrustError::operation_not_approved));
expect(refused(policy.pin_approved_tool(f.root, trusted_metadata_refresh, f.tool),
TrustError::tool_not_approved));
expect(refused(policy.pin_approved_tool(nested, compiler_use, f.tool),
TrustError::untrusted_root));
expect(refused(policy.pin_approved_tool(f.root, passive_read, f.tool),
TrustError::operation_not_approved));
expect(refused(policy.check_tool(f.root, explicit_build_action, f.tool, *a),
TrustError::explicit_action_required));
policy.revoke_root_authority(f.root);
expect(policy.generation(f.other) == *b &&
policy.check_tool(f.other, trusted_metadata_refresh, f.tool, *b).has_value());
require(policy.replace_configuration(selected(f.root, selections)).has_value());
require(policy.pin_approved_tool(f.root, compiler_use, f.tool).has_value());
expect(
refused(policy.check_tool(f.root, compiler_use, f.tool, *a), TrustError::stale_generation));
}
VOID_TEST(WorkspaceTests, relative_tool_selection_keeps_symlink_traversal) {
Fixture f;
fs::create_directory(f.other / "deep");
fs::create_directory_symlink(f.other / "deep", f.temp.path() / "links");
make_tool(f.other / "g++", "selected");
make_tool(f.root / "g++", "selected");
write(
f.temp.path() / "user",
R"({"toolApprovals":[{"root":"one","operation":"compilerUse","executable":"links/../g++"}]})");
auto user = read_user_settings(f.temp.path() / "user");
require(user.has_value());
TrustPolicy policy;
auto gen = policy.replace_configuration(merge_configuration(workspace(f.root), &*user));
require(gen.has_value());
const auto spelling = f.temp.path() / "links/../g++";
auto pin = policy.pin_approved_tool(f.root, compiler_use, spelling);
require(pin.has_value());
expect(pin->identity.selected_path.native() == spelling.native() &&
pin->identity.canonical_path == f.other / "g++");
fs::remove(f.temp.path() / "links");
fs::create_directory_symlink(f.root / "out", f.temp.path() / "links");
expect(refused(policy.check_tool(f.root, compiler_use, spelling, *gen),
TrustError::identity_changed));
}
VOID_TEST(WorkspaceTests, replacing_workspace_directory_revokes_its_pins) {
Fixture f;
TrustPolicy policy;
const auto gen = f.install(policy, compiler_use);
fs::rename(f.root, f.temp.path() / "previous");
fs::create_directory(f.root);
expect(
refused(policy.check_tool(f.root, compiler_use, f.tool, gen), TrustError::untrusted_root));
expect(policy.generation(f.root) > gen);
expect(!policy.pin_approved_tool(f.root, compiler_use, f.tool));
}
VOID_TEST(WorkspaceTests, cached_settings_cannot_rearm_revoked_pins) {
Fixture f;
const auto file = f.temp.path() / "selected.json";
write(file,
R"({"toolApprovals":[{"root":"one","operation":"compilerUse","executable":"g++"}]})");
auto user = read_user_settings(file);
require(user.has_value());
auto merged = merge_configuration(workspace(f.root), &*user);
TrustPolicy policy;
auto gen = policy.replace_configuration(merged);
require(gen.has_value() && policy.pin_approved_tool(f.root, compiler_use, f.tool).has_value());
auto changed_workspace =
merge_configuration(workspace(f.root, R"({"provider":"compdb"})"), &*user);
expect(policy.replace_configuration(changed_workspace) == gen);
require(policy.check_tool(f.root, compiler_use, f.tool, *gen).has_value());
// This trusted input was read before revocation, even though delivery to the policy is later.
auto delayed = read_user_settings(file);
require(delayed.has_value());
write(f.tool, "abd");
require(refused(policy.check_tool(f.root, compiler_use, f.tool, *gen),
TrustError::identity_changed));
expect(refused(policy.replace_configuration(changed_workspace), TrustError::stale_generation));
expect(refused(policy.replace_configuration(merge_configuration(workspace(f.root), &*delayed)),
TrustError::stale_generation));
expect(!policy.pin_approved_tool(f.root, compiler_use, f.tool));
auto fresh = read_user_settings(file);
require(fresh.has_value());
gen = policy.replace_configuration(merge_configuration(workspace(f.root), &*fresh));
require(gen.has_value() && policy.pin_approved_tool(f.root, compiler_use, f.tool).has_value());
policy.revoke_root_authority(f.root);
expect(refused(policy.replace_configuration(merge_configuration(workspace(f.root), &*fresh)),
TrustError::stale_generation));
}
VOID_TEST(WorkspaceTests, revocation_during_revalidation_commits_before_callback) {
Fixture f;
fs::resize_file(f.tool, 8 * 1024 * 1024);
std::uint64_t seen = 0;
TrustPolicy policy([&](const fs::path &root, std::uint64_t gen) noexcept {
if (policy.generation(root) == gen) {
seen = gen; // callback reenters the policy
policy.revoke_root_authority(root);
}
});
const auto gen = f.install(policy, compiler_use);
Void::System::OwnedFd watch{::inotify_init1(IN_NONBLOCK | IN_CLOEXEC)};
require(watch.valid() && ::inotify_add_watch(watch.get(), f.tool.c_str(), IN_ACCESS) >= 0);
auto work = std::async(std::launch::async,
[&] { return policy.check_tool(f.root, compiler_use, f.tool, gen); });
pollfd event{watch.get(), POLLIN, 0};
require(::poll(&event, 1, 5000) == 1);
policy.revoke_root_authority(f.root);
const auto result = work.get();
expect(seen > gen && seen == policy.generation(f.root));
// If the reader won scheduling, its completed report still carries the superseded generation.
expect(result ? result->trust_generation != seen
: result.error() == TrustError::stale_generation);
expect(!policy.check_tool(f.root, compiler_use, f.tool, gen));
}
VOID_TEST(WorkspaceTests, build_approval_binds_every_invocation_field) {
Fixture f;
TrustPolicy policy;
auto request = f.action(f.install(policy, explicit_build_action));
auto ticket = policy.approve_build_action(request);
require(ticket.has_value());
expect(refused(policy.approve_build_action(request), TrustError::action_pending));
auto mismatch = [&](auto edit) {
auto different = request;
edit(different);
expect(refused(policy.consume_build_action(*ticket, different),
TrustError::invocation_mismatch));
};
mismatch([&](auto &v) { v.root = f.other; });
mismatch([&](auto &v) { v.working_directory = f.root; });
mismatch([&](auto &v) { v.executable = f.root / "other-tool"; });
mismatch([&](auto &v) {
v.executable = f.tool.parent_path().native() + "//" + f.tool.filename().native();
});
mismatch([](auto &v) { std::ranges::reverse(v.arguments); });
mismatch([](auto &v) { v.input_identity += "2"; });
mismatch([](auto &v) { v.environment_identity += "2"; });
mismatch([](auto &v) { ++v.trust_generation; });
TrustPolicy other;
(void)f.install(other, explicit_build_action);
require(other.approve_build_action(request).has_value());
expect(refused(other.consume_build_action(*ticket, request), TrustError::action_not_current));
require(policy.consume_build_action(*ticket, request).has_value());
expect(refused(policy.consume_build_action(*ticket, request), TrustError::action_not_current));
ticket = policy.approve_build_action(request);
require(ticket.has_value());
expect(policy.cancel_build_action(*ticket) && !policy.cancel_build_action(*ticket));
expect(!policy.consume_build_action(*ticket, request));
}
VOID_TEST(WorkspaceTests, build_scope_changes_and_revocation_invalidate_pending_action) {
Fixture f;
TrustPolicy policy;
auto action = f.action(f.install(policy, explicit_build_action));
auto invalid = action;
invalid.working_directory = f.other;
expect(refused(policy.approve_build_action(invalid), TrustError::invalid_invocation));
fs::create_directory_symlink(f.other, f.root / "alias");
invalid.working_directory = f.root / "alias";
expect(refused(policy.approve_build_action(invalid), TrustError::invalid_invocation));
invalid = action;
invalid.arguments.emplace_back("test\0tail", 9);
expect(refused(policy.approve_build_action(invalid), TrustError::invalid_invocation));
invalid = action;
invalid.input_identity.clear();
expect(refused(policy.approve_build_action(invalid), TrustError::invalid_invocation));
auto ticket = policy.approve_build_action(action);
require(ticket.has_value());
fs::rename(action.working_directory, f.root / "retired");
fs::create_directory(action.working_directory);
expect(refused(policy.consume_build_action(*ticket, action), TrustError::invalid_invocation));
ticket = policy.approve_build_action(action);
require(ticket.has_value());
policy.revoke_root_authority(f.root);
expect(!policy.consume_build_action(*ticket, action));
action.trust_generation = f.install(policy, explicit_build_action);
ticket = policy.approve_build_action(action);
require(ticket.has_value());
write(f.tool, "new");
expect(refused(policy.consume_build_action(*ticket, action), TrustError::identity_changed));
expect(!policy.consume_build_action(*ticket, action));
}
VOID_TEST(WorkspaceTests, concurrent_build_ticket_has_one_consumer) {
Fixture f;
TrustPolicy policy;
auto request = f.action(f.install(policy, explicit_build_action));
auto ticket = policy.approve_build_action(request);
require(ticket.has_value());
std::barrier start(2);
auto consume = [&] {
start.arrive_and_wait();
return policy.consume_build_action(*ticket, request).has_value();
};
auto other = std::async(std::launch::async, consume);
const bool first = consume();
expect(first != other.get());
}
VOID_TEST(WorkspaceTests, supported_compiler_and_wrappers_are_only_data) {
Fixture f;
const std::string marker_tool = "#!/bin/sh\nprintf 'unexpected launch' >> \"${0%/*}/marker\"\n";
make_tool(f.tool, marker_tool);
make_tool(f.temp.path() / "ccache", marker_tool);
make_tool(f.temp.path() / "plugin-marker", marker_tool);
write(f.temp.path() / "marker", "untouched");
TrustPolicy policy;
const auto gen = f.install(policy, compiler_use);
auto checked = policy.check_tool(f.root, compiler_use, f.tool, gen);
require(checked.has_value());
const std::vector<std::string> wrappers{"ccache", "sccache"};
const std::vector<std::string> command{f.tool.string(),
"-c",
"-std=c++26",
"-freflection",
"-D",
"WITH_SPACES=a b",
"-UUNUSED",
"-I" + f.root.string(),
"-isystem",
f.other.string(),
"-x",
"c++",
(f.root / "some source.cpp").string()};
auto report =
classify_and_unwrap_command(command, wrappers, checked->identity, CompilerDriver::gcc);
expect(report.allowed && report.classification == FlagClassification::safe &&
report.unwrapped_argv == command);
auto wrapped = command;
wrapped.insert(wrapped.begin(), {(f.temp.path() / "ccache").string(), "sccache"});
report = classify_and_unwrap_command(wrapped, wrappers, checked->identity, CompilerDriver::gcc);
expect(report.allowed && report.classification == FlagClassification::transparent_wrapper);
expect(report.unwrapped_argv == command &&
report.unwrapped_wrappers == std::vector<std::string>{wrapped[0], wrapped[1]});
wrapped.push_back("-fplugin=" + (f.temp.path() / "plugin-marker").string());
report = classify_and_unwrap_command(wrapped, wrappers, checked->identity, CompilerDriver::gcc);
expect(!report.allowed &&
report.classification == FlagClassification::execution_bearing_plugin);
std::ifstream marker(f.temp.path() / "marker");
std::string contents;
std::getline(marker, contents);
expect(contents == "untouched");
}
VOID_TEST(WorkspaceTests, unresolved_or_execution_bearing_argv_is_refused) {
Fixture f;
auto identity = inspect_tool_identity(f.tool);
require(identity.has_value());
const std::vector<std::string> wrappers{"ccache", "custom-wrapper"};
auto classify = [&](std::vector<std::string> argv,
CompilerDriver driver = CompilerDriver::gcc) {
return classify_and_unwrap_command(argv, wrappers, *identity, driver);
};
using enum FlagClassification;
auto blocked = [&](std::vector<std::string> middle, FlagClassification reason) {
std::vector<std::string> cmd{f.tool.string(), "-c"};
cmd.insert(cmd.end(), middle.begin(), middle.end());
cmd.push_back((f.root / "a.cpp").string());
auto report = classify(cmd);
expect(!report.allowed && report.classification == reason &&
report.unwrapped_argv.empty() && !report.reason.empty());
};
for (auto flag : {"-fplugin=marker", "-fpass-plugin=marker", "-load"})
blocked({flag}, execution_bearing_plugin);
for (auto flag : {"-Xclang", "-Wp,marker", "-Wa,marker", "-Wl,marker", "-B/tools",
"-specs=marker", "--config=marker"})
blocked({flag}, suspicious_toolchain_override);
blocked({"-wrapper=marker"}, execution_bearing_wrapper);
blocked({"@nested.rsp"}, unresolved_response_file);
blocked({"-I", "@include.rsp"}, unresolved_response_file);
blocked({"-isystem", "-fplugin=marker"}, unsupported_operand);
blocked({"-x", "assembler"}, unsupported_operand);
blocked({"-D", "-Xclang"}, unsupported_operand);
for (auto flag : {"-finvented-mode", "-o", "--", "-cc1", "-mllvm", "--target=elsewhere"})
blocked({flag}, unsupported_option);
blocked({"relative.cpp"}, unsupported_operand);
blocked({std::string("a\0b", 3)}, invalid_command);
expect(classify({"g++", "-c", (f.root / "a.cpp").string()}).classification ==
unapproved_compiler_path);
expect(classify({"custom-wrapper", f.tool.string(), "-c", (f.root / "a.cpp").string()})
.classification == execution_bearing_wrapper);
expect(classify({f.tool.string(), "-c", (f.root / "a.cpp").string()}, CompilerDriver::unknown)
.classification == unsupported_driver);
expect(!classify({f.tool.string(), (f.root / "a.cpp").string()}).allowed);
}