554 lines
20 KiB
C++
554 lines
20 KiB
C++
// tests/plugin/test_plugin_sdk.cpp
|
||
// P4-45:VDE 插件 C++ SDK(header-only,C++17)单元测试。
|
||
//
|
||
// 覆盖:
|
||
// 1) HostServices:宿主表校验(null/ABI major/core/旧 struct_size 字段可用性)、
|
||
// alloc/free/set_error、body_from_mesh/body_destroy、body_tessellate 网格 RAII;
|
||
// 2) BufferOwner:free_buffer/host->free 两种释放策略、move、release_buffer;
|
||
// 3) ErrorMessage:set/setf/format、宿主播空;
|
||
// 4) JsonWriter:对象/数组/嵌套、字符串转义、整数/浮点/bool/null、错误状态;
|
||
// 5) PluginInfo / Registration<T>:fill_plugin_info、格式/Mesher/未知扩展注册、
|
||
// query_plugin/initialize_plugin 辅助、ABI/struct_size 校验。
|
||
//
|
||
// 本测试只依赖 SDK 头文件 + gtest(不链接内核、不加载插件 DLL)。
|
||
#include <gtest/gtest.h>
|
||
|
||
#include <cmath>
|
||
#include <cstddef>
|
||
#include <cstdint>
|
||
#include <cstdlib>
|
||
#include <cstring>
|
||
#include <string>
|
||
#include <type_traits>
|
||
|
||
#include "vde/plugin/sdk/plugin.hpp"
|
||
|
||
static_assert(__cplusplus >= 201703L, "plugin SDK 需要 C++17");
|
||
static_assert(std::is_move_constructible_v<vde::plugin::sdk::BufferOwner>);
|
||
static_assert(!std::is_copy_constructible_v<vde::plugin::sdk::BufferOwner>);
|
||
static_assert(std::is_move_constructible_v<vde::plugin::sdk::HostServices::MeshData>);
|
||
static_assert(!std::is_copy_constructible_v<vde::plugin::sdk::HostServices::MeshData>);
|
||
|
||
namespace {
|
||
|
||
using vde::plugin::sdk::BufferOwner;
|
||
using vde::plugin::sdk::ErrorMessage;
|
||
using vde::plugin::sdk::FormatRegistration;
|
||
using vde::plugin::sdk::HostServices;
|
||
using vde::plugin::sdk::JsonWriter;
|
||
using vde::plugin::sdk::MesherRegistration;
|
||
using vde::plugin::sdk::PluginInfo;
|
||
using vde::plugin::sdk::Registration;
|
||
|
||
std::string g_last_error;
|
||
int g_alloc_count = 0;
|
||
int g_free_count = 0;
|
||
int g_free_buffer_count = 0;
|
||
int g_destroy_count = 0;
|
||
bool g_fail_tessellate = false;
|
||
|
||
VdeBody g_body_handle = reinterpret_cast<VdeBody>(static_cast<std::uintptr_t>(0x1234));
|
||
|
||
void reset_state() {
|
||
g_last_error.clear();
|
||
g_alloc_count = 0;
|
||
g_free_count = 0;
|
||
g_free_buffer_count = 0;
|
||
g_destroy_count = 0;
|
||
g_fail_tessellate = false;
|
||
}
|
||
|
||
void* fake_alloc(std::size_t size) {
|
||
++g_alloc_count;
|
||
return std::malloc(size == 0 ? 1 : size);
|
||
}
|
||
void* fake_realloc(void* pointer, std::size_t size) {
|
||
return std::realloc(pointer, size == 0 ? 1 : size);
|
||
}
|
||
void fake_free(void* pointer) {
|
||
++g_free_count;
|
||
std::free(pointer);
|
||
}
|
||
void fake_log(int, const char*) {}
|
||
void fake_set_error(const char* message) { g_last_error = message ? message : ""; }
|
||
|
||
VdeBody fake_body_from_mesh(const double*, std::size_t, const std::uint32_t*,
|
||
std::size_t) {
|
||
return g_body_handle;
|
||
}
|
||
void fake_body_destroy(VdeBody) { ++g_destroy_count; }
|
||
void fake_free_buffer(void* pointer) {
|
||
++g_free_buffer_count;
|
||
std::free(pointer);
|
||
}
|
||
|
||
int fake_body_query(VdeBody, VdeBodyInfo* out) {
|
||
if (out) out->is_valid = 1;
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
int fake_body_volume(VdeBody, double* value) {
|
||
if (value) *value = 1.0;
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
int fake_body_area(VdeBody, double* value) {
|
||
if (value) *value = 1.0;
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
int fake_body_bbox(VdeBody, double out_min[3], double out_max[3]) {
|
||
for (int i = 0; i < 3; ++i) {
|
||
out_min[i] = 0.0;
|
||
out_max[i] = 1.0;
|
||
}
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
int fake_body_tessellate(VdeBody, double, VdeMeshData* out) {
|
||
if (!out) return VDE_PLUGIN_ERR_INVALID_ARG;
|
||
out->positions = nullptr;
|
||
out->indices = nullptr;
|
||
out->normals = nullptr;
|
||
out->num_vertices = 0;
|
||
out->num_triangles = 0;
|
||
if (g_fail_tessellate) return VDE_PLUGIN_ERR_INTERNAL;
|
||
out->positions = static_cast<double*>(std::malloc(sizeof(double) * 9));
|
||
out->indices = static_cast<std::uint32_t*>(std::malloc(sizeof(std::uint32_t) * 3));
|
||
if (!out->positions || !out->indices) {
|
||
std::free(out->positions);
|
||
std::free(out->indices);
|
||
out->positions = nullptr;
|
||
out->indices = nullptr;
|
||
return VDE_PLUGIN_ERR_BUFFER;
|
||
}
|
||
const double positions[9] = {0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0};
|
||
std::memcpy(out->positions, positions, sizeof(positions));
|
||
out->indices[0] = 0;
|
||
out->indices[1] = 1;
|
||
out->indices[2] = 2;
|
||
out->num_vertices = 3;
|
||
out->num_triangles = 1;
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
|
||
VdePluginHost make_host() {
|
||
VdePluginHost host{};
|
||
host.struct_size = static_cast<std::uint32_t>(sizeof(VdePluginHost));
|
||
host.abi_major = VDE_PLUGIN_ABI_MAJOR;
|
||
host.abi_minor = VDE_PLUGIN_ABI_MINOR;
|
||
host.kernel_version = "mock";
|
||
host.alloc = &fake_alloc;
|
||
host.realloc = &fake_realloc;
|
||
host.free = &fake_free;
|
||
host.log = &fake_log;
|
||
host.set_error = &fake_set_error;
|
||
host.body_query = &fake_body_query;
|
||
host.body_volume = &fake_body_volume;
|
||
host.body_area = &fake_body_area;
|
||
host.body_bbox = &fake_body_bbox;
|
||
host.body_tessellate = &fake_body_tessellate;
|
||
host.body_destroy = &fake_body_destroy;
|
||
host.free_buffer = &fake_free_buffer;
|
||
host.body_from_mesh = &fake_body_from_mesh;
|
||
return host;
|
||
}
|
||
|
||
class PluginSdkTest : public ::testing::Test {
|
||
protected:
|
||
void SetUp() override { reset_state(); }
|
||
void TearDown() override {
|
||
reset_state();
|
||
}
|
||
};
|
||
|
||
} // namespace
|
||
|
||
// ── 1. HostServices ────────────────────────────────────────────────
|
||
TEST_F(PluginSdkTest, HostServicesValidation) {
|
||
HostServices empty;
|
||
EXPECT_FALSE(empty.valid());
|
||
EXPECT_FALSE(empty.abi_major_ok());
|
||
EXPECT_FALSE(empty.has_core());
|
||
EXPECT_FALSE(static_cast<bool>(empty));
|
||
|
||
EXPECT_FALSE(HostServices(nullptr).valid());
|
||
|
||
VdePluginHost host = make_host();
|
||
const HostServices services(&host);
|
||
EXPECT_TRUE(services.valid());
|
||
EXPECT_TRUE(services.abi_major_ok());
|
||
EXPECT_EQ(services.abi_minor(), VDE_PLUGIN_ABI_MINOR);
|
||
EXPECT_TRUE(services.has_core());
|
||
EXPECT_TRUE(services.has_body_from_mesh());
|
||
EXPECT_TRUE(services.has_body_tessellate());
|
||
EXPECT_TRUE(services.has_measure());
|
||
EXPECT_EQ(services.raw(), &host);
|
||
|
||
// 错误 ABI major
|
||
VdePluginHost bad_abi = make_host();
|
||
bad_abi.abi_major = VDE_PLUGIN_ABI_MAJOR + 1u;
|
||
EXPECT_FALSE(HostServices(&bad_abi).abi_major_ok());
|
||
EXPECT_FALSE(HostServices(&bad_abi).valid());
|
||
|
||
// 旧宿主:struct_size 不覆盖 body_from_mesh(ABI 1.0 风格)
|
||
VdePluginHost old_host = make_host();
|
||
old_host.struct_size = static_cast<std::uint32_t>(
|
||
offsetof(VdePluginHost, body_from_mesh));
|
||
const HostServices old_services(&old_host);
|
||
EXPECT_TRUE(old_services.valid());
|
||
EXPECT_TRUE(old_services.has_core());
|
||
EXPECT_FALSE(old_services.has_body_from_mesh());
|
||
EXPECT_TRUE(old_services.has_body_tessellate());
|
||
|
||
// 更旧/截断:body_tessellate 及其后的 body_serialize/body_destroy/free_buffer 不可见
|
||
VdePluginHost older_host = make_host();
|
||
older_host.struct_size = static_cast<std::uint32_t>(
|
||
offsetof(VdePluginHost, body_tessellate));
|
||
const HostServices older_services(&older_host);
|
||
EXPECT_FALSE(older_services.has_core()); // free_buffer 在 body_tessellate 之后
|
||
EXPECT_FALSE(older_services.has_body_tessellate());
|
||
EXPECT_FALSE(older_services.has_body_from_mesh());
|
||
EXPECT_FALSE(older_services.valid());
|
||
}
|
||
|
||
TEST_F(PluginSdkTest, HostServicesMemoryErrorAndBodies) {
|
||
VdePluginHost host = make_host();
|
||
const HostServices services(&host);
|
||
|
||
void* memory = services.alloc(16);
|
||
ASSERT_NE(memory, nullptr);
|
||
EXPECT_EQ(g_alloc_count, 1);
|
||
services.free(memory);
|
||
EXPECT_EQ(g_free_count, 1);
|
||
services.free(nullptr); // no-op
|
||
EXPECT_EQ(g_free_count, 1);
|
||
|
||
EXPECT_TRUE(services.set_error("boom"));
|
||
EXPECT_EQ(g_last_error, "boom");
|
||
EXPECT_TRUE(services.set_error(std::string("worse")));
|
||
EXPECT_EQ(g_last_error, "worse");
|
||
|
||
const VdeBody body = services.body_from_mesh(nullptr, 3, nullptr, 1);
|
||
EXPECT_EQ(body, g_body_handle);
|
||
EXPECT_TRUE(services.body_destroy(body));
|
||
EXPECT_EQ(g_destroy_count, 1);
|
||
EXPECT_FALSE(services.body_destroy(nullptr));
|
||
|
||
{
|
||
HostServices::MeshData mesh;
|
||
EXPECT_TRUE(services.tessellate(body, 0.01, &mesh));
|
||
EXPECT_TRUE(mesh);
|
||
EXPECT_FALSE(mesh.empty());
|
||
EXPECT_EQ(mesh.num_vertices(), 3u);
|
||
EXPECT_EQ(mesh.num_triangles(), 1u);
|
||
ASSERT_NE(mesh.positions(), nullptr);
|
||
ASSERT_NE(mesh.indices(), nullptr);
|
||
EXPECT_DOUBLE_EQ(mesh.positions()[3], 1.0);
|
||
EXPECT_EQ(mesh.indices()[2], 2u);
|
||
}
|
||
EXPECT_EQ(g_free_buffer_count, 2); // positions + indices(normals = null)
|
||
|
||
g_fail_tessellate = true;
|
||
HostServices::MeshData failed;
|
||
EXPECT_FALSE(services.tessellate(body, 0.01, &failed));
|
||
EXPECT_TRUE(failed.empty());
|
||
EXPECT_EQ(g_free_buffer_count, 2);
|
||
}
|
||
|
||
TEST_F(PluginSdkTest, MeshDataMoveAndDetach) {
|
||
VdePluginHost host = make_host();
|
||
const HostServices services(&host);
|
||
HostServices::MeshData source;
|
||
ASSERT_TRUE(services.tessellate(g_body_handle, 0.01, &source));
|
||
HostServices::MeshData moved(std::move(source));
|
||
EXPECT_TRUE(moved);
|
||
EXPECT_TRUE(source.empty());
|
||
{
|
||
HostServices::MeshData second = std::move(moved);
|
||
EXPECT_TRUE(second);
|
||
}
|
||
EXPECT_EQ(g_free_buffer_count, 2);
|
||
EXPECT_TRUE(moved.empty());
|
||
|
||
HostServices::MeshData detach_source;
|
||
ASSERT_TRUE(services.tessellate(g_body_handle, 0.01, &detach_source));
|
||
VdeMeshData raw = detach_source.detach();
|
||
EXPECT_TRUE(detach_source.empty());
|
||
ASSERT_NE(raw.positions, nullptr);
|
||
ASSERT_NE(raw.indices, nullptr);
|
||
host.free_buffer(raw.positions);
|
||
host.free_buffer(raw.indices);
|
||
EXPECT_EQ(g_free_buffer_count, 4);
|
||
}
|
||
|
||
// ── 2. BufferOwner ─────────────────────────────────────────────────
|
||
TEST_F(PluginSdkTest, BufferOwnerFreeBufferPolicy) {
|
||
VdePluginHost host = make_host();
|
||
{
|
||
VdeBuffer buffer{};
|
||
buffer.data = std::malloc(8);
|
||
buffer.size = 8;
|
||
BufferOwner owner(&host, buffer);
|
||
EXPECT_TRUE(owner);
|
||
EXPECT_TRUE(owner.owns());
|
||
EXPECT_EQ(owner.size(), 8u);
|
||
EXPECT_FALSE(owner.empty());
|
||
EXPECT_NE(owner.data(), nullptr);
|
||
}
|
||
EXPECT_EQ(g_free_buffer_count, 1);
|
||
|
||
// release_buffer:交出所有权,不再自动释放
|
||
VdeBuffer buffer{};
|
||
buffer.data = std::malloc(4);
|
||
buffer.size = 4;
|
||
BufferOwner owner(&host, buffer);
|
||
const VdeBuffer released = owner.release_buffer();
|
||
EXPECT_FALSE(owner.owns());
|
||
ASSERT_NE(released.data, nullptr);
|
||
host.free_buffer(released.data);
|
||
EXPECT_EQ(g_free_buffer_count, 2);
|
||
}
|
||
|
||
TEST_F(PluginSdkTest, BufferOwnerHostFreePolicyAndMove) {
|
||
VdePluginHost host = make_host();
|
||
{
|
||
VdeBuffer buffer{};
|
||
buffer.data = host.alloc(16); // 计数+1
|
||
buffer.size = 16;
|
||
BufferOwner owner(&host, buffer, BufferOwner::Deallocator::HostFree);
|
||
EXPECT_TRUE(owner);
|
||
}
|
||
EXPECT_EQ(g_alloc_count, 1);
|
||
EXPECT_EQ(g_free_count, 1);
|
||
EXPECT_EQ(g_free_buffer_count, 0);
|
||
|
||
// move:只释放一次
|
||
{
|
||
VdeBuffer buffer{};
|
||
buffer.data = std::malloc(8);
|
||
buffer.size = 8;
|
||
BufferOwner first(&host, buffer);
|
||
BufferOwner second(std::move(first));
|
||
EXPECT_FALSE(first.owns());
|
||
EXPECT_TRUE(second.owns());
|
||
}
|
||
EXPECT_EQ(g_free_buffer_count, 1);
|
||
|
||
BufferOwner empty_owner;
|
||
EXPECT_FALSE(empty_owner);
|
||
EXPECT_FALSE(empty_owner.owns());
|
||
}
|
||
|
||
// ── 3. ErrorMessage ────────────────────────────────────────────────
|
||
TEST_F(PluginSdkTest, ErrorMessageFormatting) {
|
||
VdePluginHost host = make_host();
|
||
const ErrorMessage error(&host);
|
||
EXPECT_TRUE(error.available());
|
||
EXPECT_TRUE(error.set("plain"));
|
||
EXPECT_EQ(g_last_error, "plain");
|
||
EXPECT_TRUE(error.set(std::string("string")));
|
||
EXPECT_EQ(g_last_error, "string");
|
||
EXPECT_TRUE(error.setf("code=%d name=%s", 7, "vde"));
|
||
EXPECT_EQ(g_last_error, "code=7 name=vde");
|
||
EXPECT_EQ(ErrorMessage::format("%s:%u", "x", 3u), "x:3");
|
||
EXPECT_EQ(ErrorMessage::format(nullptr), "");
|
||
|
||
const ErrorMessage no_host(nullptr);
|
||
EXPECT_FALSE(no_host.available());
|
||
EXPECT_FALSE(no_host.set("x"));
|
||
EXPECT_FALSE(no_host.setf("%d", 1));
|
||
EXPECT_EQ(g_last_error, "code=7 name=vde");
|
||
}
|
||
|
||
// ── 4. JsonWriter ──────────────────────────────────────────────────
|
||
TEST_F(PluginSdkTest, JsonWriterObjectsArraysAndScalars) {
|
||
JsonWriter writer;
|
||
writer.begin_object();
|
||
writer.key("id").value("com.vde.test");
|
||
writer.key("version").value(3);
|
||
writer.key("ok").value(true);
|
||
writer.key("ratio").value(2.5);
|
||
writer.key("nothing").null();
|
||
writer.key("list").begin_array().value(1).value("two").value(false).end_array();
|
||
writer.key("nested").begin_object().key("x").value(0).end_object();
|
||
writer.end_object();
|
||
ASSERT_TRUE(writer.ok()) << writer.error();
|
||
EXPECT_EQ(writer.str(),
|
||
"{\"id\":\"com.vde.test\",\"version\":3,\"ok\":true,\"ratio\":2.5,"
|
||
"\"nothing\":null,\"list\":[1,\"two\",false],\"nested\":{\"x\":0}}");
|
||
const std::string taken = writer.take();
|
||
EXPECT_EQ(taken,
|
||
"{\"id\":\"com.vde.test\",\"version\":3,\"ok\":true,\"ratio\":2.5,"
|
||
"\"nothing\":null,\"list\":[1,\"two\",false],\"nested\":{\"x\":0}}");
|
||
EXPECT_TRUE(writer.str().empty());
|
||
|
||
JsonWriter arrays;
|
||
arrays.begin_array().begin_array().end_array().begin_object().end_object().end_array();
|
||
ASSERT_TRUE(arrays.ok());
|
||
EXPECT_EQ(arrays.str(), "[[],{}]");
|
||
|
||
JsonWriter scalar;
|
||
scalar.value(true);
|
||
ASSERT_TRUE(scalar.ok());
|
||
EXPECT_EQ(scalar.str(), "true");
|
||
|
||
JsonWriter negative;
|
||
negative.value(-42);
|
||
EXPECT_EQ(negative.str(), "-42");
|
||
}
|
||
|
||
TEST_F(PluginSdkTest, JsonWriterEscaping) {
|
||
JsonWriter writer;
|
||
writer.begin_object();
|
||
writer.key("s").value("a\"b\\c\n\t\r\b\f");
|
||
writer.key("ctrl").value(std::string("x\x01y", 3));
|
||
writer.key("utf8").value("\xE4\xB8\xAD"); // 中
|
||
writer.end_object();
|
||
ASSERT_TRUE(writer.ok()) << writer.error();
|
||
EXPECT_EQ(writer.str(),
|
||
"{\"s\":\"a\\\"b\\\\c\\n\\t\\r\\b\\f\",\"ctrl\":\"x\\u0001y\","
|
||
"\"utf8\":\"\xE4\xB8\xAD\"}");
|
||
}
|
||
|
||
TEST_F(PluginSdkTest, JsonWriterErrorStates) {
|
||
JsonWriter writer;
|
||
writer.value(1);
|
||
EXPECT_TRUE(writer.ok());
|
||
writer.value(2); // 多个根值
|
||
EXPECT_FALSE(writer.ok());
|
||
EXPECT_NE(writer.error().find("multiple root"), std::string::npos);
|
||
|
||
JsonWriter no_key;
|
||
no_key.begin_object();
|
||
no_key.value(1);
|
||
EXPECT_FALSE(no_key.ok());
|
||
|
||
JsonWriter key_outside;
|
||
key_outside.key("x");
|
||
EXPECT_FALSE(key_outside.ok());
|
||
|
||
JsonWriter mismatch;
|
||
mismatch.begin_object();
|
||
mismatch.end_array();
|
||
EXPECT_FALSE(mismatch.ok());
|
||
|
||
JsonWriter pending;
|
||
pending.begin_object();
|
||
pending.key("x");
|
||
pending.end_object();
|
||
EXPECT_FALSE(pending.ok());
|
||
|
||
JsonWriter nonfinite;
|
||
nonfinite.begin_object();
|
||
nonfinite.key("v").value(std::nan(""));
|
||
nonfinite.end_object();
|
||
EXPECT_FALSE(nonfinite.ok());
|
||
EXPECT_NE(nonfinite.str().find("null"), std::string::npos);
|
||
|
||
writer.clear();
|
||
EXPECT_TRUE(writer.ok());
|
||
EXPECT_TRUE(writer.str().empty());
|
||
}
|
||
|
||
// ── 5. PluginInfo / Registration ───────────────────────────────────
|
||
TEST_F(PluginSdkTest, FillPluginInfoAndValidation) {
|
||
const PluginInfo info{"com.vde.test.format", "Test Format", "1.2.3",
|
||
"format:test.read;format:test.write", "Apache-2.0",
|
||
VDE_PLUGIN_KIND_FORMAT};
|
||
VdePluginInfo out{};
|
||
out.struct_size = static_cast<std::uint32_t>(sizeof(VdePluginInfo));
|
||
ASSERT_TRUE(vde::plugin::sdk::fill_plugin_info(info, VDE_PLUGIN_ABI_MAJOR, &out));
|
||
EXPECT_STREQ(out.id, "com.vde.test.format");
|
||
EXPECT_STREQ(out.name, "Test Format");
|
||
EXPECT_STREQ(out.version, "1.2.3");
|
||
EXPECT_STREQ(out.capabilities, "format:test.read;format:test.write");
|
||
EXPECT_STREQ(out.license, "Apache-2.0");
|
||
EXPECT_EQ(out.kind, static_cast<std::uint32_t>(VDE_PLUGIN_KIND_FORMAT));
|
||
EXPECT_EQ(out.abi_major, VDE_PLUGIN_ABI_MAJOR);
|
||
EXPECT_EQ(out.abi_minor, VDE_PLUGIN_ABI_MINOR);
|
||
|
||
VdePluginInfo too_small{};
|
||
too_small.struct_size = 1;
|
||
EXPECT_FALSE(vde::plugin::sdk::fill_plugin_info(info, VDE_PLUGIN_ABI_MAJOR, &too_small));
|
||
VdePluginInfo wrong_abi{};
|
||
wrong_abi.struct_size = sizeof(VdePluginInfo);
|
||
EXPECT_FALSE(vde::plugin::sdk::fill_plugin_info(info, VDE_PLUGIN_ABI_MAJOR + 1u,
|
||
&wrong_abi));
|
||
PluginInfo bad = info;
|
||
bad.id.clear();
|
||
VdePluginInfo out2{};
|
||
out2.struct_size = sizeof(VdePluginInfo);
|
||
EXPECT_FALSE(vde::plugin::sdk::fill_plugin_info(bad, VDE_PLUGIN_ABI_MAJOR, &out2));
|
||
}
|
||
|
||
TEST_F(PluginSdkTest, FormatRegistrationAndHelpers) {
|
||
VdePluginHost host = make_host();
|
||
const VdeFormatPlugin format{};
|
||
PluginInfo info{"com.vde.test.dxf", "Test DXF", "1.0.0",
|
||
"format:dxf.read;format:dxf.write", "Apache-2.0",
|
||
VDE_PLUGIN_KIND_FORMAT};
|
||
const FormatRegistration registration(info, &format);
|
||
|
||
VdePluginInfo plugin_info{};
|
||
plugin_info.struct_size = sizeof(VdePluginInfo);
|
||
EXPECT_EQ(vde::plugin::sdk::query_plugin(registration, VDE_PLUGIN_ABI_MAJOR,
|
||
&plugin_info),
|
||
VDE_PLUGIN_OK);
|
||
EXPECT_STREQ(plugin_info.id, "com.vde.test.dxf");
|
||
EXPECT_EQ(vde::plugin::sdk::query_plugin(registration, VDE_PLUGIN_ABI_MAJOR + 1u,
|
||
&plugin_info),
|
||
VDE_PLUGIN_ERR_ABI);
|
||
|
||
VdePluginRegistration reg{};
|
||
reg.struct_size = sizeof(VdePluginRegistration);
|
||
EXPECT_TRUE(registration.initialize(&host, ®));
|
||
EXPECT_EQ(reg.kind, static_cast<std::uint32_t>(VDE_PLUGIN_KIND_FORMAT));
|
||
EXPECT_EQ(reg.format, &format);
|
||
EXPECT_EQ(reg.cae_solver, nullptr);
|
||
EXPECT_EQ(reg.mesher, nullptr);
|
||
|
||
VdePluginRegistration null_host_reg{};
|
||
null_host_reg.struct_size = sizeof(VdePluginRegistration);
|
||
EXPECT_FALSE(registration.initialize(nullptr, &null_host_reg));
|
||
EXPECT_EQ(vde::plugin::sdk::initialize_plugin(registration, nullptr, &null_host_reg),
|
||
VDE_PLUGIN_ERR_INVALID_ARG);
|
||
|
||
VdePluginRegistration small{};
|
||
small.struct_size = 1;
|
||
EXPECT_EQ(vde::plugin::sdk::initialize_plugin(registration, &host, &small),
|
||
VDE_PLUGIN_ERR_ABI);
|
||
|
||
// 旧宿主 struct_size 只到 kind 时不得越界写扩展点字段
|
||
VdePluginRegistration old_reg{};
|
||
old_reg.struct_size = static_cast<std::uint32_t>(
|
||
offsetof(VdePluginRegistration, format));
|
||
EXPECT_TRUE(registration.initialize(&host, &old_reg));
|
||
EXPECT_EQ(old_reg.format, nullptr);
|
||
}
|
||
|
||
TEST_F(PluginSdkTest, MesherRegistrationAndUnknownExtension) {
|
||
VdePluginHost host = make_host();
|
||
const VdeMesherPlugin mesher{};
|
||
const MesherRegistration registration(
|
||
PluginInfo{"com.vde.test.mesher", "Test Mesher", "1.0.0", "mesher:test",
|
||
"Apache-2.0", VDE_PLUGIN_KIND_MESHER},
|
||
&mesher);
|
||
VdePluginRegistration reg{};
|
||
reg.struct_size = sizeof(VdePluginRegistration);
|
||
ASSERT_TRUE(registration.initialize(&host, ®));
|
||
EXPECT_EQ(reg.kind, static_cast<std::uint32_t>(VDE_PLUGIN_KIND_MESHER));
|
||
EXPECT_EQ(reg.mesher, &mesher);
|
||
EXPECT_EQ(reg.format, nullptr);
|
||
|
||
struct CustomExtension {
|
||
int value = 1;
|
||
};
|
||
const CustomExtension custom{};
|
||
const Registration<CustomExtension> generic_registration(
|
||
PluginInfo{"com.vde.test.custom", "Test Custom", "1.0.0", "", "Apache-2.0",
|
||
VDE_PLUGIN_KIND_GEOM_OP},
|
||
&custom);
|
||
VdePluginRegistration generic{};
|
||
generic.struct_size = sizeof(VdePluginRegistration);
|
||
ASSERT_TRUE(generic_registration.initialize(&host, &generic));
|
||
EXPECT_EQ(generic.kind, static_cast<std::uint32_t>(VDE_PLUGIN_KIND_GEOM_OP));
|
||
EXPECT_EQ(generic.generic, &custom);
|
||
}
|