- P0-2/P0-3 插件管理器实现(src/plugin/plugin_manager.cpp):严格语义化版本、vde-plugin.json 解析与 ABI/平台 entry/库存在性校验、CapabilityRegistry(冲突记录)、发现/加载/卸载/失败隔离/格式分派/ 线程安全;tests/plugin/test_plugin_manager.cpp 29 用例全绿; - P0-4 宿主几何服务表(src/plugin/host_services_geometry.cpp,编入 vde_io):alloc/log/set_error、 box/cylinder/sphere、VDE-TEXT/二进制序列化往返、query/volume/area/bbox/tessellate/destroy/free_buffer; - P0-5 示例插件 plugins/sample_vbox(.vbox 文本格式,纯 C ABI 不链接内核)+ 端到端测试 12 项全绿; - P0-6 CMake:vde::kernel 分层聚合、VDE_BUILD_PLUGIN/SAMPLE_PLUGINS/INSTALL_SDK/TOOLS 开关、 kernel SDK install/export(find_package(vde-kernel CONFIG),12 库/231 头/consumer 验证)、 -DVDE_BUILD_PLUGIN=OFF kernel-only 可构建; - tools/vde_plugin_tool(--list/--inspect/--abi); - docs/design/microkernel-plugin-architecture.md(P0..P3 计划)+ docs/guides/plugin-development.md + docs/guides/architecture.md 微内核章节。 验证:全量 ctest 7249/7249 PASS(0 failed、22 skipped、406.31s);插件测试 41 项(29+12)全绿; kernel SDK consumer find_package + 编译运行 PASS;-DVDE_BUILD_PLUGIN=OFF / -DVDE_BUILD_SAMPLE_PLUGINS=OFF 可构建。
596 lines
24 KiB
C++
596 lines
24 KiB
C++
// P0-4 / P0-5 集成测试:宿主几何服务表直测 + sample_vbox 插件端到端
|
||
// (加载 → formats → import → 查询 → export → 再 import → 卸载 → 错误路径)
|
||
#include <gtest/gtest.h>
|
||
|
||
#include <algorithm>
|
||
#include <cmath>
|
||
#include <cstdio>
|
||
#include <cstring>
|
||
#include <fstream>
|
||
#include <iterator>
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
#include "vde/plugin/geometry_host_services.h"
|
||
#include "vde/plugin/plugin_manager.h"
|
||
#include "vde/plugin/vde_plugin_api.h"
|
||
#include "vde/foundation/vde_format.h"
|
||
#include "vde_platform/platform.h"
|
||
|
||
namespace {
|
||
|
||
using vde::plugin::FormatImportResult;
|
||
using vde::plugin::PluginManager;
|
||
|
||
constexpr double kPi = 3.14159265358979323846;
|
||
constexpr const char* kSamplePluginId = "com.vde.sample.vbox";
|
||
|
||
std::string temp_path(const char* name) {
|
||
return std::string(VDE_TEST_TEMP_DIR) + name;
|
||
}
|
||
|
||
bool write_text_file(const std::string& path, const std::string& text) {
|
||
std::ofstream out(path, std::ios::binary | std::ios::trunc);
|
||
if (!out.good()) return false;
|
||
out.write(text.data(), static_cast<std::streamsize>(text.size()));
|
||
out.close();
|
||
return out.good();
|
||
}
|
||
|
||
std::string read_text_file(const std::string& path) {
|
||
std::ifstream in(path, std::ios::binary);
|
||
if (!in.good()) return {};
|
||
return std::string((std::istreambuf_iterator<char>(in)),
|
||
std::istreambuf_iterator<char>());
|
||
}
|
||
|
||
bool near_rel(double a, double b, double rel) {
|
||
const double scale = std::max(std::abs(a), std::abs(b));
|
||
if (scale <= 0.0) return std::abs(a - b) <= rel;
|
||
return std::abs(a - b) / scale <= rel;
|
||
}
|
||
|
||
struct HostFixture {
|
||
VdePluginHost host = vde::plugin::make_geometry_host_services();
|
||
~HostFixture() { vde::plugin::release_geometry_host_services(&host); }
|
||
};
|
||
|
||
void destroy_bodies(const VdePluginHost& host, std::vector<VdeBody>& bodies) {
|
||
for (VdeBody body : bodies) host.body_destroy(body);
|
||
bodies.clear();
|
||
}
|
||
|
||
/** 把 PluginManager 恢复成干净状态(单例跨用例复用) */
|
||
void reset_manager(const VdePluginHost& host) {
|
||
PluginManager& pm = PluginManager::instance();
|
||
pm.clear();
|
||
pm.set_host_services(host);
|
||
pm.clear_search_paths();
|
||
pm.add_search_path(VDE_PLUGIN_SAMPLE_DIR);
|
||
pm.scan_search_paths();
|
||
}
|
||
|
||
bool load_sample_plugin(std::string* error) {
|
||
PluginManager& pm = PluginManager::instance();
|
||
if (pm.is_loaded(kSamplePluginId)) return true;
|
||
return pm.load(kSamplePluginId, error);
|
||
}
|
||
|
||
bool import_vbox_file(const std::string& path, FormatImportResult* result) {
|
||
PluginManager& pm = PluginManager::instance();
|
||
return pm.import_file(path, result);
|
||
}
|
||
|
||
} // namespace
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 1. 宿主几何服务表直测(P0-4)
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(PluginHostServices, TableIsComplete) {
|
||
HostFixture fixture;
|
||
const VdePluginHost& host = fixture.host;
|
||
EXPECT_EQ(host.struct_size, sizeof(VdePluginHost));
|
||
EXPECT_EQ(host.abi_major, VDE_PLUGIN_ABI_MAJOR);
|
||
EXPECT_EQ(host.abi_minor, VDE_PLUGIN_ABI_MINOR);
|
||
ASSERT_NE(host.kernel_version, nullptr);
|
||
EXPECT_GT(std::strlen(host.kernel_version), 0u);
|
||
|
||
EXPECT_NE(host.alloc, nullptr);
|
||
EXPECT_NE(host.realloc, nullptr);
|
||
EXPECT_NE(host.free, nullptr);
|
||
EXPECT_NE(host.log, nullptr);
|
||
EXPECT_NE(host.set_error, nullptr);
|
||
EXPECT_NE(host.body_create_box, nullptr);
|
||
EXPECT_NE(host.body_create_cylinder, nullptr);
|
||
EXPECT_NE(host.body_create_sphere, nullptr);
|
||
EXPECT_NE(host.body_from_serialized, nullptr);
|
||
EXPECT_NE(host.body_query, nullptr);
|
||
EXPECT_NE(host.body_volume, nullptr);
|
||
EXPECT_NE(host.body_area, nullptr);
|
||
EXPECT_NE(host.body_bbox, nullptr);
|
||
EXPECT_NE(host.body_tessellate, nullptr);
|
||
EXPECT_NE(host.body_serialize, nullptr);
|
||
EXPECT_NE(host.body_destroy, nullptr);
|
||
EXPECT_NE(host.free_buffer, nullptr);
|
||
}
|
||
|
||
TEST(PluginHostServices, BoxQueryVolumeAreaBBox) {
|
||
HostFixture fixture;
|
||
const VdePluginHost& host = fixture.host;
|
||
vde::plugin::clear_geometry_host_last_error();
|
||
|
||
VdeBody body = host.body_create_box(20.0, 30.0, 40.0);
|
||
ASSERT_NE(body, nullptr) << vde::plugin::geometry_host_last_error();
|
||
|
||
VdeBodyInfo info{};
|
||
info.struct_size = sizeof(VdeBodyInfo);
|
||
ASSERT_EQ(host.body_query(body, &info), VDE_PLUGIN_OK)
|
||
<< vde::plugin::geometry_host_last_error();
|
||
EXPECT_EQ(info.num_faces, 6u);
|
||
EXPECT_EQ(info.num_edges, 12u);
|
||
EXPECT_EQ(info.num_vertices, 8u);
|
||
EXPECT_EQ(info.is_valid, 1u);
|
||
EXPECT_TRUE(near_rel(info.volume, 24000.0, 1e-9)) << info.volume;
|
||
EXPECT_TRUE(near_rel(info.surface_area, 5200.0, 1e-9)) << info.surface_area;
|
||
EXPECT_NEAR(info.bbox_min[0], -10.0, 1e-9);
|
||
EXPECT_NEAR(info.bbox_min[1], -15.0, 1e-9);
|
||
EXPECT_NEAR(info.bbox_min[2], -20.0, 1e-9);
|
||
EXPECT_NEAR(info.bbox_max[0], 10.0, 1e-9);
|
||
EXPECT_NEAR(info.bbox_max[1], 15.0, 1e-9);
|
||
EXPECT_NEAR(info.bbox_max[2], 20.0, 1e-9);
|
||
|
||
double volume = 0.0;
|
||
ASSERT_EQ(host.body_volume(body, &volume), VDE_PLUGIN_OK);
|
||
EXPECT_TRUE(near_rel(volume, info.volume, 1e-12));
|
||
double area = 0.0;
|
||
ASSERT_EQ(host.body_area(body, &area), VDE_PLUGIN_OK);
|
||
EXPECT_TRUE(near_rel(area, info.surface_area, 1e-12));
|
||
double lo[3] = {0.0, 0.0, 0.0};
|
||
double hi[3] = {0.0, 0.0, 0.0};
|
||
ASSERT_EQ(host.body_bbox(body, lo, hi), VDE_PLUGIN_OK);
|
||
EXPECT_NEAR(lo[2], -20.0, 1e-9);
|
||
EXPECT_NEAR(hi[2], 20.0, 1e-9);
|
||
|
||
host.body_destroy(body);
|
||
}
|
||
|
||
TEST(PluginHostServices, CylinderAndSphere) {
|
||
HostFixture fixture;
|
||
const VdePluginHost& host = fixture.host;
|
||
|
||
// 解析圆柱(segments=0):体积 ≈ πr²h,包围盒 ±r/±h/2
|
||
VdeBody cylinder = host.body_create_cylinder(2.0, 5.0, 0);
|
||
ASSERT_NE(cylinder, nullptr) << vde::plugin::geometry_host_last_error();
|
||
VdeBodyInfo info{};
|
||
info.struct_size = sizeof(VdeBodyInfo);
|
||
ASSERT_EQ(host.body_query(cylinder, &info), VDE_PLUGIN_OK);
|
||
EXPECT_TRUE(near_rel(info.volume, kPi * 4.0 * 5.0, 0.01)) << info.volume;
|
||
EXPECT_NEAR(info.bbox_min[0], -2.0, 1e-6);
|
||
EXPECT_NEAR(info.bbox_max[1], 2.0, 1e-6);
|
||
EXPECT_NEAR(info.bbox_min[2], -2.5, 1e-6);
|
||
EXPECT_NEAR(info.bbox_max[2], 2.5, 1e-6);
|
||
host.body_destroy(cylinder);
|
||
|
||
VdeBody sphere = host.body_create_sphere(3.0, 32, 16);
|
||
ASSERT_NE(sphere, nullptr) << vde::plugin::geometry_host_last_error();
|
||
info = VdeBodyInfo{};
|
||
info.struct_size = sizeof(VdeBodyInfo);
|
||
ASSERT_EQ(host.body_query(sphere, &info), VDE_PLUGIN_OK);
|
||
EXPECT_TRUE(near_rel(info.volume, 4.0 / 3.0 * kPi * 27.0, 0.02)) << info.volume;
|
||
EXPECT_NEAR(info.bbox_min[0], -3.0, 1e-6);
|
||
EXPECT_NEAR(info.bbox_max[2], 3.0, 1e-6);
|
||
host.body_destroy(sphere);
|
||
}
|
||
|
||
TEST(PluginHostServices, TessellateProducesHostOwnedMesh) {
|
||
HostFixture fixture;
|
||
const VdePluginHost& host = fixture.host;
|
||
VdeBody body = host.body_create_box(10.0, 20.0, 30.0);
|
||
ASSERT_NE(body, nullptr);
|
||
|
||
VdeMeshData mesh{};
|
||
mesh.struct_size = sizeof(VdeMeshData);
|
||
ASSERT_EQ(host.body_tessellate(body, 0.02, &mesh), VDE_PLUGIN_OK)
|
||
<< vde::plugin::geometry_host_last_error();
|
||
ASSERT_NE(mesh.positions, nullptr);
|
||
ASSERT_NE(mesh.indices, nullptr);
|
||
EXPECT_GT(mesh.num_vertices, 0u);
|
||
EXPECT_GT(mesh.num_triangles, 0u);
|
||
EXPECT_NE(mesh.normals, nullptr);
|
||
|
||
for (size_t i = 0; i < mesh.num_triangles * 3; ++i) {
|
||
EXPECT_LT(mesh.indices[i], mesh.num_vertices);
|
||
}
|
||
for (size_t i = 0; i < mesh.num_vertices; ++i) {
|
||
EXPECT_LE(std::abs(mesh.positions[3 * i + 0]), 5.0 + 1e-6);
|
||
EXPECT_LE(std::abs(mesh.positions[3 * i + 1]), 10.0 + 1e-6);
|
||
EXPECT_LE(std::abs(mesh.positions[3 * i + 2]), 15.0 + 1e-6);
|
||
const double nx = mesh.normals[3 * i + 0];
|
||
const double ny = mesh.normals[3 * i + 1];
|
||
const double nz = mesh.normals[3 * i + 2];
|
||
const double length = std::sqrt(nx * nx + ny * ny + nz * nz);
|
||
EXPECT_TRUE(std::isfinite(length));
|
||
EXPECT_NEAR(length, 1.0, 1e-3) << "per-vertex normal 应为单位向量";
|
||
}
|
||
|
||
host.free_buffer(mesh.positions);
|
||
host.free_buffer(mesh.indices);
|
||
host.free_buffer(mesh.normals);
|
||
|
||
// 再次三角化(确认释放语义与可重入)
|
||
VdeMeshData mesh2{};
|
||
mesh2.struct_size = sizeof(VdeMeshData);
|
||
ASSERT_EQ(host.body_tessellate(body, 0.05, &mesh2), VDE_PLUGIN_OK);
|
||
EXPECT_NE(mesh2.positions, nullptr);
|
||
host.free_buffer(mesh2.positions);
|
||
host.free_buffer(mesh2.indices);
|
||
host.free_buffer(mesh2.normals);
|
||
host.body_destroy(body);
|
||
}
|
||
|
||
TEST(PluginHostServices, SerializeRoundTrip) {
|
||
HostFixture fixture;
|
||
const VdePluginHost& host = fixture.host;
|
||
vde::plugin::clear_geometry_host_last_error();
|
||
|
||
VdeBody original = host.body_create_box(20.0, 30.0, 40.0);
|
||
ASSERT_NE(original, nullptr);
|
||
|
||
VdeBuffer buffer{nullptr, 0};
|
||
ASSERT_EQ(host.body_serialize(original, &buffer), VDE_PLUGIN_OK)
|
||
<< vde::plugin::geometry_host_last_error();
|
||
ASSERT_NE(buffer.data, nullptr);
|
||
EXPECT_GT(buffer.size, 0u);
|
||
const std::string text(static_cast<const char*>(buffer.data), buffer.size);
|
||
EXPECT_NE(text.find("VDE-TEXT"), std::string::npos)
|
||
<< "body_serialize 应输出 VDE-TEXT 1.0";
|
||
|
||
VdeBody restored = host.body_from_serialized(buffer.data, buffer.size);
|
||
host.free_buffer(buffer.data);
|
||
ASSERT_NE(restored, nullptr) << vde::plugin::geometry_host_last_error();
|
||
|
||
VdeBodyInfo a{};
|
||
a.struct_size = sizeof(VdeBodyInfo);
|
||
VdeBodyInfo b{};
|
||
b.struct_size = sizeof(VdeBodyInfo);
|
||
ASSERT_EQ(host.body_query(original, &a), VDE_PLUGIN_OK);
|
||
ASSERT_EQ(host.body_query(restored, &b), VDE_PLUGIN_OK);
|
||
EXPECT_EQ(a.num_faces, b.num_faces);
|
||
EXPECT_EQ(a.num_edges, b.num_edges);
|
||
EXPECT_EQ(a.num_vertices, b.num_vertices);
|
||
EXPECT_TRUE(near_rel(a.volume, b.volume, 1e-9)) << a.volume << " vs " << b.volume;
|
||
for (int i = 0; i < 3; ++i) {
|
||
EXPECT_NEAR(a.bbox_min[i], b.bbox_min[i], 1e-9);
|
||
EXPECT_NEAR(a.bbox_max[i], b.bbox_max[i], 1e-9);
|
||
}
|
||
host.body_destroy(restored);
|
||
host.body_destroy(original);
|
||
|
||
// 非法序列化数据 → nullptr + 错误信息
|
||
vde::plugin::clear_geometry_host_last_error();
|
||
const char* bogus = "this is not a vde document";
|
||
VdeBody bad = host.body_from_serialized(bogus, std::strlen(bogus));
|
||
EXPECT_EQ(bad, nullptr);
|
||
EXPECT_GT(std::strlen(vde::plugin::geometry_host_last_error()), 0u);
|
||
}
|
||
|
||
TEST(PluginHostServices, ErrorPathsDoNotCrash) {
|
||
HostFixture fixture;
|
||
const VdePluginHost& host = fixture.host;
|
||
|
||
vde::plugin::clear_geometry_host_last_error();
|
||
EXPECT_EQ(host.body_create_box(0.0, 1.0, 1.0), nullptr);
|
||
EXPECT_GT(std::strlen(vde::plugin::geometry_host_last_error()), 0u);
|
||
|
||
vde::plugin::clear_geometry_host_last_error();
|
||
EXPECT_EQ(host.body_create_box(-1.0, 2.0, 3.0), nullptr);
|
||
EXPECT_GT(std::strlen(vde::plugin::geometry_host_last_error()), 0u);
|
||
|
||
vde::plugin::clear_geometry_host_last_error();
|
||
EXPECT_EQ(host.body_create_cylinder(0.0, 1.0, 8), nullptr);
|
||
EXPECT_GT(std::strlen(vde::plugin::geometry_host_last_error()), 0u);
|
||
|
||
VdeBodyInfo info{};
|
||
info.struct_size = sizeof(VdeBodyInfo);
|
||
EXPECT_LT(host.body_query(nullptr, &info), 0);
|
||
EXPECT_LT(host.body_tessellate(nullptr, 0.01, nullptr), 0);
|
||
EXPECT_LT(host.body_serialize(nullptr, nullptr), 0);
|
||
double value = 0.0;
|
||
EXPECT_LT(host.body_volume(nullptr, &value), 0);
|
||
EXPECT_LT(host.body_area(nullptr, &value), 0);
|
||
EXPECT_LT(host.body_bbox(nullptr, nullptr, nullptr), 0);
|
||
|
||
host.body_destroy(nullptr);
|
||
host.free_buffer(nullptr);
|
||
SUCCEED();
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 2. sample_vbox 插件端到端(P0-5;依赖 PluginManager 实现)
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(PluginIntegration, LoadAndRegisterFormat) {
|
||
HostFixture fixture;
|
||
reset_manager(fixture.host);
|
||
PluginManager& pm = PluginManager::instance();
|
||
|
||
// 发现阶段:清单应已被扫描到(P0-2/P0-3)
|
||
bool discovered = false;
|
||
for (const auto& manifest : pm.plugins()) {
|
||
if (manifest.id == kSamplePluginId) {
|
||
discovered = true;
|
||
EXPECT_EQ(manifest.kind, vde::plugin::PluginKind::Format);
|
||
EXPECT_FALSE(manifest.library_path.empty());
|
||
}
|
||
}
|
||
EXPECT_TRUE(discovered) << "scan_search_paths 应发现 sample_vbox 清单";
|
||
|
||
std::string error;
|
||
ASSERT_TRUE(load_sample_plugin(&error)) << error << " / " << pm.last_error();
|
||
EXPECT_TRUE(pm.is_loaded(kSamplePluginId));
|
||
EXPECT_TRUE(pm.has_host_services());
|
||
|
||
const auto formats = pm.formats();
|
||
bool found = false;
|
||
for (const auto& format : formats) {
|
||
if (format.format_id == "vbox") {
|
||
found = true;
|
||
EXPECT_EQ(format.plugin_id, kSamplePluginId);
|
||
EXPECT_NE(format.extensions.find(".vbox"), std::string::npos);
|
||
}
|
||
}
|
||
EXPECT_TRUE(found) << "formats() 应包含 vbox";
|
||
EXPECT_TRUE(pm.capabilities().has("format:vbox.read"));
|
||
EXPECT_TRUE(pm.capabilities().has("format:vbox.write"));
|
||
|
||
pm.clear();
|
||
}
|
||
|
||
TEST(PluginIntegration, ImportExportRoundTrip) {
|
||
HostFixture fixture;
|
||
reset_manager(fixture.host);
|
||
PluginManager& pm = PluginManager::instance();
|
||
std::string error;
|
||
ASSERT_TRUE(load_sample_plugin(&error)) << error << " / " << pm.last_error();
|
||
|
||
const std::string input_path = temp_path("vde_p0_plugin_input.vbox");
|
||
ASSERT_TRUE(write_text_file(input_path, "vbox 1\n20 30 40\n"));
|
||
|
||
FormatImportResult imported;
|
||
ASSERT_TRUE(import_vbox_file(input_path, &imported))
|
||
<< imported.error << " / " << pm.last_error();
|
||
ASSERT_EQ(imported.bodies.size(), 1u);
|
||
EXPECT_EQ(imported.format_id, "vbox");
|
||
|
||
VdeBody body = imported.bodies[0];
|
||
ASSERT_NE(body, nullptr);
|
||
VdeBodyInfo info{};
|
||
info.struct_size = sizeof(VdeBodyInfo);
|
||
ASSERT_EQ(fixture.host.body_query(body, &info), VDE_PLUGIN_OK);
|
||
EXPECT_EQ(info.num_faces, 6u);
|
||
EXPECT_TRUE(near_rel(info.volume, 24000.0, 1e-9)) << info.volume;
|
||
EXPECT_NEAR(info.bbox_min[0], -10.0, 1e-9);
|
||
EXPECT_NEAR(info.bbox_max[1], 15.0, 1e-9);
|
||
EXPECT_NEAR(info.bbox_min[2], -20.0, 1e-9);
|
||
|
||
// 导出后再导入:体积/包围盒一致
|
||
const std::string export_path = temp_path("vde_p0_plugin_export.vbox");
|
||
ASSERT_TRUE(pm.export_file(export_path, imported.bodies, "vbox"))
|
||
<< pm.last_error();
|
||
const std::string exported = read_text_file(export_path);
|
||
ASSERT_FALSE(exported.empty());
|
||
EXPECT_NE(exported.find("vbox 1"), std::string::npos);
|
||
|
||
FormatImportResult reimported;
|
||
ASSERT_TRUE(import_vbox_file(export_path, &reimported))
|
||
<< reimported.error << " / " << pm.last_error();
|
||
ASSERT_EQ(reimported.bodies.size(), 1u);
|
||
VdeBodyInfo reimported_info{};
|
||
reimported_info.struct_size = sizeof(VdeBodyInfo);
|
||
ASSERT_EQ(fixture.host.body_query(reimported.bodies[0], &reimported_info),
|
||
VDE_PLUGIN_OK);
|
||
EXPECT_TRUE(near_rel(info.volume, reimported_info.volume, 1e-9));
|
||
for (int i = 0; i < 3; ++i) {
|
||
EXPECT_NEAR(info.bbox_min[i], reimported_info.bbox_min[i], 1e-9);
|
||
EXPECT_NEAR(info.bbox_max[i], reimported_info.bbox_max[i], 1e-9);
|
||
}
|
||
|
||
destroy_bodies(fixture.host, reimported.bodies);
|
||
destroy_bodies(fixture.host, imported.bodies);
|
||
std::remove(input_path.c_str());
|
||
std::remove(export_path.c_str());
|
||
pm.clear();
|
||
}
|
||
|
||
TEST(PluginIntegration, UnloadRemovesFormatsAndCapabilities) {
|
||
HostFixture fixture;
|
||
reset_manager(fixture.host);
|
||
PluginManager& pm = PluginManager::instance();
|
||
std::string error;
|
||
ASSERT_TRUE(load_sample_plugin(&error)) << error << " / " << pm.last_error();
|
||
ASSERT_FALSE(pm.formats().empty());
|
||
|
||
ASSERT_TRUE(pm.unload(kSamplePluginId, &error)) << error;
|
||
EXPECT_FALSE(pm.is_loaded(kSamplePluginId));
|
||
EXPECT_TRUE(pm.formats().empty());
|
||
EXPECT_FALSE(pm.capabilities().has("format:vbox.read"));
|
||
pm.clear();
|
||
}
|
||
|
||
TEST(PluginIntegration, ErrorPathsAreIsolated) {
|
||
HostFixture fixture;
|
||
reset_manager(fixture.host);
|
||
PluginManager& pm = PluginManager::instance();
|
||
std::string error;
|
||
ASSERT_TRUE(load_sample_plugin(&error)) << error << " / " << pm.last_error();
|
||
|
||
// 文件不存在
|
||
FormatImportResult missing;
|
||
EXPECT_FALSE(pm.import_file(temp_path("vde_p0_plugin_missing.vbox"), &missing));
|
||
EXPECT_FALSE(missing.error.empty());
|
||
EXPECT_FALSE(pm.last_error().empty());
|
||
|
||
// 内容损坏(magic 之后不是数字)
|
||
const std::string bad_path = temp_path("vde_p0_plugin_bad.vbox");
|
||
ASSERT_TRUE(write_text_file(bad_path, "vbox 1\nnot-a-number 10 20\n"));
|
||
vde::plugin::clear_geometry_host_last_error();
|
||
FormatImportResult bad;
|
||
EXPECT_FALSE(pm.import_file(bad_path, &bad));
|
||
EXPECT_FALSE(bad.error.empty());
|
||
EXPECT_FALSE(pm.last_error().empty());
|
||
// 插件通过 host->set_error 上报的错误可由宿主读取(同一 thread-local)
|
||
EXPECT_GT(std::strlen(vde::plugin::geometry_host_last_error()), 0u);
|
||
|
||
// 失败后管理器仍可用(失败隔离):正常文件继续可导入
|
||
const std::string good_path = temp_path("vde_p0_plugin_good.vbox");
|
||
ASSERT_TRUE(write_text_file(good_path, "vbox 1\n2 3 4\n"));
|
||
FormatImportResult good;
|
||
ASSERT_TRUE(pm.import_file(good_path, &good))
|
||
<< good.error << " / " << pm.last_error();
|
||
ASSERT_EQ(good.bodies.size(), 1u);
|
||
destroy_bodies(fixture.host, good.bodies);
|
||
std::remove(bad_path.c_str());
|
||
std::remove(good_path.c_str());
|
||
pm.clear();
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 3. 插件 C ABI 直测(不经 PluginManager):probe / read_memory / write_memory
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(PluginDirectAbi, ProbeReadWriteMemory) {
|
||
HostFixture fixture;
|
||
const VdePluginHost& host = fixture.host;
|
||
#ifdef _WIN32
|
||
const std::string library_path =
|
||
std::string(VDE_PLUGIN_SAMPLE_DIR) + "/sample_vbox/vde_sample_vbox.dll";
|
||
#else
|
||
const std::string library_path =
|
||
std::string(VDE_PLUGIN_SAMPLE_DIR) + "/sample_vbox/libvde_sample_vbox.so";
|
||
#endif
|
||
vde::platform::LibHandle library = vde::platform::load_library(library_path);
|
||
ASSERT_NE(library, nullptr) << vde::platform::last_error();
|
||
|
||
auto query = reinterpret_cast<VdePluginQueryFn>(
|
||
vde::platform::find_symbol(library, VDE_PLUGIN_SYMBOL_QUERY));
|
||
auto init = reinterpret_cast<VdePluginInitFn>(
|
||
vde::platform::find_symbol(library, VDE_PLUGIN_SYMBOL_INIT));
|
||
ASSERT_NE(query, nullptr);
|
||
ASSERT_NE(init, nullptr);
|
||
|
||
VdePluginInfo info{};
|
||
info.struct_size = sizeof(VdePluginInfo);
|
||
ASSERT_EQ(query(VDE_PLUGIN_ABI_MAJOR, &info), VDE_PLUGIN_OK);
|
||
EXPECT_STREQ(info.id, kSamplePluginId);
|
||
EXPECT_STREQ(info.version, "0.1.0");
|
||
EXPECT_EQ(info.kind, static_cast<uint32_t>(VDE_PLUGIN_KIND_FORMAT));
|
||
EXPECT_EQ(info.abi_major, static_cast<uint32_t>(VDE_PLUGIN_ABI_MAJOR));
|
||
|
||
VdePluginRegistration registration{};
|
||
registration.struct_size = sizeof(VdePluginRegistration);
|
||
ASSERT_EQ(init(&host, ®istration), VDE_PLUGIN_OK);
|
||
ASSERT_EQ(registration.kind, static_cast<uint32_t>(VDE_PLUGIN_KIND_FORMAT));
|
||
ASSERT_NE(registration.format, nullptr);
|
||
const VdeFormatPlugin& format = *registration.format;
|
||
ASSERT_NE(format.probe, nullptr);
|
||
ASSERT_NE(format.read_memory, nullptr);
|
||
ASSERT_NE(format.write_memory, nullptr);
|
||
EXPECT_STREQ(format.format_id, "vbox");
|
||
EXPECT_NE(std::string(format.extensions).find(".vbox"), std::string::npos);
|
||
|
||
// probe:识别 vbox magic;不认识的格式返回 0
|
||
const char* text = "vbox 1\n1 2 3\n";
|
||
EXPECT_EQ(format.probe(text, std::strlen(text)), 100);
|
||
const char* other = "STEP;ISO-10303-21;";
|
||
EXPECT_EQ(format.probe(other, std::strlen(other)), 0);
|
||
|
||
// read_memory:1 个盒体(1×2×3),体积 6
|
||
VdeBody* bodies = nullptr;
|
||
size_t count = 0;
|
||
ASSERT_EQ(format.read_memory(&host, text, std::strlen(text), &bodies, &count),
|
||
VDE_PLUGIN_OK)
|
||
<< vde::plugin::geometry_host_last_error();
|
||
ASSERT_EQ(count, 1u);
|
||
ASSERT_NE(bodies, nullptr);
|
||
VdeBodyInfo body_info{};
|
||
body_info.struct_size = sizeof(VdeBodyInfo);
|
||
ASSERT_EQ(host.body_query(bodies[0], &body_info), VDE_PLUGIN_OK);
|
||
EXPECT_EQ(body_info.num_faces, 6u);
|
||
EXPECT_TRUE(near_rel(body_info.volume, 6.0, 1e-9)) << body_info.volume;
|
||
for (size_t i = 0; i < count; ++i) host.body_destroy(bodies[i]);
|
||
host.free(bodies);
|
||
|
||
// write_memory:从宿主盒体句柄写回文本
|
||
VdeBody box = host.body_create_box(20.0, 30.0, 40.0);
|
||
ASSERT_NE(box, nullptr);
|
||
VdeBuffer buffer{nullptr, 0};
|
||
ASSERT_EQ(format.write_memory(&host, &box, 1, &buffer), VDE_PLUGIN_OK)
|
||
<< vde::plugin::geometry_host_last_error();
|
||
ASSERT_NE(buffer.data, nullptr);
|
||
const std::string written(static_cast<const char*>(buffer.data), buffer.size);
|
||
EXPECT_NE(written.find("vbox 1"), std::string::npos);
|
||
EXPECT_NE(written.find("20"), std::string::npos) << written;
|
||
EXPECT_NE(written.find("30"), std::string::npos) << written;
|
||
EXPECT_NE(written.find("40"), std::string::npos) << written;
|
||
host.free_buffer(buffer.data);
|
||
host.body_destroy(box);
|
||
|
||
// 坏内容:负错误码 + host 错误信息(不静默)
|
||
vde::plugin::clear_geometry_host_last_error();
|
||
VdeBody* bad_bodies = nullptr;
|
||
size_t bad_count = 0;
|
||
const char* malformed = "vbox 1\n1 2\n";
|
||
EXPECT_LT(format.read_memory(&host, malformed, std::strlen(malformed),
|
||
&bad_bodies, &bad_count),
|
||
0);
|
||
EXPECT_GT(std::strlen(vde::plugin::geometry_host_last_error()), 0u);
|
||
|
||
// 多实体:两行尺寸 → 两个句柄;写回同样两行
|
||
const char* two_bodies = "vbox 1\n1 2 3\n4 5 6\n";
|
||
VdeBody* multi = nullptr;
|
||
size_t multi_count = 0;
|
||
ASSERT_EQ(format.read_memory(&host, two_bodies, std::strlen(two_bodies), &multi,
|
||
&multi_count),
|
||
VDE_PLUGIN_OK);
|
||
ASSERT_EQ(multi_count, 2u);
|
||
VdeBuffer multi_buffer{nullptr, 0};
|
||
ASSERT_EQ(format.write_memory(&host, multi, multi_count, &multi_buffer),
|
||
VDE_PLUGIN_OK);
|
||
const std::string multi_text(static_cast<const char*>(multi_buffer.data),
|
||
multi_buffer.size);
|
||
EXPECT_NE(multi_text.find("vbox 1"), std::string::npos);
|
||
host.free_buffer(multi_buffer.data);
|
||
for (size_t i = 0; i < multi_count; ++i) host.body_destroy(multi[i]);
|
||
host.free(multi);
|
||
|
||
vde::platform::unload_library(library);
|
||
}
|
||
|
||
TEST(PluginHostServices, BinarySerializedBodyLoads) {
|
||
HostFixture fixture;
|
||
const VdePluginHost& host = fixture.host;
|
||
VdeBody box = host.body_create_box(5.0, 6.0, 7.0);
|
||
ASSERT_NE(box, nullptr);
|
||
|
||
VdeBuffer text_buffer{nullptr, 0};
|
||
ASSERT_EQ(host.body_serialize(box, &text_buffer), VDE_PLUGIN_OK);
|
||
const std::string text(static_cast<const char*>(text_buffer.data),
|
||
text_buffer.size);
|
||
host.free_buffer(text_buffer.data);
|
||
|
||
// VDE-TEXT → VDE 二进制 v3:body_from_serialized 应自动识别二进制路径
|
||
const std::vector<uint8_t> binary = vde::foundation::vde_text_to_binary(text);
|
||
ASSERT_FALSE(binary.empty());
|
||
vde::plugin::clear_geometry_host_last_error();
|
||
VdeBody restored = host.body_from_serialized(binary.data(), binary.size());
|
||
ASSERT_NE(restored, nullptr) << vde::plugin::geometry_host_last_error();
|
||
VdeBodyInfo info{};
|
||
info.struct_size = sizeof(VdeBodyInfo);
|
||
ASSERT_EQ(host.body_query(restored, &info), VDE_PLUGIN_OK);
|
||
EXPECT_EQ(info.num_faces, 6u);
|
||
EXPECT_TRUE(near_rel(info.volume, 210.0, 1e-9)) << info.volume;
|
||
host.body_destroy(restored);
|
||
host.body_destroy(box);
|
||
}
|