858 lines
36 KiB
C++
858 lines
36 KiB
C++
// WP-P4-22 官方 AMF(Additive Manufacturing File)格式插件测试
|
||
// (确定性 mock 宿主 + 真实 C ABI 动态加载)。
|
||
//
|
||
// 覆盖:
|
||
// 1) vde_plugin_query / init / describe / manifest 的 id、能力、扩展名与 ABI;
|
||
// 2) XML AMF 读取:单 object、多 object、同一 object 多 volume、单位换算、
|
||
// metadata/material/color/texture/constellation 忽略、0-based 三角形索引;
|
||
// 3) 错误路径:空/空白、非 AMF 根、标签截断/不匹配、属性/实体非法、object id、
|
||
// vertices/volume/coordinates/v1-v3 缺失、坐标/索引非法、越界、退化三角形、超限;
|
||
// 4) 写出:mock 宿主 body_tessellate -> 每体一个 <object>,vertices/coordinates、
|
||
// volume/triangles 计数正确,write_file 与 write_memory 字节一致,写后重读;
|
||
// 5) 宿主能力缺失、空实体、三角化失败、非法路径等明确错误。
|
||
//
|
||
// 本测试只依赖纯 C ABI(vde_plugin_api.h)+ gtest,不链接内核。
|
||
#include <gtest/gtest.h>
|
||
|
||
#include <cmath>
|
||
#include <cstdint>
|
||
#include <cstdlib>
|
||
#include <cstring>
|
||
#include <fstream>
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
#if defined(_WIN32)
|
||
#ifndef NOMINMAX
|
||
#define NOMINMAX
|
||
#endif
|
||
#include <windows.h>
|
||
#else
|
||
#include <dlfcn.h>
|
||
#endif
|
||
|
||
#include "vde/plugin/vde_plugin_api.h"
|
||
|
||
#ifndef VDE_PLUGIN_OFFICIAL_DIR
|
||
#define VDE_PLUGIN_OFFICIAL_DIR ""
|
||
#endif
|
||
#ifndef VDE_TEST_TEMP_DIR
|
||
#define VDE_TEST_TEMP_DIR ""
|
||
#endif
|
||
|
||
namespace {
|
||
|
||
constexpr const char* kPluginId = "com.vde.official.amf";
|
||
|
||
#if defined(_WIN32)
|
||
constexpr const char* kPluginRelativePath = "/amf/vde_official_amf.dll";
|
||
#elif defined(__APPLE__)
|
||
constexpr const char* kPluginRelativePath = "/amf/libvde_official_amf.dylib";
|
||
#else
|
||
constexpr const char* kPluginRelativePath = "/amf/libvde_official_amf.so";
|
||
#endif
|
||
|
||
// ── 动态加载(与宿主相同的纯 C ABI 入口)──────────────────
|
||
#if defined(_WIN32)
|
||
using LibraryHandle = HMODULE;
|
||
using RawSymbol = void (*)();
|
||
LibraryHandle open_library(const char* path) { return ::LoadLibraryA(path); }
|
||
RawSymbol find_symbol(LibraryHandle library, const char* name) {
|
||
return reinterpret_cast<RawSymbol>(::GetProcAddress(library, name));
|
||
}
|
||
void close_library(LibraryHandle library) {
|
||
if (library) ::FreeLibrary(library);
|
||
}
|
||
#else
|
||
using LibraryHandle = void*;
|
||
using RawSymbol = void (*)();
|
||
LibraryHandle open_library(const char* path) {
|
||
return ::dlopen(path, RTLD_NOW | RTLD_LOCAL);
|
||
}
|
||
RawSymbol find_symbol(LibraryHandle library, const char* name) {
|
||
return reinterpret_cast<RawSymbol>(::dlsym(library, name));
|
||
}
|
||
void close_library(LibraryHandle library) {
|
||
if (library) ::dlclose(library);
|
||
}
|
||
#endif
|
||
|
||
struct PluginApi {
|
||
LibraryHandle library = nullptr;
|
||
VdePluginQueryFn query = nullptr;
|
||
VdePluginInitFn init = nullptr;
|
||
VdePluginShutdownFn shutdown = nullptr;
|
||
const VdeFormatPlugin* format = nullptr;
|
||
|
||
~PluginApi() { close(); }
|
||
|
||
bool open(std::string* error) {
|
||
const std::string path = std::string(VDE_PLUGIN_OFFICIAL_DIR) + kPluginRelativePath;
|
||
library = open_library(path.c_str());
|
||
if (!library) {
|
||
if (error) *error = "无法加载插件动态库: " + path;
|
||
return false;
|
||
}
|
||
query = reinterpret_cast<VdePluginQueryFn>(find_symbol(library, "vde_plugin_query"));
|
||
init = reinterpret_cast<VdePluginInitFn>(find_symbol(library, "vde_plugin_init"));
|
||
shutdown = reinterpret_cast<VdePluginShutdownFn>(
|
||
find_symbol(library, "vde_plugin_shutdown"));
|
||
if (!query || !init || !shutdown) {
|
||
if (error) *error = "插件缺少 vde_plugin_query/init/shutdown 导出: " + path;
|
||
close();
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
void close() {
|
||
if (library) {
|
||
close_library(library);
|
||
library = nullptr;
|
||
}
|
||
query = nullptr;
|
||
init = nullptr;
|
||
shutdown = nullptr;
|
||
format = nullptr;
|
||
}
|
||
};
|
||
|
||
// ── 确定性 mock 宿主 ─────────────────────────────────────
|
||
std::string g_last_error;
|
||
int g_live_bodies = 0;
|
||
|
||
struct MockBody {
|
||
std::vector<double> positions; // 3×N
|
||
std::vector<std::uint32_t> indices; // 3×T
|
||
bool fail_tessellate = false;
|
||
bool empty_tessellate = false;
|
||
};
|
||
|
||
MockBody* as_mock(VdeBody body) { return reinterpret_cast<MockBody*>(body); }
|
||
const MockBody* as_mock_const(VdeBody body) {
|
||
return reinterpret_cast<const MockBody*>(body);
|
||
}
|
||
|
||
void mock_log(int, const char*) {}
|
||
void mock_set_error(const char* message) { g_last_error = message ? message : ""; }
|
||
void* mock_alloc(std::size_t size) { return std::malloc(size == 0 ? 1 : size); }
|
||
void* mock_realloc(void* pointer, std::size_t size) {
|
||
return std::realloc(pointer, size == 0 ? 1 : size);
|
||
}
|
||
void mock_free(void* pointer) { std::free(pointer); }
|
||
void mock_free_buffer(void* pointer) { std::free(pointer); }
|
||
|
||
VdeBody mock_body_from_mesh(const double* positions, std::size_t num_vertices,
|
||
const std::uint32_t* indices, std::size_t num_triangles) {
|
||
if (!positions || num_vertices == 0) {
|
||
g_last_error = "mock body_from_mesh: 空网格";
|
||
return nullptr;
|
||
}
|
||
if (num_triangles > 0 && !indices) {
|
||
g_last_error = "mock body_from_mesh: 缺少索引";
|
||
return nullptr;
|
||
}
|
||
for (std::size_t i = 0; i < num_triangles * 3u; ++i) {
|
||
if (static_cast<std::size_t>(indices[i]) >= num_vertices) {
|
||
g_last_error = "mock body_from_mesh: 索引越界";
|
||
return nullptr;
|
||
}
|
||
}
|
||
auto* body = new MockBody();
|
||
body->positions.assign(positions, positions + num_vertices * 3u);
|
||
if (num_triangles > 0)
|
||
body->indices.assign(indices, indices + num_triangles * 3u);
|
||
++g_live_bodies;
|
||
return reinterpret_cast<VdeBody>(body);
|
||
}
|
||
|
||
void mock_body_destroy(VdeBody handle) {
|
||
auto* body = as_mock(handle);
|
||
if (!body) return;
|
||
delete body;
|
||
--g_live_bodies;
|
||
}
|
||
|
||
int mock_body_tessellate(VdeBody handle, double, VdeMeshData* out) {
|
||
if (!out || !handle) {
|
||
g_last_error = "mock body_tessellate: 参数为空";
|
||
return VDE_PLUGIN_ERR_INVALID_ARG;
|
||
}
|
||
MockBody* body = as_mock(handle);
|
||
out->positions = nullptr;
|
||
out->num_vertices = 0;
|
||
out->indices = nullptr;
|
||
out->num_triangles = 0;
|
||
out->normals = nullptr;
|
||
if (body->fail_tessellate) {
|
||
g_last_error = "mock body_tessellate: 注入失败";
|
||
return VDE_PLUGIN_ERR_INTERNAL;
|
||
}
|
||
if (body->empty_tessellate || body->indices.empty()) return VDE_PLUGIN_OK;
|
||
const std::size_t triangles = body->indices.size() / 3u;
|
||
const std::size_t vertices = triangles * 3u;
|
||
auto* positions = static_cast<double*>(std::malloc(vertices * 3u * sizeof(double)));
|
||
auto* indices = static_cast<std::uint32_t*>(std::malloc(vertices * sizeof(std::uint32_t)));
|
||
if (!positions || !indices) {
|
||
std::free(positions);
|
||
std::free(indices);
|
||
g_last_error = "mock body_tessellate: 内存不足";
|
||
return VDE_PLUGIN_ERR_BUFFER;
|
||
}
|
||
for (std::size_t t = 0; t < triangles; ++t) {
|
||
for (std::size_t k = 0; k < 3; ++k) {
|
||
const std::uint32_t source = body->indices[3u * t + k];
|
||
positions[(3u * t + k) * 3u + 0u] = body->positions[3u * source + 0u];
|
||
positions[(3u * t + k) * 3u + 1u] = body->positions[3u * source + 1u];
|
||
positions[(3u * t + k) * 3u + 2u] = body->positions[3u * source + 2u];
|
||
indices[3u * t + k] = static_cast<std::uint32_t>(3u * t + k);
|
||
}
|
||
}
|
||
out->positions = positions;
|
||
out->num_vertices = vertices;
|
||
out->indices = indices;
|
||
out->num_triangles = triangles;
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
|
||
VdePluginHost make_mock_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 = &mock_alloc;
|
||
host.realloc = &mock_realloc;
|
||
host.free = &mock_free;
|
||
host.log = &mock_log;
|
||
host.set_error = &mock_set_error;
|
||
host.body_destroy = &mock_body_destroy;
|
||
host.free_buffer = &mock_free_buffer;
|
||
host.body_from_mesh = &mock_body_from_mesh;
|
||
host.body_tessellate = &mock_body_tessellate;
|
||
return host;
|
||
}
|
||
|
||
// ── 几何/文件辅助 ────────────────────────────────────────
|
||
struct Cube {
|
||
std::vector<double> positions;
|
||
std::vector<std::uint32_t> indices;
|
||
};
|
||
|
||
Cube make_cube(double width, double height, double depth) {
|
||
const double x = width * 0.5;
|
||
const double y = height * 0.5;
|
||
const double z = depth * 0.5;
|
||
Cube cube;
|
||
cube.positions = {-x, -y, -z, x, -y, -z, x, y, -z, -x, y, -z,
|
||
-x, -y, z, x, -y, z, x, y, z, -x, y, z};
|
||
cube.indices = {0, 2, 1, 0, 3, 2, 4, 5, 6, 4, 6, 7, 0, 1, 5, 0, 5, 4,
|
||
1, 2, 6, 1, 6, 5, 2, 3, 7, 2, 7, 6, 3, 0, 4, 3, 4, 7};
|
||
return cube;
|
||
}
|
||
|
||
std::string temp_path(const char* name) { return std::string(VDE_TEST_TEMP_DIR) + name; }
|
||
|
||
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 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::size_t count_occurrences(const std::string& text, const std::string& needle) {
|
||
std::size_t count = 0;
|
||
for (std::size_t p = text.find(needle); p != std::string::npos;
|
||
p = text.find(needle, p + needle.size())) {
|
||
++count;
|
||
}
|
||
return count;
|
||
}
|
||
|
||
const char* kVerticesThree =
|
||
"<vertices>"
|
||
"<vertex><coordinates><x>0</x><y>0</y><z>0</z></coordinates></vertex>"
|
||
"<vertex><coordinates><x>1</x><y>0</y><z>0</z></coordinates></vertex>"
|
||
"<vertex><coordinates><x>0</x><y>1</y><z>0</z></coordinates></vertex>"
|
||
"</vertices>";
|
||
|
||
const char* kTriangle012 = "<volume><triangle><v1>0</v1><v2>1</v2><v3>2</v3></triangle></volume>";
|
||
|
||
std::string mesh_xml(const std::string& vertices, const std::string& volumes) {
|
||
return "<mesh>" + vertices + volumes + "</mesh>";
|
||
}
|
||
|
||
std::string object_xml(int id, const std::string& content) {
|
||
return "<object id=\"" + std::to_string(id) + "\">" + content + "</object>";
|
||
}
|
||
|
||
std::string amf_xml(const std::string& objects, const char* unit = "millimeter") {
|
||
return std::string("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<amf unit=\"") +
|
||
unit + "\" version=\"1.1\">\n" + objects + "</amf>\n";
|
||
}
|
||
|
||
std::string valid_object(int id) {
|
||
return object_xml(id, mesh_xml(kVerticesThree, kTriangle012));
|
||
}
|
||
|
||
// ── 测试夹具 ─────────────────────────────────────────────
|
||
class AmfPluginFixture : public ::testing::Test {
|
||
protected:
|
||
void SetUp() override {
|
||
g_last_error.clear();
|
||
g_live_bodies = 0;
|
||
host_ = make_mock_host();
|
||
ASSERT_TRUE(api_.open(&load_error_)) << load_error_;
|
||
VdePluginInfo info{};
|
||
info.struct_size = static_cast<std::uint32_t>(sizeof(VdePluginInfo));
|
||
ASSERT_EQ(api_.query(VDE_PLUGIN_ABI_MAJOR, &info), VDE_PLUGIN_OK);
|
||
ASSERT_STREQ(info.id, kPluginId);
|
||
VdePluginRegistration registration{};
|
||
registration.struct_size = static_cast<std::uint32_t>(sizeof(VdePluginRegistration));
|
||
ASSERT_EQ(api_.init(&host_, ®istration), VDE_PLUGIN_OK) << g_last_error;
|
||
ASSERT_EQ(registration.kind, VDE_PLUGIN_KIND_FORMAT);
|
||
ASSERT_NE(registration.format, nullptr);
|
||
format_ = registration.format;
|
||
}
|
||
|
||
void TearDown() override {
|
||
if (api_.shutdown) api_.shutdown();
|
||
api_.close();
|
||
EXPECT_EQ(g_live_bodies, 0) << "mock 实体未全部销毁";
|
||
}
|
||
|
||
int read_memory_text(const std::string& text, VdeBody** bodies, std::size_t* count) {
|
||
g_last_error.clear();
|
||
return format_->read_memory(&host_, text.empty() ? nullptr : text.data(),
|
||
text.size(), bodies, count);
|
||
}
|
||
|
||
void release(VdeBody* bodies, std::size_t count) {
|
||
if (!bodies) return;
|
||
for (std::size_t i = 0; i < count; ++i) {
|
||
if (bodies[i]) host_.body_destroy(bodies[i]);
|
||
}
|
||
host_.free(bodies);
|
||
}
|
||
|
||
VdeBody make_body(const Cube& cube) {
|
||
return host_.body_from_mesh(cube.positions.data(), cube.positions.size() / 3u,
|
||
cube.indices.data(), cube.indices.size() / 3u);
|
||
}
|
||
|
||
VdePluginHost host_{};
|
||
PluginApi api_;
|
||
const VdeFormatPlugin* format_ = nullptr;
|
||
std::string load_error_;
|
||
};
|
||
|
||
} // namespace
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 1. 元信息 / probe / manifest
|
||
// ═══════════════════════════════════════════════════════════
|
||
TEST_F(AmfPluginFixture, QueryDescribeAndProbe) {
|
||
VdePluginInfo info{};
|
||
info.struct_size = static_cast<std::uint32_t>(sizeof(VdePluginInfo));
|
||
ASSERT_EQ(api_.query(VDE_PLUGIN_ABI_MAJOR, &info), VDE_PLUGIN_OK);
|
||
EXPECT_STREQ(info.id, "com.vde.official.amf");
|
||
EXPECT_STREQ(info.name, "VDE Official AMF Format");
|
||
EXPECT_STREQ(info.version, "1.0.0");
|
||
EXPECT_EQ(info.kind, VDE_PLUGIN_KIND_FORMAT);
|
||
EXPECT_EQ(info.abi_major, static_cast<std::uint32_t>(VDE_PLUGIN_ABI_MAJOR));
|
||
EXPECT_EQ(info.abi_minor, static_cast<std::uint32_t>(VDE_PLUGIN_ABI_MINOR));
|
||
EXPECT_STREQ(info.capabilities, "format:amf.read;format:amf.write");
|
||
EXPECT_STREQ(info.license, "Apache-2.0");
|
||
|
||
ASSERT_NE(format_, nullptr);
|
||
EXPECT_STREQ(format_->format_id, "amf");
|
||
EXPECT_STREQ(format_->extensions, ".amf");
|
||
EXPECT_NE(format_->probe, nullptr);
|
||
EXPECT_NE(format_->read_file, nullptr);
|
||
EXPECT_NE(format_->read_memory, nullptr);
|
||
EXPECT_NE(format_->write_file, nullptr);
|
||
EXPECT_NE(format_->write_memory, nullptr);
|
||
ASSERT_NE(format_->describe, nullptr);
|
||
EXPECT_NE(std::string(format_->describe()).find("AMF"), std::string::npos);
|
||
|
||
const auto probe_text = [this](const char* text) {
|
||
return format_->probe(text, std::strlen(text));
|
||
};
|
||
EXPECT_EQ(format_->probe(nullptr, 0), 0);
|
||
EXPECT_EQ(probe_text("<?xml version=\"1.0\"?>\n<amf unit=\"millimeter\">\n"), 100);
|
||
EXPECT_EQ(probe_text("\xEF\xBB\xBF\n<!-- c -->\n<amf>\n"), 100);
|
||
EXPECT_EQ(probe_text("<amf/>"), 100);
|
||
EXPECT_EQ(probe_text("<amf:model>"), 0);
|
||
EXPECT_EQ(probe_text("<amfX>"), 0);
|
||
EXPECT_EQ(probe_text("<model>"), 0);
|
||
EXPECT_EQ(probe_text("PLY\n"), 0);
|
||
}
|
||
|
||
TEST_F(AmfPluginFixture, ManifestDeclaresIdCapabilitiesAndAbi) {
|
||
const std::string manifest =
|
||
std::string(VDE_PLUGIN_OFFICIAL_DIR) + "/amf/vde-plugin.json";
|
||
const std::string text = read_text_file(manifest);
|
||
ASSERT_FALSE(text.empty()) << manifest;
|
||
EXPECT_NE(text.find("\"id\": \"com.vde.official.amf\""), std::string::npos);
|
||
EXPECT_NE(text.find("\"kind\": \"format\""), std::string::npos);
|
||
EXPECT_NE(text.find("\"major\": 1"), std::string::npos);
|
||
EXPECT_NE(text.find("\"minor\": 3"), std::string::npos);
|
||
EXPECT_NE(text.find("format:amf.read"), std::string::npos);
|
||
EXPECT_NE(text.find("format:amf.write"), std::string::npos);
|
||
EXPECT_NE(text.find("vde_official_amf.dll"), std::string::npos);
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 2. 读取
|
||
// ═══════════════════════════════════════════════════════════
|
||
TEST_F(AmfPluginFixture, ReadSingleObjectIgnoresMetadataMaterialAndColor) {
|
||
const std::string text = amf_xml(
|
||
"<metadata type=\"name\">part & demo</metadata>\n"
|
||
"<material id=\"1\"><color><r>1</r><g>0</g><b>0</b><a>1</a></color></material>\n"
|
||
"<texture id=\"1\"><metadata type=\"name\">tex</metadata></texture>\n"
|
||
"<constellation id=\"1\"><instance objectid=\"7\"/></constellation>\n" +
|
||
object_xml(7,
|
||
"<metadata type=\"description\">object</metadata>" +
|
||
mesh_xml(kVerticesThree, kTriangle012)));
|
||
VdeBody* bodies = nullptr;
|
||
std::size_t count = 0;
|
||
ASSERT_EQ(read_memory_text(text, &bodies, &count), VDE_PLUGIN_OK) << g_last_error;
|
||
ASSERT_EQ(count, 1u);
|
||
ASSERT_NE(bodies, nullptr);
|
||
const MockBody* body = as_mock_const(bodies[0]);
|
||
ASSERT_NE(body, nullptr);
|
||
EXPECT_EQ(body->positions.size(), 9u);
|
||
EXPECT_EQ(body->indices, (std::vector<std::uint32_t>{0, 1, 2}));
|
||
EXPECT_DOUBLE_EQ(body->positions[0], 0.0);
|
||
EXPECT_DOUBLE_EQ(body->positions[3], 1.0);
|
||
EXPECT_DOUBLE_EQ(body->positions[7], 1.0);
|
||
release(bodies, count);
|
||
}
|
||
|
||
TEST_F(AmfPluginFixture, ReadMultipleObjectsWithInchUnitScaling) {
|
||
const std::string text = amf_xml(
|
||
object_xml(10, mesh_xml(kVerticesThree, kTriangle012)) +
|
||
object_xml(20, mesh_xml("<vertices>"
|
||
"<vertex><coordinates><x>2</x><y>0</y><z>0</z>"
|
||
"</coordinates></vertex>"
|
||
"<vertex><coordinates><x>3</x><y>0</y><z>0</z>"
|
||
"</coordinates></vertex>"
|
||
"<vertex><coordinates><x>2</x><y>1</y><z>0</z>"
|
||
"</coordinates></vertex>"
|
||
"</vertices>",
|
||
"<volume>"
|
||
"<triangle><v1>0</v1><v2>1</v2><v3>2</v3></triangle>"
|
||
"</volume>")),
|
||
"inch");
|
||
VdeBody* bodies = nullptr;
|
||
std::size_t count = 0;
|
||
ASSERT_EQ(read_memory_text(text, &bodies, &count), VDE_PLUGIN_OK) << g_last_error;
|
||
ASSERT_EQ(count, 2u);
|
||
const MockBody* first = as_mock_const(bodies[0]);
|
||
const MockBody* second = as_mock_const(bodies[1]);
|
||
ASSERT_NE(first, nullptr);
|
||
ASSERT_NE(second, nullptr);
|
||
EXPECT_EQ(first->indices, (std::vector<std::uint32_t>{0, 1, 2}));
|
||
EXPECT_DOUBLE_EQ(first->positions[3], 25.4);
|
||
EXPECT_DOUBLE_EQ(first->positions[7], 25.4);
|
||
EXPECT_DOUBLE_EQ(second->positions[0], 50.8);
|
||
EXPECT_DOUBLE_EQ(second->positions[3], 76.2);
|
||
release(bodies, count);
|
||
}
|
||
|
||
TEST_F(AmfPluginFixture, ReadMultipleVolumesMergesIntoOneBody) {
|
||
const std::string volume_a =
|
||
"<volume><triangle><v1>0</v1><v2>1</v2><v3>2</v3></triangle></volume>";
|
||
const std::string volume_b =
|
||
"<volume><triangle><v1>2</v1><v2>1</v2><v3>0</v3></triangle></volume>";
|
||
const std::string text =
|
||
amf_xml(object_xml(1, mesh_xml(kVerticesThree, volume_a + volume_b)));
|
||
VdeBody* bodies = nullptr;
|
||
std::size_t count = 0;
|
||
ASSERT_EQ(read_memory_text(text, &bodies, &count), VDE_PLUGIN_OK) << g_last_error;
|
||
ASSERT_EQ(count, 1u);
|
||
const MockBody* body = as_mock_const(bodies[0]);
|
||
ASSERT_NE(body, nullptr);
|
||
EXPECT_EQ(body->positions.size(), 9u);
|
||
EXPECT_EQ(body->indices, (std::vector<std::uint32_t>{0, 1, 2, 2, 1, 0}));
|
||
release(bodies, count);
|
||
}
|
||
|
||
TEST_F(AmfPluginFixture, ReadFileMatchesReadMemory) {
|
||
const std::string text = amf_xml(valid_object(3));
|
||
const std::string path = temp_path("vde_amf_read.amf");
|
||
ASSERT_TRUE(write_text_file(path, text));
|
||
VdeBody* bodies = nullptr;
|
||
std::size_t count = 0;
|
||
g_last_error.clear();
|
||
ASSERT_EQ(format_->read_file(&host_, path.c_str(), &bodies, &count), VDE_PLUGIN_OK)
|
||
<< g_last_error;
|
||
ASSERT_EQ(count, 1u);
|
||
const MockBody* body = as_mock_const(bodies[0]);
|
||
ASSERT_NE(body, nullptr);
|
||
EXPECT_EQ(body->positions.size(), 9u);
|
||
EXPECT_EQ(body->indices, (std::vector<std::uint32_t>{0, 1, 2}));
|
||
release(bodies, count);
|
||
std::remove(path.c_str());
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 3. 读取错误路径
|
||
// ═══════════════════════════════════════════════════════════
|
||
TEST_F(AmfPluginFixture, ReadRejectsMalformedTruncatedAndOutOfRangeInput) {
|
||
const std::string valid = valid_object(1);
|
||
const std::string mesh = mesh_xml(kVerticesThree, kTriangle012);
|
||
struct BadCase {
|
||
const char* name;
|
||
std::string text;
|
||
int expected;
|
||
};
|
||
const std::vector<BadCase> cases = {
|
||
{"empty-buffer", "", VDE_PLUGIN_ERR_INVALID_ARG},
|
||
{"whitespace-only", " \n\t\r\n", VDE_PLUGIN_ERR_PARSE},
|
||
{"non-amf-root", "<model/>", VDE_PLUGIN_ERR_PARSE},
|
||
{"truncated-root", "<amf", VDE_PLUGIN_ERR_PARSE},
|
||
{"truncated-document", "<amf><object id=\"1\">", VDE_PLUGIN_ERR_PARSE},
|
||
{"mismatched-tags", "<amf><object id=\"1\"></amf></object>", VDE_PLUGIN_ERR_PARSE},
|
||
{"text-before-root", "garbage<amf/>", VDE_PLUGIN_ERR_PARSE},
|
||
{"text-after-root", "<amf>" + valid + "</amf>junk", VDE_PLUGIN_ERR_PARSE},
|
||
{"second-root", "<amf>" + valid + "</amf><amf>" + valid + "</amf>",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"doctype", "<!DOCTYPE amf><amf>" + valid + "</amf>", VDE_PLUGIN_ERR_PARSE},
|
||
{"unquoted-attribute", "<amf unit=millimeter>" + valid + "</amf>",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"unclosed-comment", "<amf><!-- comment", VDE_PLUGIN_ERR_PARSE},
|
||
{"unknown-entity", "<amf><metadata>a &bogus; b</metadata>" + valid + "</amf>",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"missing-object-id", "<amf><object>" + mesh + "</object></amf>",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"zero-object-id", "<amf>" + object_xml(0, mesh) + "</amf>",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"negative-object-id",
|
||
"<amf><object id=\"-1\">" + mesh + "</object></amf>", VDE_PLUGIN_ERR_PARSE},
|
||
{"duplicate-object-id", "<amf>" + valid_object(1) + valid_object(1) + "</amf>",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"object-without-mesh", "<amf><object id=\"1\"></object></amf>",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"object-without-objects", "<amf unit=\"millimeter\"/>", VDE_PLUGIN_ERR_PARSE},
|
||
{"mesh-without-vertices",
|
||
"<amf><object id=\"1\"><mesh><volume><triangle><v1>0</v1><v2>0</v2><v3>0</v3>"
|
||
"</triangle></volume></mesh></object></amf>",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"mesh-without-volume",
|
||
"<amf>" + object_xml(1, mesh_xml(kVerticesThree, "")) + "</amf>",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"empty-volume",
|
||
"<amf>" +
|
||
object_xml(1, mesh_xml(kVerticesThree,
|
||
std::string("<volume></volume>") + kTriangle012)) +
|
||
"</amf>",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"duplicate-mesh",
|
||
"<amf>" + object_xml(1, mesh + mesh) + "</amf>", VDE_PLUGIN_ERR_PARSE},
|
||
{"empty-vertices",
|
||
"<amf>" + object_xml(1, mesh_xml("<vertices></vertices>", kTriangle012)) +
|
||
"</amf>",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"vertex-without-coordinates",
|
||
"<amf>" +
|
||
object_xml(1, mesh_xml("<vertices><vertex></vertex></vertices>",
|
||
kTriangle012)) +
|
||
"</amf>",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"coordinates-missing-z",
|
||
"<amf>" +
|
||
object_xml(1, mesh_xml("<vertices><vertex><coordinates><x>0</x><y>0</y>"
|
||
"</coordinates></vertex></vertices>",
|
||
kTriangle012)) +
|
||
"</amf>",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"duplicate-coordinate",
|
||
"<amf>" +
|
||
object_xml(1, mesh_xml("<vertices><vertex><coordinates><x>0</x><x>1</x>"
|
||
"<y>0</y><z>0</z></coordinates></vertex></vertices>",
|
||
kTriangle012)) +
|
||
"</amf>",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"duplicate-coordinates",
|
||
"<amf>" +
|
||
object_xml(1, mesh_xml("<vertices><vertex><coordinates><x>0</x><y>0</y>"
|
||
"<z>0</z></coordinates><coordinates><x>0</x><y>0</y>"
|
||
"<z>0</z></coordinates></vertex></vertices>",
|
||
kTriangle012)) +
|
||
"</amf>",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"bad-coordinate-number",
|
||
"<amf>" +
|
||
object_xml(1, mesh_xml("<vertices><vertex><coordinates><x>abc</x><y>0</y>"
|
||
"<z>0</z></coordinates></vertex></vertices>",
|
||
kTriangle012)) +
|
||
"</amf>",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"nan-coordinate",
|
||
"<amf>" +
|
||
object_xml(1, mesh_xml("<vertices><vertex><coordinates><x>nan</x><y>0</y>"
|
||
"<z>0</z></coordinates></vertex></vertices>",
|
||
kTriangle012)) +
|
||
"</amf>",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"triangle-missing-v3",
|
||
"<amf>" +
|
||
object_xml(1, mesh_xml(kVerticesThree,
|
||
"<volume><triangle><v1>0</v1><v2>1</v2></triangle>"
|
||
"</volume>")) +
|
||
"</amf>",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"triangle-negative-index",
|
||
"<amf>" +
|
||
object_xml(1, mesh_xml(kVerticesThree,
|
||
"<volume><triangle><v1>-1</v1><v2>1</v2><v3>2</v3>"
|
||
"</triangle></volume>")) +
|
||
"</amf>",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"triangle-out-of-range",
|
||
"<amf>" +
|
||
object_xml(1, mesh_xml(kVerticesThree,
|
||
"<volume><triangle><v1>0</v1><v2>1</v2><v3>3</v3>"
|
||
"</triangle></volume>")) +
|
||
"</amf>",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"triangle-degenerate",
|
||
"<amf>" +
|
||
object_xml(1, mesh_xml(kVerticesThree,
|
||
"<volume><triangle><v1>0</v1><v2>1</v2><v3>1</v3>"
|
||
"</triangle></volume>")) +
|
||
"</amf>",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"triangle-value-child",
|
||
"<amf>" +
|
||
object_xml(1, mesh_xml(kVerticesThree,
|
||
"<volume><triangle><v1><b>0</b></v1><v2>1</v2><v3>2</v3>"
|
||
"</triangle></volume>")) +
|
||
"</amf>",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"unknown-unit", amf_xml(valid, "furlong"), VDE_PLUGIN_ERR_PARSE},
|
||
};
|
||
for (const BadCase& test_case : cases) {
|
||
VdeBody* bodies = reinterpret_cast<VdeBody*>(0x1);
|
||
std::size_t count = 123;
|
||
const int rc = read_memory_text(test_case.text, &bodies, &count);
|
||
EXPECT_EQ(rc, test_case.expected) << test_case.name;
|
||
EXPECT_EQ(bodies, nullptr) << test_case.name;
|
||
EXPECT_EQ(count, 0u) << test_case.name;
|
||
EXPECT_FALSE(g_last_error.empty()) << test_case.name;
|
||
}
|
||
}
|
||
|
||
TEST_F(AmfPluginFixture, ReadFileMissingAndEmptyReturnExplicitErrors) {
|
||
const std::string missing = temp_path("vde_amf_does_not_exist.amf");
|
||
std::remove(missing.c_str());
|
||
VdeBody* bodies = nullptr;
|
||
std::size_t count = 0;
|
||
g_last_error.clear();
|
||
EXPECT_EQ(format_->read_file(&host_, missing.c_str(), &bodies, &count),
|
||
VDE_PLUGIN_ERR_IO);
|
||
EXPECT_EQ(bodies, nullptr);
|
||
EXPECT_EQ(count, 0u);
|
||
EXPECT_FALSE(g_last_error.empty());
|
||
|
||
const std::string empty = temp_path("vde_amf_empty.amf");
|
||
ASSERT_TRUE(write_text_file(empty, ""));
|
||
g_last_error.clear();
|
||
EXPECT_EQ(format_->read_file(&host_, empty.c_str(), &bodies, &count),
|
||
VDE_PLUGIN_ERR_PARSE);
|
||
EXPECT_EQ(bodies, nullptr);
|
||
EXPECT_EQ(count, 0u);
|
||
EXPECT_FALSE(g_last_error.empty());
|
||
std::remove(empty.c_str());
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 4. 写出:每体一个 object / 计数 / 重读
|
||
// ═══════════════════════════════════════════════════════════
|
||
TEST_F(AmfPluginFixture, WriteFileCreatesOneObjectPerBody) {
|
||
VdeBody body_a = make_body(make_cube(2.0, 3.0, 4.0));
|
||
VdeBody body_b = make_body(make_cube(1.0, 1.0, 1.0));
|
||
ASSERT_NE(body_a, nullptr);
|
||
ASSERT_NE(body_b, nullptr);
|
||
const std::vector<VdeBody> bodies = {body_a, body_b};
|
||
const std::string path = temp_path("vde_amf_write.amf");
|
||
std::remove(path.c_str());
|
||
g_last_error.clear();
|
||
ASSERT_EQ(format_->write_file(&host_, path.c_str(), bodies.data(), bodies.size()),
|
||
VDE_PLUGIN_OK)
|
||
<< g_last_error;
|
||
|
||
const std::string text = read_text_file(path);
|
||
ASSERT_FALSE(text.empty());
|
||
EXPECT_EQ(text.rfind("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", 0), 0u);
|
||
EXPECT_NE(text.find("<amf unit=\"millimeter\" version=\"1.1\">"), std::string::npos);
|
||
EXPECT_EQ(count_occurrences(text, "<object "), 2u);
|
||
EXPECT_NE(text.find("<object id=\"1\">"), std::string::npos);
|
||
EXPECT_NE(text.find("<object id=\"2\">"), std::string::npos);
|
||
// mock body_tessellate 以三角面展开:每立方体 12 面 -> 36 顶点;两体 72/24。
|
||
EXPECT_EQ(count_occurrences(text, "<vertex>"), 72u);
|
||
EXPECT_EQ(count_occurrences(text, "<triangle>"), 24u);
|
||
EXPECT_EQ(count_occurrences(text, "<v1>"), 24u);
|
||
EXPECT_EQ(count_occurrences(text, "<x>"), 72u);
|
||
|
||
// 写出的文件必须能被同一插件重读为两个三角网格实体。
|
||
VdeBody* reimported = nullptr;
|
||
std::size_t reimported_count = 0;
|
||
g_last_error.clear();
|
||
ASSERT_EQ(format_->read_file(&host_, path.c_str(), &reimported, &reimported_count),
|
||
VDE_PLUGIN_OK)
|
||
<< g_last_error;
|
||
ASSERT_EQ(reimported_count, 2u);
|
||
const MockBody* first = as_mock_const(reimported[0]);
|
||
const MockBody* second = as_mock_const(reimported[1]);
|
||
ASSERT_NE(first, nullptr);
|
||
ASSERT_NE(second, nullptr);
|
||
EXPECT_EQ(first->positions.size(), 36u * 3u);
|
||
EXPECT_EQ(first->indices.size(), 12u * 3u);
|
||
EXPECT_EQ(second->positions.size(), 36u * 3u);
|
||
EXPECT_EQ(second->indices.size(), 12u * 3u);
|
||
release(reimported, reimported_count);
|
||
|
||
host_.body_destroy(body_a);
|
||
host_.body_destroy(body_b);
|
||
std::remove(path.c_str());
|
||
}
|
||
|
||
TEST_F(AmfPluginFixture, WriteMemoryMatchesWriteFile) {
|
||
VdeBody body = make_body(make_cube(2.0, 2.0, 2.0));
|
||
ASSERT_NE(body, nullptr);
|
||
VdeBuffer buffer{};
|
||
g_last_error.clear();
|
||
ASSERT_EQ(format_->write_memory(&host_, &body, 1, &buffer), VDE_PLUGIN_OK)
|
||
<< g_last_error;
|
||
ASSERT_NE(buffer.data, nullptr);
|
||
const std::string memory_text(static_cast<const char*>(buffer.data), buffer.size);
|
||
host_.free(buffer.data);
|
||
|
||
const std::string path = temp_path("vde_amf_memory.amf");
|
||
std::remove(path.c_str());
|
||
ASSERT_EQ(format_->write_file(&host_, path.c_str(), &body, 1), VDE_PLUGIN_OK)
|
||
<< g_last_error;
|
||
const std::string file_text = read_text_file(path);
|
||
EXPECT_EQ(memory_text, file_text);
|
||
EXPECT_NE(memory_text.find("<amf unit=\"millimeter\" version=\"1.1\">"),
|
||
std::string::npos);
|
||
EXPECT_EQ(count_occurrences(memory_text, "<object "), 1u);
|
||
host_.body_destroy(body);
|
||
std::remove(path.c_str());
|
||
}
|
||
|
||
TEST_F(AmfPluginFixture, WriteEmptyEntityListIsRejected) {
|
||
VdeBody* no_bodies = nullptr;
|
||
VdeBuffer buffer{};
|
||
g_last_error.clear();
|
||
EXPECT_EQ(format_->write_memory(&host_, no_bodies, 0, &buffer),
|
||
VDE_PLUGIN_ERR_INTERNAL);
|
||
EXPECT_EQ(buffer.data, nullptr);
|
||
EXPECT_EQ(buffer.size, 0u);
|
||
EXPECT_FALSE(g_last_error.empty());
|
||
|
||
const std::string path = temp_path("vde_amf_empty_list.amf");
|
||
g_last_error.clear();
|
||
EXPECT_EQ(format_->write_file(&host_, path.c_str(), no_bodies, 0),
|
||
VDE_PLUGIN_ERR_INTERNAL);
|
||
EXPECT_FALSE(g_last_error.empty());
|
||
std::remove(path.c_str());
|
||
}
|
||
|
||
TEST_F(AmfPluginFixture, WriteReportsTessellationAndPathFailures) {
|
||
VdeBody failing = make_body(make_cube(1.0, 1.0, 1.0));
|
||
ASSERT_NE(failing, nullptr);
|
||
as_mock(failing)->fail_tessellate = true;
|
||
const std::string path = temp_path("vde_amf_tess_fail.amf");
|
||
g_last_error.clear();
|
||
EXPECT_EQ(format_->write_file(&host_, path.c_str(), &failing, 1),
|
||
VDE_PLUGIN_ERR_INTERNAL);
|
||
EXPECT_FALSE(g_last_error.empty());
|
||
|
||
as_mock(failing)->fail_tessellate = false;
|
||
as_mock(failing)->empty_tessellate = true;
|
||
g_last_error.clear();
|
||
EXPECT_EQ(format_->write_file(&host_, path.c_str(), &failing, 1),
|
||
VDE_PLUGIN_ERR_INTERNAL);
|
||
EXPECT_FALSE(g_last_error.empty());
|
||
host_.body_destroy(failing);
|
||
|
||
// 不存在的输出目录 -> IO。
|
||
VdeBody body = make_body(make_cube(1.0, 1.0, 1.0));
|
||
ASSERT_NE(body, nullptr);
|
||
const std::string bad_path = temp_path("vde_amf_no_such_dir/out.amf");
|
||
g_last_error.clear();
|
||
EXPECT_EQ(format_->write_file(&host_, bad_path.c_str(), &body, 1),
|
||
VDE_PLUGIN_ERR_IO);
|
||
EXPECT_FALSE(g_last_error.empty());
|
||
host_.body_destroy(body);
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 5. 宿主能力 / 参数校验
|
||
// ═══════════════════════════════════════════════════════════
|
||
TEST_F(AmfPluginFixture, MissingHostServicesReturnUnsupported) {
|
||
const std::string text = amf_xml(valid_object(1));
|
||
VdePluginHost read_host = host_;
|
||
read_host.body_from_mesh = nullptr;
|
||
VdeBody* bodies = nullptr;
|
||
std::size_t count = 0;
|
||
g_last_error.clear();
|
||
EXPECT_EQ(format_->read_memory(&read_host, text.data(), text.size(), &bodies, &count),
|
||
VDE_PLUGIN_ERR_UNSUPPORTED);
|
||
EXPECT_EQ(bodies, nullptr);
|
||
EXPECT_EQ(count, 0u);
|
||
EXPECT_FALSE(g_last_error.empty());
|
||
|
||
VdeBody body = make_body(make_cube(1.0, 1.0, 1.0));
|
||
ASSERT_NE(body, nullptr);
|
||
VdePluginHost write_host = host_;
|
||
write_host.body_tessellate = nullptr;
|
||
VdeBuffer buffer{};
|
||
g_last_error.clear();
|
||
EXPECT_EQ(format_->write_memory(&write_host, &body, 1, &buffer),
|
||
VDE_PLUGIN_ERR_UNSUPPORTED);
|
||
EXPECT_EQ(buffer.data, nullptr);
|
||
EXPECT_FALSE(g_last_error.empty());
|
||
host_.body_destroy(body);
|
||
}
|
||
|
||
TEST_F(AmfPluginFixture, EntryPointArgumentValidation) {
|
||
VdePluginInfo info{};
|
||
info.struct_size = static_cast<std::uint32_t>(sizeof(VdePluginInfo));
|
||
EXPECT_EQ(api_.query(VDE_PLUGIN_ABI_MAJOR + 1u, &info), VDE_PLUGIN_ERR_ABI);
|
||
EXPECT_EQ(api_.query(VDE_PLUGIN_ABI_MAJOR, nullptr), VDE_PLUGIN_ERR_ABI);
|
||
|
||
VdePluginRegistration registration{};
|
||
registration.struct_size = static_cast<std::uint32_t>(sizeof(VdePluginRegistration));
|
||
EXPECT_EQ(api_.init(nullptr, ®istration), VDE_PLUGIN_ERR_INVALID_ARG);
|
||
EXPECT_EQ(api_.init(&host_, nullptr), VDE_PLUGIN_ERR_ABI);
|
||
registration.struct_size = 1u;
|
||
EXPECT_EQ(api_.init(&host_, ®istration), VDE_PLUGIN_ERR_ABI);
|
||
|
||
EXPECT_EQ(format_->write_memory(&host_, nullptr, 0, nullptr),
|
||
VDE_PLUGIN_ERR_INVALID_ARG);
|
||
EXPECT_EQ(format_->write_file(&host_, nullptr, nullptr, 0),
|
||
VDE_PLUGIN_ERR_INVALID_ARG);
|
||
VdeBody* bodies = nullptr;
|
||
std::size_t count = 0;
|
||
EXPECT_EQ(format_->read_file(&host_, nullptr, &bodies, &count),
|
||
VDE_PLUGIN_ERR_INVALID_ARG);
|
||
EXPECT_EQ(format_->read_memory(&host_, nullptr, 0, &bodies, &count),
|
||
VDE_PLUGIN_ERR_INVALID_ARG);
|
||
EXPECT_EQ(format_->read_memory(&host_, "<amf/>", 6, nullptr, &count),
|
||
VDE_PLUGIN_ERR_INVALID_ARG);
|
||
EXPECT_EQ(format_->read_memory(&host_, "<amf/>", 6, &bodies, nullptr),
|
||
VDE_PLUGIN_ERR_INVALID_ARG);
|
||
}
|