Files
ViewDesignEngine/tests/plugin/test_plugin_external_service.cpp
T

1060 lines
48 KiB
C++

// tests/plugin/test_plugin_external_service.cpp
// task-40 P2-4:进程外服务(vde-service)接入 PluginManager 的端到端与错误路径测试。
// 覆盖:清单发现/坏清单、官方示例 STL->OBJ 端到端、超时 kill、崩溃隔离、坏响应、
// status=error 映射、输出路径白名单(越界拒绝)、输出捕获上限、环境变量隔离、
// 沙箱工作目录白名单、未知服务/坏请求。
#include <gtest/gtest.h>
#include "vde/plugin/plugin_manager.h"
#include "vde/plugin/external_service_adapters.h"
#include <chrono>
#include "vde/fs_compat.h"
#include <fstream>
#include <sstream>
#include <string>
#include <thread>
#include <vector>
#if defined(_WIN32)
#include <cstdlib>
#include <windows.h>
#else
#include <cerrno>
#include <csignal>
#include <cstdlib>
#include <sys/types.h>
#include <unistd.h>
#endif
namespace fs = vde::fs;
using vde::plugin::ExternalServiceInvokeOptions;
using vde::plugin::ExternalServiceResult;
using vde::plugin::PluginManager;
#ifndef VDE_EXTERNAL_STL_SERVICE_DIR
#define VDE_EXTERNAL_STL_SERVICE_DIR ""
#endif
#ifndef VDE_TEST_SERVICE_EXE
#define VDE_TEST_SERVICE_EXE ""
#endif
#ifndef VDE_EXTERNAL_GCS_SERVICE_DIR
#define VDE_EXTERNAL_GCS_SERVICE_DIR ""
#endif
namespace {
void set_test_env(const char* name, const char* value) {
#if defined(_WIN32)
_putenv_s(name, value ? value : "");
#else
if (value)
setenv(name, value, 1);
else
unsetenv(name);
#endif
}
std::string read_text(const fs::path& path) {
std::ifstream in(path.string(), std::ios::binary);
std::ostringstream ss;
ss << in.rdbuf();
return ss.str();
}
bool write_text(const fs::path& path, const std::string& text) {
std::ofstream out(path.string(), std::ios::binary | std::ios::trunc);
if (!out) return false;
out.write(text.data(), static_cast<std::streamsize>(text.size()));
return static_cast<bool>(out);
}
std::string make_manifest_text(
const std::string& id, const fs::path& executable, int request_timeout_ms,
const std::vector<std::string>& env_allow,
const std::string& limits_json = std::string(),
const std::vector<std::string>& capabilities = std::vector<std::string>{"service:test.echo"},
int priority = 0) {
std::string capability_json = "[";
for (std::size_t i = 0; i < capabilities.size(); ++i) {
if (i != 0) capability_json += ",";
capability_json += "\"" + capabilities[i] + "\"";
}
capability_json += "]";
std::string env_json = "[";
for (std::size_t i = 0; i < env_allow.size(); ++i) {
if (i != 0) env_json += ",";
env_json += "\"" + env_allow[i] + "\"";
}
env_json += "]";
const std::string exe = executable.generic_string();
std::string text = "{\n";
text += " \"id\": \"" + id + "\",\n";
text += " \"name\": \"VDE Test External Service\",\n";
text += " \"version\": \"0.1.0\",\n";
text += " \"protocol\": { \"name\": \"vde-service\", \"version\": \"0.1\" },\n";
text += " \"executable\": { \"windows\": \"" + exe + "\", \"linux\": \"" + exe + "\" },\n";
text += " \"capabilities\": " + capability_json + ",\n";
if (priority != 0) text += " \"priority\": " + std::to_string(priority) + ",\n";
text += " \"timeouts\": { \"request_ms\": " + std::to_string(request_timeout_ms) + " },\n";
if (!limits_json.empty()) text += " \"limits\": " + limits_json + ",\n";
text += " \"sandbox\": { \"network\": false, \"env_allow\": " + env_json + " },\n";
text += " \"license\": \"Apache-2.0\"\n";
text += "}\n";
return text;
}
fs::path make_helper_service_dir(
const fs::path& root, const std::string& id,
const std::vector<std::string>& env_allow, int request_timeout_ms = 5000,
const std::string& limits_json = std::string(),
const std::vector<std::string>& capabilities = std::vector<std::string>{"service:test.echo"},
int priority = 0) {
const fs::path dir = root / ("svc_" + id);
fs::create_directories(dir);
write_text(dir / "vde-service.json",
make_manifest_text(id, fs::path(VDE_TEST_SERVICE_EXE), request_timeout_ms,
env_allow, limits_json, capabilities, priority));
return dir;
}
std::string make_policy_manifest_text(
const std::string& id, const fs::path& executable, int request_timeout_ms,
const std::vector<std::string>& env_allow, const std::string& sandbox_extra_json,
const std::vector<std::string>& capabilities = std::vector<std::string>{"service:test.echo"}) {
std::string env_json = "[";
for (std::size_t i = 0; i < env_allow.size(); ++i) {
if (i != 0) env_json += ",";
env_json += "\"" + env_allow[i] + "\"";
}
env_json += "]";
std::string capability_json = "[";
for (std::size_t i = 0; i < capabilities.size(); ++i) {
if (i != 0) capability_json += ",";
capability_json += "\"" + capabilities[i] + "\"";
}
capability_json += "]";
const std::string exe = executable.generic_string();
std::string sandbox = " \"sandbox\": { ";
if (!sandbox_extra_json.empty()) sandbox += sandbox_extra_json + ", ";
sandbox += "\"env_allow\": " + env_json + " },\n";
std::string text = "{\n";
text += " \"id\": \"" + id + "\",\n";
text += " \"name\": \"VDE Policy Test Service\",\n";
text += " \"version\": \"0.1.0\",\n";
text += " \"protocol\": { \"name\": \"vde-service\", \"version\": \"0.1\" },\n";
text += " \"executable\": { \"windows\": \"" + exe + "\", \"linux\": \"" + exe + "\" },\n";
text += " \"capabilities\": " + capability_json + ",\n";
text += " \"timeouts\": { \"request_ms\": " + std::to_string(request_timeout_ms) + " },\n";
text += sandbox;
text += " \"license\": \"Apache-2.0\"\n";
text += "}\n";
return text;
}
fs::path make_policy_service_dir(
const fs::path& root, const std::string& id, const std::vector<std::string>& env_allow,
const std::string& sandbox_extra_json, int request_timeout_ms = 5000,
const std::vector<std::string>& capabilities = std::vector<std::string>{"service:test.echo"}) {
const fs::path dir = root / ("svc_" + id);
fs::create_directories(dir);
write_text(dir / "vde-service.json",
make_policy_manifest_text(id, fs::path(VDE_TEST_SERVICE_EXE),
request_timeout_ms, env_allow, sandbox_extra_json,
capabilities));
return dir;
}
std::string make_ascii_stl() {
std::string stl;
stl += "solid test\n";
stl += " facet normal 0 0 1\n outer loop\n";
stl += " vertex 0 0 0\n vertex 1 0 0\n vertex 0 1 0\n";
stl += " endloop\n endfacet\n";
stl += " facet normal 0 0 1\n outer loop\n";
stl += " vertex 1 0 0\n vertex 1 1 0\n vertex 0 1 0\n";
stl += " endloop\n endfacet\n";
stl += "endsolid test\n";
return stl;
}
bool process_is_alive(long long pid) {
if (pid <= 0) return false;
#if defined(_WIN32)
HANDLE handle = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE,
static_cast<DWORD>(pid));
if (handle == nullptr) return false;
DWORD code = 0;
const bool alive = GetExitCodeProcess(handle, &code) && code == STILL_ACTIVE;
CloseHandle(handle);
return alive;
#else
return ::kill(static_cast<pid_t>(pid), 0) == 0 || errno == EPERM;
#endif
}
void terminate_process_best_effort(long long pid) {
if (pid <= 0) return;
#if defined(_WIN32)
HANDLE handle = OpenProcess(PROCESS_TERMINATE, FALSE, static_cast<DWORD>(pid));
if (handle != nullptr) {
TerminateProcess(handle, 1);
CloseHandle(handle);
}
#else
::kill(static_cast<pid_t>(pid), SIGKILL);
#endif
}
bool wait_for_process_exit(long long pid, int timeout_ms) {
const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeout_ms);
while (std::chrono::steady_clock::now() < deadline) {
if (!process_is_alive(pid)) return true;
std::this_thread::sleep_for(std::chrono::milliseconds(25));
}
return !process_is_alive(pid);
}
long long read_pid_file(const fs::path& path) {
const std::string text = read_text(path);
try {
return std::stoll(text);
} catch (...) {
return -1;
}
}
class ExternalServiceTest : public ::testing::Test {
protected:
void SetUp() override {
root_ = fs::temp_directory_path() /
("vde_service_test_" +
std::to_string(static_cast<long long>(
std::chrono::steady_clock::now().time_since_epoch().count())));
fs::create_directories(root_);
auto& manager = PluginManager::instance();
manager.clear();
manager.clear_external_services();
manager.clear_service_search_paths();
manager.set_service_sandbox_root((root_ / "sandbox").string());
manager.set_service_output_limit(256 * 1024);
set_test_env("VDE_TEST_SERVICE_MODE", nullptr);
set_test_env("VDE_SECRET_TOKEN", nullptr);
set_test_env("VDE_ALLOWED_TEST_VAR", nullptr);
}
void TearDown() override {
auto& manager = PluginManager::instance();
manager.clear();
manager.clear_external_services();
manager.clear_service_search_paths();
manager.set_service_sandbox_root("");
std::error_code remove_ec;
fs::remove_all(root_, remove_ec);
set_test_env("VDE_TEST_SERVICE_MODE", nullptr);
set_test_env("VDE_SECRET_TOKEN", nullptr);
set_test_env("VDE_ALLOWED_TEST_VAR", nullptr);
}
ExternalServiceResult invoke_helper_request(const std::string& service_id,
const std::string& request,
const fs::path& work_dir, int timeout_ms = 0) {
ExternalServiceInvokeOptions options;
options.work_dir = work_dir.string();
options.timeout_ms = timeout_ms;
ExternalServiceResult result;
std::string error;
PluginManager::instance().invoke_external_service(service_id, request, options, &result,
&error);
return result;
}
ExternalServiceResult invoke_helper(const std::string& service_id, const fs::path& work_dir) {
const std::string request = "{\"request_id\":\"test-request\",\"operation\":\"echo\"}";
return invoke_helper_request(service_id, request, work_dir);
}
fs::path make_work_dir(const char* name) {
const fs::path dir = root_ / "sandbox" / name;
fs::create_directories(dir);
return dir;
}
fs::path root_;
};
TEST_F(ExternalServiceTest, ScanDiscoversOfficialService) {
auto& manager = PluginManager::instance();
std::string error;
const std::size_t added = manager.scan_service_directory(VDE_EXTERNAL_STL_SERVICE_DIR, &error);
ASSERT_GE(added, 1u) << error;
const auto services = manager.external_services();
ASSERT_FALSE(services.empty());
bool found = false;
for (const auto& service : services) {
if (service.id != "com.vde.example.stl_converter") continue;
found = true;
EXPECT_EQ(service.version.major, 0);
EXPECT_EQ(service.version.minor, 1);
EXPECT_EQ(service.version.patch, 0);
EXPECT_EQ(service.protocol_name, "vde-service");
EXPECT_EQ(service.protocol_version, "0.1");
EXPECT_EQ(service.request_timeout_ms, 15000);
EXPECT_FALSE(service.executable.empty());
EXPECT_TRUE(fs::is_regular_file(service.executable));
EXPECT_EQ(service.capabilities.size(), 1u);
EXPECT_EQ(service.capabilities.front(), "service:mesh.convert");
EXPECT_EQ(service.license, "Apache-2.0");
}
EXPECT_TRUE(found);
}
TEST_F(ExternalServiceTest, ScanInvalidManifestSkippedAndMissingDir) {
auto& manager = PluginManager::instance();
const fs::path bad_dir = root_ / "bad_service";
fs::create_directories(bad_dir);
write_text(bad_dir / "vde-service.json", "{ not a json manifest");
make_helper_service_dir(root_, "com.vde.test.good", {});
std::string error;
EXPECT_EQ(manager.scan_service_directory(root_.string(), &error), 1u);
EXPECT_FALSE(error.empty());
EXPECT_TRUE(manager.external_service("com.vde.test.good").has_value());
EXPECT_FALSE(manager.external_service("com.vde.test.missing").has_value());
std::string missing_error;
EXPECT_EQ(manager.scan_service_directory((root_ / "does_not_exist").string(), &missing_error),
0u);
EXPECT_FALSE(missing_error.empty());
}
TEST_F(ExternalServiceTest, OfficialEndToEndStlToObj) {
auto& manager = PluginManager::instance();
std::string error;
ASSERT_GE(manager.scan_service_directory(VDE_EXTERNAL_STL_SERVICE_DIR, &error), 1u) << error;
const fs::path input = root_ / "input.stl";
write_text(input, make_ascii_stl());
const fs::path work_dir = make_work_dir("e2e");
const fs::path output = work_dir / "output.obj";
const std::string request =
"{\"request_id\":\"e2e-1\",\"operation\":\"stl_to_obj\",\"input\":\"" +
input.generic_string() + "\",\"output\":\"" + output.generic_string() + "\"}";
ExternalServiceInvokeOptions options;
options.work_dir = work_dir.string();
ExternalServiceResult result;
ASSERT_TRUE(manager.invoke_external_service("com.vde.example.stl_converter", request, options,
&result, &error))
<< result.error << " / " << error;
EXPECT_TRUE(result.ok);
EXPECT_TRUE(result.started);
EXPECT_FALSE(result.timed_out);
EXPECT_FALSE(result.protocol_error);
EXPECT_FALSE(result.crashed);
EXPECT_EQ(result.exit_code, 0);
ASSERT_EQ(result.status, "ok");
EXPECT_EQ(result.request_id, "e2e-1");
ASSERT_EQ(result.outputs.size(), 1u);
EXPECT_TRUE(fs::is_regular_file(result.outputs.front()));
const std::string obj = read_text(result.outputs.front());
EXPECT_NE(obj.find("v "), std::string::npos);
EXPECT_NE(result.response_json.find("\"triangles\":2"), std::string::npos);
EXPECT_NE(result.stdout_excerpt.find("\"status\":\"ok\""), std::string::npos);
}
TEST_F(ExternalServiceTest, TimeoutKillsService) {
auto& manager = PluginManager::instance();
std::string error;
ASSERT_GE(manager.scan_service_directory(VDE_EXTERNAL_STL_SERVICE_DIR, &error), 1u) << error;
const fs::path input = root_ / "slow.stl";
write_text(input, make_ascii_stl());
const fs::path work_dir = make_work_dir("timeout");
const std::string request =
"{\"request_id\":\"timeout-1\",\"operation\":\"stl_to_obj\",\"input\":\"" +
input.generic_string() + "\",\"output\":\"" +
(work_dir / "out.obj").generic_string() + "\",\"args\":{\"sleep_ms\":5000}}";
ExternalServiceInvokeOptions options;
options.work_dir = work_dir.string();
options.timeout_ms = 200;
ExternalServiceResult result;
EXPECT_FALSE(manager.invoke_external_service("com.vde.example.stl_converter", request, options,
&result, &error));
EXPECT_FALSE(result.ok);
EXPECT_TRUE(result.started);
EXPECT_TRUE(result.timed_out);
EXPECT_LE(result.duration_ms, 4000);
EXPECT_NE(result.error.find("timed out"), std::string::npos);
// 宿主未被拖垮:后续调用仍可用(helper 服务)
const fs::path dir = make_helper_service_dir(root_, "com.vde.test.after_timeout", {});
std::string scan_error;
EXPECT_GE(manager.scan_service_directory(dir.string(), &scan_error), 1u) << scan_error;
const fs::path helper_work = make_work_dir("after_timeout");
const ExternalServiceResult recovered = invoke_helper("com.vde.test.after_timeout", helper_work);
EXPECT_TRUE(recovered.ok) << recovered.error;
}
TEST_F(ExternalServiceTest, CrashServiceIsolatedAndRecoverable) {
auto& manager = PluginManager::instance();
const fs::path dir =
make_helper_service_dir(root_, "com.vde.test.crash", {"VDE_TEST_SERVICE_MODE"});
std::string error;
ASSERT_GE(manager.scan_service_directory(dir.string(), &error), 1u) << error;
const fs::path work_dir = make_work_dir("crash");
set_test_env("VDE_TEST_SERVICE_MODE", "crash");
ExternalServiceResult result = invoke_helper("com.vde.test.crash", work_dir);
EXPECT_FALSE(result.ok);
EXPECT_TRUE(result.started);
EXPECT_FALSE(result.timed_out);
EXPECT_FALSE(result.protocol_error);
EXPECT_TRUE(result.crashed);
EXPECT_EQ(result.exit_code, 7);
set_test_env("VDE_TEST_SERVICE_MODE", "ok");
result = invoke_helper("com.vde.test.crash", work_dir);
EXPECT_TRUE(result.ok) << result.error;
EXPECT_FALSE(result.crashed);
}
TEST_F(ExternalServiceTest, BadResponseJsonIsProtocolError) {
auto& manager = PluginManager::instance();
const fs::path dir =
make_helper_service_dir(root_, "com.vde.test.badjson", {"VDE_TEST_SERVICE_MODE"});
std::string error;
ASSERT_GE(manager.scan_service_directory(dir.string(), &error), 1u) << error;
const fs::path work_dir = make_work_dir("badjson");
set_test_env("VDE_TEST_SERVICE_MODE", "badjson");
const ExternalServiceResult result = invoke_helper("com.vde.test.badjson", work_dir);
EXPECT_FALSE(result.ok);
EXPECT_TRUE(result.protocol_error);
EXPECT_FALSE(result.crashed);
EXPECT_FALSE(result.response_json.empty());
EXPECT_NE(result.error.find("invalid response JSON"), std::string::npos);
}
TEST_F(ExternalServiceTest, StatusErrorMappedWithCode) {
auto& manager = PluginManager::instance();
const fs::path dir =
make_helper_service_dir(root_, "com.vde.test.statuserror", {"VDE_TEST_SERVICE_MODE"});
std::string error;
ASSERT_GE(manager.scan_service_directory(dir.string(), &error), 1u) << error;
const fs::path work_dir = make_work_dir("statuserror");
set_test_env("VDE_TEST_SERVICE_MODE", "status_error");
const ExternalServiceResult result = invoke_helper("com.vde.test.statuserror", work_dir);
EXPECT_FALSE(result.ok);
EXPECT_TRUE(result.protocol_error);
EXPECT_EQ(result.status, "error");
EXPECT_EQ(result.error_code, "unsupported_operation");
EXPECT_NE(result.error.find("unsupported_operation"), std::string::npos);
}
TEST_F(ExternalServiceTest, OutputPathEscapeRejected) {
auto& manager = PluginManager::instance();
std::string error;
ASSERT_GE(manager.scan_service_directory(VDE_EXTERNAL_STL_SERVICE_DIR, &error), 1u) << error;
const fs::path input = root_ / "escape.stl";
write_text(input, make_ascii_stl());
const fs::path work_dir = make_work_dir("escape_work");
const std::string request =
"{\"request_id\":\"escape-1\",\"operation\":\"stl_to_obj\",\"input\":\"" +
input.generic_string() + "\",\"output\":\"../escape.obj\"}";
ExternalServiceInvokeOptions options;
options.work_dir = work_dir.string();
ExternalServiceResult result;
EXPECT_FALSE(manager.invoke_external_service("com.vde.example.stl_converter", request, options,
&result, &error));
EXPECT_FALSE(result.ok);
EXPECT_TRUE(result.protocol_error);
EXPECT_NE(result.error.find("outside"), std::string::npos);
}
TEST_F(ExternalServiceTest, OutputCaptureIsCapped) {
auto& manager = PluginManager::instance();
const fs::path dir = make_helper_service_dir(root_, "com.vde.test.spam", {"VDE_TEST_SERVICE_MODE"});
std::string error;
ASSERT_GE(manager.scan_service_directory(dir.string(), &error), 1u) << error;
const fs::path work_dir = make_work_dir("spam");
manager.set_service_output_limit(4096);
set_test_env("VDE_TEST_SERVICE_MODE", "spam");
const ExternalServiceResult result = invoke_helper("com.vde.test.spam", work_dir);
EXPECT_TRUE(result.ok) << result.error;
EXPECT_LE(result.stdout_excerpt.size(), 4096 + 64);
EXPECT_LE(result.stderr_excerpt.size(), 4096 + 64);
EXPECT_NE(result.stdout_excerpt.find("truncated"), std::string::npos);
manager.set_service_output_limit(256 * 1024);
}
TEST_F(ExternalServiceTest, EnvironmentVariablesAreAllowlisted) {
auto& manager = PluginManager::instance();
const fs::path dir = make_helper_service_dir(
root_, "com.vde.test.env", {"VDE_TEST_SERVICE_MODE", "VDE_ALLOWED_TEST_VAR"});
std::string error;
ASSERT_GE(manager.scan_service_directory(dir.string(), &error), 1u) << error;
const fs::path work_dir = make_work_dir("env");
set_test_env("VDE_TEST_SERVICE_MODE", "env_echo");
set_test_env("VDE_SECRET_TOKEN", "top-secret");
set_test_env("VDE_ALLOWED_TEST_VAR", "allowed-value");
const ExternalServiceResult result = invoke_helper("com.vde.test.env", work_dir);
EXPECT_TRUE(result.ok) << result.error;
EXPECT_NE(result.response_json.find("\"secret\":\"absent\""), std::string::npos);
EXPECT_NE(result.response_json.find("\"allowed\":\"present\""), std::string::npos);
}
TEST_F(ExternalServiceTest, SandboxWorkDirWhitelist) {
auto& manager = PluginManager::instance();
const fs::path dir = make_helper_service_dir(root_, "com.vde.test.sandbox", {});
std::string error;
ASSERT_GE(manager.scan_service_directory(dir.string(), &error), 1u) << error;
const fs::path sandbox = root_ / "sandbox";
fs::create_directories(sandbox);
const fs::path outside = root_ / "outside_work";
fs::create_directories(outside);
ExternalServiceInvokeOptions options;
options.work_dir = outside.string();
ExternalServiceResult result;
EXPECT_FALSE(manager.invoke_external_service("com.vde.test.sandbox", "{\"operation\":\"echo\"}",
options, &result, &error));
EXPECT_TRUE(result.protocol_error);
EXPECT_NE(result.error.find("whitelist"), std::string::npos);
options.work_dir = (sandbox / "inside_work").string();
result = ExternalServiceResult{};
EXPECT_TRUE(manager.invoke_external_service("com.vde.test.sandbox", "{\"operation\":\"echo\"}",
options, &result, &error))
<< result.error;
EXPECT_TRUE(result.ok);
}
TEST_F(ExternalServiceTest, UnknownServiceAndInvalidRequestRejected) {
auto& manager = PluginManager::instance();
ExternalServiceResult result;
std::string error;
EXPECT_FALSE(manager.invoke_external_service("com.vde.test.unknown", "{\"operation\":\"echo\"}",
&result, &error));
EXPECT_NE(result.error.find("not discovered"), std::string::npos);
const fs::path dir = make_helper_service_dir(root_, "com.vde.test.invalidreq", {});
ASSERT_GE(manager.scan_service_directory(dir.string(), &error), 1u) << error;
result = ExternalServiceResult{};
EXPECT_FALSE(manager.invoke_external_service("com.vde.test.invalidreq", "{bad request", &result,
&error));
EXPECT_TRUE(result.protocol_error);
result = ExternalServiceResult{};
EXPECT_FALSE(manager.invoke_external_service("com.vde.test.invalidreq", "{\"request_id\":\"x\"}",
&result, &error));
EXPECT_TRUE(result.protocol_error);
EXPECT_NE(result.error.find("operation"), std::string::npos);
}
TEST_F(ExternalServiceTest, LimitsAbsentKeepsLegacyBehavior) {
auto& manager = PluginManager::instance();
const fs::path dir = make_helper_service_dir(root_, "com.vde.test.nolimits", {});
std::string error;
ASSERT_GE(manager.scan_service_directory(dir.string(), &error), 1u) << error;
const ExternalServiceResult result =
invoke_helper("com.vde.test.nolimits", make_work_dir("nolimits"));
EXPECT_TRUE(result.ok) << result.error;
EXPECT_FALSE(result.limit_exceeded);
EXPECT_TRUE(result.limit_kind.empty());
}
TEST_F(ExternalServiceTest, InvalidLimitsManifestRejected) {
auto& manager = PluginManager::instance();
const fs::path dir = root_ / "svc_badlimits";
fs::create_directories(dir);
write_text(dir / "vde-service.json",
make_manifest_text("com.vde.test.badlimits", fs::path(VDE_TEST_SERVICE_EXE), 5000,
{}, "{\"max_memory_mb\": \"lots\"}"));
std::string error;
EXPECT_EQ(manager.scan_service_directory(dir.string(), &error), 0u);
EXPECT_NE(error.find("max_memory_mb"), std::string::npos) << error;
manager.clear_external_services();
write_text(dir / "vde-service.json",
make_manifest_text("com.vde.test.badlimits", fs::path(VDE_TEST_SERVICE_EXE), 5000,
{}, "{\"max_cpu_seconds\": -1}"));
EXPECT_EQ(manager.scan_service_directory(dir.string(), &error), 0u);
EXPECT_NE(error.find("max_cpu_seconds"), std::string::npos) << error;
}
TEST_F(ExternalServiceTest, MemoryLimitKillsService) {
auto& manager = PluginManager::instance();
const fs::path dir = make_helper_service_dir(root_, "com.vde.test.mem",
{"VDE_TEST_SERVICE_MODE"}, 15000,
"{\"max_memory_mb\": 64}");
std::string error;
ASSERT_GE(manager.scan_service_directory(dir.string(), &error), 1u) << error;
set_test_env("VDE_TEST_SERVICE_MODE", "memory_hog");
const ExternalServiceResult result = invoke_helper("com.vde.test.mem", make_work_dir("mem"));
EXPECT_FALSE(result.ok);
EXPECT_FALSE(result.timed_out);
EXPECT_LT(result.duration_ms, 10000);
const bool killed_by_limit = result.limit_exceeded && result.limit_kind == "memory";
const bool reported_oom = result.error_code == "oom";
EXPECT_TRUE(killed_by_limit || reported_oom) << "error=" << result.error;
}
TEST_F(ExternalServiceTest, CpuLimitTerminatesService) {
auto& manager = PluginManager::instance();
const fs::path dir = make_helper_service_dir(root_, "com.vde.test.cpu",
{"VDE_TEST_SERVICE_MODE"}, 15000,
"{\"max_cpu_seconds\": 1}");
std::string error;
ASSERT_GE(manager.scan_service_directory(dir.string(), &error), 1u) << error;
set_test_env("VDE_TEST_SERVICE_MODE", "cpu_hog");
const ExternalServiceResult result = invoke_helper("com.vde.test.cpu", make_work_dir("cpu"));
EXPECT_FALSE(result.ok);
EXPECT_FALSE(result.timed_out);
EXPECT_GE(result.duration_ms, 300);
EXPECT_LT(result.duration_ms, 12000);
EXPECT_TRUE(result.limit_exceeded) << "error=" << result.error;
EXPECT_EQ(result.limit_kind, "cpu");
}
TEST_F(ExternalServiceTest, ProcessLimitBlocksChildSpawn) {
auto& manager = PluginManager::instance();
const fs::path dir = make_helper_service_dir(root_, "com.vde.test.proc",
{"VDE_TEST_SERVICE_MODE"}, 5000,
"{\"max_processes\": 1}");
std::string error;
ASSERT_GE(manager.scan_service_directory(dir.string(), &error), 1u) << error;
set_test_env("VDE_TEST_SERVICE_MODE", "spawn_child");
const std::string request =
"{\"request_id\":\"proc-1\",\"operation\":\"spawn\",\"helper_exe\":\"" +
fs::path(VDE_TEST_SERVICE_EXE).generic_string() + "\"}";
const ExternalServiceResult result =
invoke_helper_request("com.vde.test.proc", request, make_work_dir("proc"));
EXPECT_FALSE(result.ok);
EXPECT_FALSE(result.timed_out);
EXPECT_EQ(result.error_code, "spawn_blocked") << "error=" << result.error;
}
TEST_F(ExternalServiceTest, KillOnCloseReapsLeftoverChild) {
auto& manager = PluginManager::instance();
const fs::path dir = make_helper_service_dir(root_, "com.vde.test.killclose",
{"VDE_TEST_SERVICE_MODE"}, 10000,
"{\"kill_on_close\": true, \"max_processes\": 4}");
std::string error;
ASSERT_GE(manager.scan_service_directory(dir.string(), &error), 1u) << error;
const fs::path work = make_work_dir("killclose");
const fs::path pid_file = work / "leftover_child.pid";
set_test_env("VDE_TEST_SERVICE_MODE", "leave_child");
const std::string request =
"{\"request_id\":\"kill-1\",\"operation\":\"leave_child\",\"helper_exe\":\"" +
fs::path(VDE_TEST_SERVICE_EXE).generic_string() + "\",\"pid_file\":\"" +
pid_file.generic_string() + "\"}";
const ExternalServiceResult result =
invoke_helper_request("com.vde.test.killclose", request, work);
EXPECT_TRUE(result.ok) << result.error;
ASSERT_TRUE(fs::exists(pid_file)) << "leftover child pid file missing";
const long long child_pid = read_pid_file(pid_file);
ASSERT_GT(child_pid, 0);
const bool exited = wait_for_process_exit(child_pid, 5000);
if (!exited) terminate_process_best_effort(child_pid);
EXPECT_TRUE(exited) << "leftover child still alive: " << child_pid;
}
// ── P3-14 能力路由 + 类型化适配 ─────────────────────────────────────────
namespace {
std::string make_minimal_gcs_request(const std::string& request_id) {
std::string request = "{\"request_id\":\"" + request_id + "\",\"operation\":\"gcs.solve\",";
request += "\"schema_version\":1,\"sketch\":{\"points\":[{\"id\":\"p1\",\"x\":0,\"y\":0}]},";
request += "\"options\":{}}";
return request;
}
} // namespace
TEST_F(ExternalServiceTest, CapabilityRoutingPrioritySelectsHighest) {
auto& manager = PluginManager::instance();
const fs::path high_dir = make_helper_service_dir(root_, "com.vde.test.route_high", {}, 5000,
std::string(), {"route:test"}, 10);
const fs::path low_dir = make_helper_service_dir(root_, "com.vde.test.route_low", {}, 5000,
std::string(), {"route:test"}, 0);
std::string error;
ASSERT_GE(manager.scan_service_directory(high_dir.string(), &error), 1u) << error;
ASSERT_GE(manager.scan_service_directory(low_dir.string(), &error), 1u) << error;
const auto candidates = manager.services_with_capability("route:test");
ASSERT_EQ(candidates.size(), 2u);
EXPECT_EQ(candidates[0].id, "com.vde.test.route_high");
EXPECT_EQ(candidates[0].priority, 10);
EXPECT_EQ(candidates[1].id, "com.vde.test.route_low");
const auto selected = manager.select_external_service("route:test");
ASSERT_TRUE(selected.has_value());
EXPECT_EQ(selected->id, "com.vde.test.route_high");
ExternalServiceResult result;
std::string selected_id;
ASSERT_TRUE(manager.invoke_external_service_by_capability(
"route:test", "{\"request_id\":\"cap-priority\",\"operation\":\"echo\"}", &result,
&selected_id, &error))
<< result.error << " / " << error;
EXPECT_EQ(selected_id, "com.vde.test.route_high");
EXPECT_TRUE(result.ok);
}
TEST_F(ExternalServiceTest, CapabilityRoutingFallsBackOnFailure) {
auto& manager = PluginManager::instance();
const fs::path bad_dir = make_helper_service_dir(
root_, "com.vde.test.route_bad", {"VDE_TEST_SERVICE_MODE"}, 5000, std::string(),
{"gcs:solve"}, 100);
std::string error;
ASSERT_GE(manager.scan_service_directory(bad_dir.string(), &error), 1u) << error;
ASSERT_GE(manager.scan_service_directory(VDE_EXTERNAL_GCS_SERVICE_DIR, &error), 1u) << error;
set_test_env("VDE_TEST_SERVICE_MODE", "badjson");
ExternalServiceResult result;
std::string selected_id;
ASSERT_TRUE(manager.invoke_external_service_by_capability(
"gcs:solve", make_minimal_gcs_request("cap-fallback"), &result, &selected_id, &error))
<< result.error << " / " << error;
EXPECT_EQ(selected_id, "com.vde.example.gcs_solver");
EXPECT_TRUE(result.ok);
EXPECT_NE(result.response_json.find("\"schema_version\":1"), std::string::npos);
set_test_env("VDE_TEST_SERVICE_MODE", nullptr);
}
TEST_F(ExternalServiceTest, CapabilityRoutingNoCandidateAndUnknownIsolated) {
auto& manager = PluginManager::instance();
std::string error;
ASSERT_GE(manager.scan_service_directory(VDE_EXTERNAL_GCS_SERVICE_DIR, &error), 1u) << error;
ExternalServiceResult result;
std::string selected_id = "sentinel";
EXPECT_FALSE(manager.invoke_external_service_by_capability(
"gcs:missing", "{\"operation\":\"x\"}", &result, &selected_id, &error));
EXPECT_TRUE(selected_id.empty());
EXPECT_NE(error.find("no external service provides capability"), std::string::npos) << error;
EXPECT_TRUE(manager.services_with_capability("gcs:missing").empty());
ASSERT_TRUE(manager.invoke_external_service_by_capability(
"gcs:solve", make_minimal_gcs_request("cap-unknown-2"), &result, &selected_id, &error))
<< result.error << " / " << error;
EXPECT_EQ(selected_id, "com.vde.example.gcs_solver");
}
TEST_F(ExternalServiceTest, CapabilityRoutingAllCandidatesFail) {
auto& manager = PluginManager::instance();
const fs::path bad_dir = make_helper_service_dir(
root_, "com.vde.test.route_only_bad", {"VDE_TEST_SERVICE_MODE"}, 5000, std::string(),
{"route:bad"}, 5);
std::string error;
ASSERT_GE(manager.scan_service_directory(bad_dir.string(), &error), 1u) << error;
set_test_env("VDE_TEST_SERVICE_MODE", "badjson");
ExternalServiceResult result;
std::string selected_id;
EXPECT_FALSE(manager.invoke_external_service_by_capability(
"route:bad", "{\"request_id\":\"all-fail\",\"operation\":\"echo\"}", &result,
&selected_id, &error));
EXPECT_TRUE(selected_id.empty());
EXPECT_TRUE(result.protocol_error);
EXPECT_NE(error.find("all candidate services failed"), std::string::npos) << error;
}
TEST_F(ExternalServiceTest, CapabilityRoutingTimeoutIsolated) {
auto& manager = PluginManager::instance();
std::string error;
ASSERT_GE(manager.scan_service_directory(VDE_EXTERNAL_GCS_SERVICE_DIR, &error), 1u) << error;
std::string slow_request = "{";
slow_request += "\"request_id\":\"cap-timeout\",\"operation\":\"gcs.solve\",";
slow_request += "\"schema_version\":1,\"sketch\":{\"points\":[{\"id\":\"p1\",\"x\":0,\"y\":0}]},";
slow_request += "\"options\":{\"sleep_ms\":5000}}";
ExternalServiceInvokeOptions options;
options.timeout_ms = 200;
ExternalServiceResult result;
std::string selected_id;
EXPECT_FALSE(manager.invoke_external_service_by_capability(
"gcs:solve", slow_request, options, &result, &selected_id, &error));
EXPECT_TRUE(result.timed_out);
EXPECT_LT(result.duration_ms, 5000);
ExternalServiceResult recovered;
ASSERT_TRUE(manager.invoke_external_service_by_capability(
"gcs:solve", make_minimal_gcs_request("cap-timeout-2"), &recovered, &selected_id, &error))
<< recovered.error << " / " << error;
EXPECT_TRUE(recovered.ok);
}
TEST_F(ExternalServiceTest, CapabilityPriorityManifestValidation) {
auto& manager = PluginManager::instance();
const fs::path dir = root_ / "svc_badpriority";
fs::create_directories(dir);
std::string manifest =
make_manifest_text("com.vde.test.badpriority", fs::path(VDE_TEST_SERVICE_EXE), 5000, {});
const std::string anchor = "\"version\": \"0.1.0\",\n";
const std::size_t pos = manifest.find(anchor);
ASSERT_NE(pos, std::string::npos);
manifest.insert(pos + anchor.size(), " \"priority\": \"high\",\n");
write_text(dir / "vde-service.json", manifest);
std::string error;
EXPECT_EQ(manager.scan_service_directory(dir.string(), &error), 0u);
EXPECT_NE(error.find("priority"), std::string::npos) << error;
}
TEST_F(ExternalServiceTest, GcsTypedAdapterEndToEnd) {
auto& manager = PluginManager::instance();
std::string error;
ASSERT_GE(manager.scan_service_directory(VDE_EXTERNAL_GCS_SERVICE_DIR, &error), 1u) << error;
vde::plugin::GcsSolveRequest request;
request.solver = "vdcs";
request.points.push_back({"p1", 0.0, 0.0, true});
request.points.push_back({"p2", 10.0, 0.0, false});
request.lines.push_back({"l1", "p1", "p2"});
vde::plugin::GcsSolveConstraint constraint;
constraint.id = "c1";
constraint.type = "horizontal";
constraint.refs = {"l1"};
request.constraints.push_back(constraint);
vde::plugin::GcsSolveParameter parameter;
parameter.name = "p2.x";
parameter.value = 20.0;
parameter.driving = true;
request.parameters.push_back(parameter);
request.max_iterations = 50;
request.tolerance = 1e-6;
vde::plugin::GcsSolveResponse response;
std::string selected_id;
ASSERT_TRUE(vde::plugin::solve_gcs_service_by_capability(manager, request, &response,
&selected_id, &error))
<< error;
EXPECT_EQ(selected_id, "com.vde.example.gcs_solver");
EXPECT_TRUE(response.ok);
EXPECT_EQ(response.schema_version, 1);
EXPECT_EQ(response.status, "ok");
EXPECT_EQ(response.solve_status, "ok");
ASSERT_EQ(response.positions.size(), 2u);
EXPECT_DOUBLE_EQ(response.positions["p2"].x, 20.0);
EXPECT_DOUBLE_EQ(response.positions["p2"].y, 0.0);
EXPECT_EQ(response.dof.total, 4);
EXPECT_EQ(response.dof.remaining, 3);
EXPECT_FALSE(response.dof.over_constrained);
ASSERT_EQ(response.constraint_status.size(), 1u);
EXPECT_EQ(response.constraint_status[0].id, "c1");
EXPECT_EQ(response.constraint_status[0].status, "ok");
EXPECT_FALSE(response.diagnostics.empty());
EXPECT_EQ(response.metric_points, 2);
EXPECT_FALSE(response.raw_response_json.empty());
}
TEST_F(ExternalServiceTest, GcsTypedAdapterByServiceAndFailureIsolation) {
auto& manager = PluginManager::instance();
std::string error;
ASSERT_GE(manager.scan_service_directory(VDE_EXTERNAL_GCS_SERVICE_DIR, &error), 1u) << error;
vde::plugin::GcsSolveRequest request;
request.points.push_back({"p1", 0.0, 0.0, true});
request.points.push_back({"p2", 5.0, 0.0, false});
vde::plugin::GcsSolveResponse response;
ASSERT_TRUE(vde::plugin::solve_gcs_service(manager, "com.vde.example.gcs_solver", request,
&response, &error))
<< error;
EXPECT_TRUE(response.ok);
EXPECT_EQ(response.positions.size(), 2u);
vde::plugin::GcsSolveResponse unknown_response;
EXPECT_FALSE(vde::plugin::solve_gcs_service(manager, "com.vde.test.no_such_gcs", request,
&unknown_response, &error));
EXPECT_FALSE(error.empty());
const fs::path bad_dir = make_helper_service_dir(
root_, "com.vde.test.gcs_bad_adapter", {"VDE_TEST_SERVICE_MODE"}, 5000, std::string(),
{"gcs:solve"});
ASSERT_GE(manager.scan_service_directory(bad_dir.string(), &error), 1u) << error;
set_test_env("VDE_TEST_SERVICE_MODE", "badjson");
vde::plugin::GcsSolveResponse bad_response;
EXPECT_FALSE(vde::plugin::solve_gcs_service(manager, "com.vde.test.gcs_bad_adapter", request,
&bad_response, &error));
EXPECT_FALSE(error.empty());
set_test_env("VDE_TEST_SERVICE_MODE", nullptr);
vde::plugin::GcsSolveResponse recovered;
ASSERT_TRUE(vde::plugin::solve_gcs_service(manager, "com.vde.example.gcs_solver", request,
&recovered, &error))
<< error;
EXPECT_TRUE(recovered.ok);
}
TEST_F(ExternalServiceTest, JsonAdapterForCaeCamCapabilities) {
auto& manager = PluginManager::instance();
const fs::path dir = make_helper_service_dir(
root_, "com.vde.test.cae_cam", {"VDE_TEST_SERVICE_MODE"}, 5000, std::string(),
{"cae:solve", "cam:plan"}, 1);
std::string error;
ASSERT_GE(manager.scan_service_directory(dir.string(), &error), 1u) << error;
vde::plugin::ExternalServiceJsonRequest request;
request.operation = "cae.solve";
request.payload_json = "{\"model\":\"vde-test\",\"params\":{\"mesh\":\"tets\"}}";
vde::plugin::ExternalServiceJsonResponse response;
std::string selected_id;
ASSERT_TRUE(vde::plugin::invoke_json_service_by_capability(
manager, "cae:solve", request, &response, &selected_id, &error))
<< error;
EXPECT_EQ(selected_id, "com.vde.test.cae_cam");
EXPECT_TRUE(response.ok);
EXPECT_EQ(response.status, "ok");
EXPECT_FALSE(response.response_json.empty());
request.operation = "cam.plan";
vde::plugin::ExternalServiceJsonResponse cam_response;
selected_id.clear();
ASSERT_TRUE(vde::plugin::invoke_json_service_by_capability(
manager, "cam:plan", request, &cam_response, &selected_id, &error))
<< error;
EXPECT_EQ(selected_id, "com.vde.test.cae_cam");
EXPECT_TRUE(cam_response.ok);
vde::plugin::ExternalServiceJsonResponse missing_response;
EXPECT_FALSE(vde::plugin::invoke_json_service_by_capability(
manager, "cam:missing", request, &missing_response, &selected_id, &error));
EXPECT_TRUE(selected_id.empty());
EXPECT_NE(error.find("no external service provides capability"), std::string::npos) << error;
EXPECT_FALSE(vde::plugin::invoke_json_service(manager, "com.vde.test.unknown", request,
&missing_response, &error));
}
// ── P3-14b 沙箱策略契约 ─────────────────────────────────────────────────
TEST_F(ExternalServiceTest, SandboxPolicyParsingAndValidation) {
auto& manager = PluginManager::instance();
const fs::path valid_dir = make_policy_service_dir(
root_, "com.vde.test.policy_valid", {"VDE_TEST_SERVICE_MODE"},
"\"network\": \"inherit\", \"filesystem\": \"read_only\", \"isolation\": \"process\", "
"\"allow_unsandboxed\": true");
std::string error;
ASSERT_GE(manager.scan_service_directory(valid_dir.string(), &error), 1u) << error;
const auto info = manager.external_service("com.vde.test.policy_valid");
ASSERT_TRUE(info.has_value());
EXPECT_TRUE(info->sandbox_policy.declared);
EXPECT_EQ(info->sandbox_policy.network, "inherit");
EXPECT_EQ(info->sandbox_policy.filesystem, "read_only");
EXPECT_EQ(info->sandbox_policy.isolation, "process");
EXPECT_TRUE(info->sandbox_policy.allow_unsandboxed);
manager.clear_external_services();
const fs::path bad_network = make_policy_service_dir(
root_, "com.vde.test.policy_bad_net", {}, "\"network\": \"blocked\"");
EXPECT_EQ(manager.scan_service_directory(bad_network.string(), &error), 0u);
EXPECT_NE(error.find("sandbox.network"), std::string::npos) << error;
manager.clear_external_services();
const fs::path bad_filesystem = make_policy_service_dir(
root_, "com.vde.test.policy_bad_fs", {}, "\"filesystem\": \"readonly\"");
EXPECT_EQ(manager.scan_service_directory(bad_filesystem.string(), &error), 0u);
EXPECT_NE(error.find("sandbox.filesystem"), std::string::npos) << error;
manager.clear_external_services();
const fs::path bad_isolation = make_policy_service_dir(
root_, "com.vde.test.policy_bad_iso", {}, "\"isolation\": \"container\"");
EXPECT_EQ(manager.scan_service_directory(bad_isolation.string(), &error), 0u);
EXPECT_NE(error.find("sandbox.isolation"), std::string::npos) << error;
}
TEST_F(ExternalServiceTest, SandboxPolicyLegacyManifestsKeepBehavior) {
auto& manager = PluginManager::instance();
const fs::path dir =
make_helper_service_dir(root_, "com.vde.test.legacy_sandbox", {"VDE_TEST_SERVICE_MODE"});
std::string error;
ASSERT_GE(manager.scan_service_directory(dir.string(), &error), 1u) << error;
set_test_env("VDE_TEST_SERVICE_MODE", "ok");
const ExternalServiceResult result =
invoke_helper("com.vde.test.legacy_sandbox", make_work_dir("legacy_sandbox"));
EXPECT_TRUE(result.ok) << result.error;
EXPECT_FALSE(result.sandbox_enforced);
EXPECT_FALSE(result.sandbox_unsupported);
EXPECT_FALSE(result.sandbox_downgraded);
}
TEST_F(ExternalServiceTest, SandboxPolicyNetworkNoneRejectedOrEnforced) {
auto& manager = PluginManager::instance();
const fs::path dir = make_policy_service_dir(
root_, "com.vde.test.policy_net_none", {"VDE_TEST_SERVICE_MODE"},
"\"network\": \"none\"");
std::string error;
ASSERT_GE(manager.scan_service_directory(dir.string(), &error), 1u) << error;
set_test_env("VDE_TEST_SERVICE_MODE", "ok");
const ExternalServiceResult result =
invoke_helper("com.vde.test.policy_net_none", make_work_dir("policy_net_none"));
#if defined(_WIN32)
EXPECT_FALSE(result.ok);
EXPECT_TRUE(result.sandbox_unsupported);
EXPECT_EQ(result.error_code, "unsupported_sandbox");
EXPECT_NE(result.error.find("unsupported_sandbox"), std::string::npos);
#else
// POSIX:允许 unshare 的平台应强制成功;无权限时必须明确 unsupported_sandbox
if (result.ok) {
EXPECT_TRUE(result.sandbox_enforced);
} else {
EXPECT_TRUE(result.sandbox_unsupported);
EXPECT_EQ(result.error_code, "unsupported_sandbox");
}
#endif
}
TEST_F(ExternalServiceTest, SandboxPolicyAllowUnsandboxedEscape) {
auto& manager = PluginManager::instance();
const fs::path dir = make_policy_service_dir(
root_, "com.vde.test.policy_escape", {"VDE_TEST_SERVICE_MODE"},
"\"network\": \"none\", \"allow_unsandboxed\": true");
std::string error;
ASSERT_GE(manager.scan_service_directory(dir.string(), &error), 1u) << error;
set_test_env("VDE_TEST_SERVICE_MODE", "ok");
const ExternalServiceResult result =
invoke_helper("com.vde.test.policy_escape", make_work_dir("policy_escape"));
#if defined(_WIN32)
EXPECT_TRUE(result.ok) << result.error;
EXPECT_FALSE(result.sandbox_enforced);
EXPECT_TRUE(result.sandbox_downgraded);
EXPECT_FALSE(result.sandbox_warnings.empty());
EXPECT_FALSE(result.sandbox_unsupported);
#else
// POSIX:若 unshare 成功则 enforced;否则降级
EXPECT_TRUE(result.ok);
EXPECT_TRUE(result.sandbox_enforced || result.sandbox_downgraded);
if (result.sandbox_downgraded) EXPECT_FALSE(result.sandbox_warnings.empty());
#endif
}
TEST_F(ExternalServiceTest, SandboxPolicyReadOnlyWorkdirBlocksWrites) {
auto& manager = PluginManager::instance();
const fs::path dir = make_policy_service_dir(
root_, "com.vde.test.policy_readonly", {"VDE_TEST_SERVICE_MODE"},
"\"filesystem\": \"read_only\"");
std::string error;
ASSERT_GE(manager.scan_service_directory(dir.string(), &error), 1u) << error;
set_test_env("VDE_TEST_SERVICE_MODE", "write_probe");
const ExternalServiceResult result =
invoke_helper("com.vde.test.policy_readonly", make_work_dir("policy_readonly"));
if (result.sandbox_unsupported) {
EXPECT_FALSE(result.ok);
EXPECT_EQ(result.error_code, "unsupported_sandbox");
} else {
EXPECT_TRUE(result.ok) << result.error << " stderr=" << result.stderr_excerpt
<< " stdout=" << result.stdout_excerpt;
EXPECT_NE(result.response_json.find("\"write_blocked\":\"yes\""), std::string::npos)
<< result.response_json;
}
}
TEST_F(ExternalServiceTest, SandboxPolicyReadOnlyAllowsResponseWrite) {
auto& manager = PluginManager::instance();
const fs::path dir = make_policy_service_dir(
root_, "com.vde.test.policy_readonly_ok", {"VDE_TEST_SERVICE_MODE"},
"\"filesystem\": \"read_only\"");
std::string error;
ASSERT_GE(manager.scan_service_directory(dir.string(), &error), 1u) << error;
set_test_env("VDE_TEST_SERVICE_MODE", "ok");
const ExternalServiceResult result =
invoke_helper("com.vde.test.policy_readonly_ok", make_work_dir("policy_readonly_ok"));
if (result.sandbox_unsupported) {
EXPECT_TRUE(result.sandbox_unsupported);
EXPECT_EQ(result.error_code, "unsupported_sandbox");
} else {
EXPECT_TRUE(result.ok) << result.error << " stderr=" << result.stderr_excerpt;
EXPECT_TRUE(result.sandbox_enforced);
}
}
TEST_F(ExternalServiceTest, SandboxPolicyIsolationProcessOrNoneDeclared) {
auto& manager = PluginManager::instance();
const fs::path process_dir = make_policy_service_dir(
root_, "com.vde.test.policy_iso_process", {"VDE_TEST_SERVICE_MODE"},
"\"isolation\": \"process\"");
std::string error;
ASSERT_GE(manager.scan_service_directory(process_dir.string(), &error), 1u) << error;
set_test_env("VDE_TEST_SERVICE_MODE", "ok");
ExternalServiceResult result =
invoke_helper("com.vde.test.policy_iso_process", make_work_dir("iso_process"));
EXPECT_TRUE(result.ok) << result.error;
EXPECT_TRUE(result.sandbox_enforced);
manager.clear_external_services();
const fs::path none_dir = make_policy_service_dir(
root_, "com.vde.test.policy_iso_none", {"VDE_TEST_SERVICE_MODE"}, "\"isolation\": \"none\"");
ASSERT_GE(manager.scan_service_directory(none_dir.string(), &error), 1u) << error;
result = invoke_helper("com.vde.test.policy_iso_none", make_work_dir("iso_none"));
EXPECT_TRUE(result.ok) << result.error;
EXPECT_TRUE(result.sandbox_enforced);
}
} // namespace