Files
ViewDesignEngine/tests/plugin/test_official_collada.cpp
T

866 lines
36 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// WP-P4-24 官方 COLLADA(.dae)格式插件测试(确定性 mock 宿主 + 真实 C ABI 动态加载)。
//
// 覆盖:
// 1) vde_plugin_query / init / describe / manifest 的 id、能力、扩展名与 ABI;
// 2) COLLADA 1.4/1.5 XML 读取:单 geometry、多 geometry、多个 <triangles> 合并、offset/stride;
// 2b) P4-31:<polylist>/<polygons>(vcount/p、3/4/>4 边形扇形三角化、多 input offset/stride、
// 混合 primitive、多 geometry→多 body、vcount 不匹配/索引越界/不支持 primitive 负例);
// 3) 写出:mock 宿主 body_tessellate -> 每体一个 <geometry>;write_file 与 write_memory 一致,写后重读;
// 4) 错误路径:空文件、缺少 <library_geometries>/<geometry>/positions source、越界索引、
// 非法版本、标签截断、超限;
// 5) 宿主能力缺失、入口参数校验、空实体、三角化失败等明确错误。
//
// 本测试只依赖纯 C ABI(vde_plugin_api.h)+ gtest,不链接内核。
#include <gtest/gtest.h>
#include <algorithm>
#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.collada";
#if defined(_WIN32)
constexpr const char* kPluginRelativePath = "/collada/vde_official_collada.dll";
#elif defined(__APPLE__)
constexpr const char* kPluginRelativePath = "/collada/libvde_official_collada.dylib";
#else
constexpr const char* kPluginRelativePath = "/collada/libvde_official_collada.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); }
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;
}
// ── 夹具文本 ────────────────────────────────────────────────────────
const char* kSimpleCollada =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<COLLADA xmlns=\"http://www.collada.org/2005/11/COLLADASchema\" version=\"1.4.1\">\n"
" <asset><unit name=\"millimeter\" meter=\"0.001\"/><up_axis>Y_UP</up_axis></asset>\n"
" <library_geometries>\n"
" <geometry id=\"g1\" name=\"tri\">\n"
" <mesh>\n"
" <source id=\"g1-positions\">\n"
" <float_array id=\"g1-positions-array\" count=\"9\">"
"0 0 0 10 0 0 0 10 0</float_array>\n"
" <technique_common><accessor source=\"#g1-positions-array\" count=\"3\" "
"stride=\"3\">\n"
" <param name=\"X\" type=\"float\"/><param name=\"Y\" type=\"float\"/>"
"<param name=\"Z\" type=\"float\"/>\n"
" </accessor></technique_common>\n"
" </source>\n"
" <vertices id=\"g1-vertices\"><input semantic=\"POSITION\" "
"source=\"#g1-positions\"/></vertices>\n"
" <triangles count=\"1\">\n"
" <input semantic=\"VERTEX\" source=\"#g1-vertices\" offset=\"0\"/>\n"
" <p>0 1 2</p>\n"
" </triangles>\n"
" </mesh>\n"
" </geometry>\n"
" </library_geometries>\n"
"</COLLADA>\n";
std::string multi_collada(const char* version) {
return std::string("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") +
"<COLLADA xmlns=\"http://www.collada.org/2005/11/COLLADASchema\" version=\"" +
version + "\">\n <library_geometries>\n" +
" <geometry id=\"g1\"><mesh>\n" +
" <source id=\"g1-pos\"><float_array id=\"g1-arr\" count=\"9\">"
"0 0 0 1 0 0 0 1 0</float_array></source>\n" +
" <vertices id=\"g1-v\"><input semantic=\"POSITION\" "
"source=\"#g1-pos\"/></vertices>\n" +
" <triangles count=\"1\"><input semantic=\"VERTEX\" "
"source=\"#g1-v\"/><p>0 1 2</p></triangles>\n" +
" </mesh></geometry>\n" +
" <geometry id=\"g2\"><mesh>\n" +
" <source id=\"g2-pos\"><float_array id=\"g2-arr\" count=\"12\">"
"20 0 0 21 0 0 20 1 0 20 1 1</float_array></source>\n" +
" <vertices id=\"g2-v\"><input semantic=\"POSITION\" "
"source=\"#g2-pos\"/></vertices>\n" +
" <triangles count=\"2\"><input semantic=\"VERTEX\" "
"source=\"#g2-v\"/><p>0 1 2 0 2 3</p></triangles>\n" +
" </mesh></geometry>\n" +
" </library_geometries>\n</COLLADA>\n";
}
// 多 input(offset 0=VERTEX, 1=NORMAL)的 stride 解析样例。
const char* kOffsetStrideCollada =
"<?xml version=\"1.0\"?>\n"
"<COLLADA version=\"1.4.1\"><library_geometries><geometry><mesh>\n"
" <source id=\"p\"><float_array id=\"pa\" count=\"9\">0 0 0 1 0 0 0 1 0</float_array>"
"</source>\n"
" <vertices id=\"v\"><input semantic=\"POSITION\" source=\"#p\"/></vertices>\n"
" <triangles count=\"1\">\n"
" <input semantic=\"VERTEX\" source=\"#v\" offset=\"0\"/>\n"
" <input semantic=\"NORMAL\" source=\"#p\" offset=\"1\"/>\n"
" <p>0 0 1 1 2 2</p>\n"
" </triangles>\n"
"</mesh></geometry></library_geometries></COLLADA>\n";
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;
}
// ── 测试夹具 ────────────────────────────────────────────────────────
class ColladaPluginFixture : 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_, &registration), 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_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);
}
VdeBody make_body(const MockBody& source) {
return host_.body_from_mesh(source.positions.data(),
source.positions.size() / 3u,
source.indices.data(), source.indices.size() / 3u);
}
MockBody make_triangle() {
MockBody body;
body.positions = {0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0};
body.indices = {0u, 1u, 2u};
return body;
}
void destroy_bodies(std::vector<VdeBody>& bodies) {
for (VdeBody body : bodies) host_.body_destroy(body);
bodies.clear();
}
PluginApi api_;
std::string load_error_;
VdePluginHost host_{};
const VdeFormatPlugin* format_ = nullptr;
};
} // namespace
// ── 1. 插件元信息 / 注册 ───────────────────────────────────────────
TEST_F(ColladaPluginFixture, QueryAndRegistrationMetadata) {
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.collada");
EXPECT_EQ(info.kind, VDE_PLUGIN_KIND_FORMAT);
EXPECT_EQ(info.abi_major, VDE_PLUGIN_ABI_MAJOR);
EXPECT_EQ(info.abi_minor, VDE_PLUGIN_ABI_MINOR);
EXPECT_STREQ(info.capabilities, "format:dae.read;format:dae.write");
ASSERT_NE(format_, nullptr);
EXPECT_STREQ(format_->format_id, "dae");
EXPECT_STREQ(format_->extensions, ".dae");
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);
}
TEST_F(ColladaPluginFixture, ProbeRecognizesColladaOnly) {
const std::string collada = kSimpleCollada;
EXPECT_EQ(format_->probe(collada.data(), collada.size()), 92);
const char* other = "<?xml version=\"1.0\"?><foo/>";
EXPECT_EQ(format_->probe(other, std::strlen(other)), 0);
EXPECT_EQ(format_->probe(nullptr, 0), 0);
}
// ── 2. 读取 ────────────────────────────────────────────────────────
TEST_F(ColladaPluginFixture, ReadSingleGeometryEntityCount) {
VdeBody* bodies = nullptr;
std::size_t count = 0;
ASSERT_EQ(read_text(kSimpleCollada, &bodies, &count), VDE_PLUGIN_OK) << g_last_error;
ASSERT_NE(bodies, nullptr);
ASSERT_EQ(count, 1u);
const MockBody* body = as_mock(bodies[0]);
ASSERT_NE(body, nullptr);
EXPECT_EQ(body->positions.size(), 9u);
EXPECT_EQ(body->indices.size(), 3u);
host_.body_destroy(bodies[0]);
host_.free(bodies);
}
TEST_F(ColladaPluginFixture, ReadMultiGeometryAndVersion15) {
for (const char* version : {"1.4.1", "1.5.0"}) {
const std::string text = multi_collada(version);
VdeBody* bodies = nullptr;
std::size_t count = 0;
ASSERT_EQ(read_text(text, &bodies, &count), VDE_PLUGIN_OK)
<< version << " / " << g_last_error;
ASSERT_NE(bodies, nullptr);
ASSERT_EQ(count, 2u) << "多 geometry 应生成 2 个实体 (" << version << ")";
const MockBody* first = as_mock(bodies[0]);
const MockBody* second = as_mock(bodies[1]);
EXPECT_EQ(first->indices.size(), 3u);
EXPECT_EQ(second->indices.size(), 6u);
host_.body_destroy(bodies[0]);
host_.body_destroy(bodies[1]);
host_.free(bodies);
}
}
TEST_F(ColladaPluginFixture, ReadOffsetStrideInputs) {
VdeBody* bodies = nullptr;
std::size_t count = 0;
ASSERT_EQ(read_text(kOffsetStrideCollada, &bodies, &count), VDE_PLUGIN_OK)
<< g_last_error;
ASSERT_EQ(count, 1u);
const MockBody* body = as_mock(bodies[0]);
ASSERT_NE(body, nullptr);
EXPECT_EQ(body->indices.size(), 3u);
host_.body_destroy(bodies[0]);
host_.free(bodies);
}
// ── 3. 写出与往返 ──────────────────────────────────────────────────
TEST_F(ColladaPluginFixture, WriteMemorySingleBody) {
MockBody triangle = make_triangle();
std::vector<VdeBody> bodies{make_body(triangle)};
ASSERT_NE(bodies[0], nullptr) << g_last_error;
VdeBuffer buffer{};
g_last_error.clear();
ASSERT_EQ(format_->write_memory(&host_, bodies.data(), bodies.size(), &buffer),
VDE_PLUGIN_OK) << g_last_error;
ASSERT_NE(buffer.data, nullptr);
ASSERT_GT(buffer.size, 0u);
const std::string xml(static_cast<const char*>(buffer.data), buffer.size);
EXPECT_NE(xml.find("<COLLADA"), std::string::npos);
EXPECT_NE(xml.find("<library_geometries>"), std::string::npos);
EXPECT_NE(xml.find("<geometry id=\"geometry-1\""), std::string::npos);
EXPECT_NE(xml.find("<triangles count=\"1\">"), std::string::npos);
EXPECT_NE(xml.find("<p>0 1 2</p>"), std::string::npos);
EXPECT_EQ(count_occurrences(xml, "<geometry "), 1u);
host_.free(buffer.data);
destroy_bodies(bodies);
}
TEST_F(ColladaPluginFixture, WriteMemoryMultipleBodies) {
MockBody triangle = make_triangle();
std::vector<VdeBody> bodies{make_body(triangle), make_body(triangle)};
ASSERT_NE(bodies[0], nullptr);
ASSERT_NE(bodies[1], nullptr);
VdeBuffer buffer{};
ASSERT_EQ(format_->write_memory(&host_, bodies.data(), bodies.size(), &buffer),
VDE_PLUGIN_OK) << g_last_error;
ASSERT_NE(buffer.data, nullptr);
const std::string xml(static_cast<const char*>(buffer.data), buffer.size);
EXPECT_EQ(count_occurrences(xml, "<geometry "), 2u);
host_.free(buffer.data);
destroy_bodies(bodies);
}
TEST_F(ColladaPluginFixture, WriteFileReadBackRoundTrip) {
MockBody triangle = make_triangle();
std::vector<VdeBody> bodies{make_body(triangle)};
ASSERT_NE(bodies[0], nullptr);
const std::string path = temp_path("p424_roundtrip.dae");
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 written = read_text_file(path);
ASSERT_FALSE(written.empty());
EXPECT_NE(written.find("<COLLADA"), std::string::npos);
VdeBody* again = nullptr;
std::size_t again_count = 0;
g_last_error.clear();
ASSERT_EQ(format_->read_file(&host_, path.c_str(), &again, &again_count),
VDE_PLUGIN_OK) << g_last_error;
ASSERT_EQ(again_count, 1u);
const MockBody* body = as_mock(again[0]);
ASSERT_NE(body, nullptr);
EXPECT_EQ(body->positions.size(), 9u);
EXPECT_EQ(body->indices.size(), 3u);
host_.body_destroy(again[0]);
host_.free(again);
destroy_bodies(bodies);
std::remove(path.c_str());
}
// ── 4. 错误路径 ────────────────────────────────────────────────────
TEST_F(ColladaPluginFixture, InvalidInputsRejectedWithParse) {
struct InvalidFixture {
const char* name;
std::string text;
} cases[] = {
{"empty", std::string()},
{"blank", " \n\t "},
{"no-COLLADA-root", "<?xml version=\"1.0\"?><foo/>"},
{"no-library-geometries",
"<?xml version=\"1.0\"?><COLLADA version=\"1.4.1\"></COLLADA>"},
{"no-geometry",
"<?xml version=\"1.0\"?><COLLADA version=\"1.4.1\"><library_geometries>"
"</library_geometries></COLLADA>"},
{"missing-positions-source",
"<COLLADA version=\"1.4.1\"><library_geometries><geometry><mesh>"
"<vertices id=\"v\"><input semantic=\"POSITION\" source=\"#absent\"/></vertices>"
"<triangles><input semantic=\"VERTEX\" source=\"#v\"/><p>0 1 2</p></triangles>"
"</mesh></geometry></library_geometries></COLLADA>"},
{"index-out-of-range",
"<COLLADA version=\"1.4.1\"><library_geometries><geometry><mesh>"
"<source id=\"p\"><float_array count=\"9\">0 0 0 1 0 0 0 1 0</float_array></source>"
"<vertices id=\"v\"><input semantic=\"POSITION\" source=\"#p\"/></vertices>"
"<triangles><input semantic=\"VERTEX\" source=\"#v\"/><p>0 1 9</p></triangles>"
"</mesh></geometry></library_geometries></COLLADA>"},
{"unsupported-version",
"<?xml version=\"1.0\"?><COLLADA version=\"2.0\"><library_geometries/>"
"</COLLADA>"},
{"truncated-tag",
"<COLLADA version=\"1.4.1\"><library_geometries><geometry><mesh>"},
{"missing-triangle-p",
"<COLLADA version=\"1.4.1\"><library_geometries><geometry><mesh>"
"<source id=\"p\"><float_array count=\"9\">0 0 0 1 0 0 0 1 0</float_array></source>"
"<vertices id=\"v\"><input semantic=\"POSITION\" source=\"#p\"/></vertices>"
"<triangles><input semantic=\"VERTEX\" source=\"#v\"/></triangles>"
"</mesh></geometry></library_geometries></COLLADA>"},
};
for (const auto& test : cases) {
VdeBody* bodies = nullptr;
std::size_t count = 0;
EXPECT_EQ(read_text(test.text, &bodies, &count), VDE_PLUGIN_ERR_PARSE)
<< "应拒绝: " << test.name;
EXPECT_EQ(bodies, nullptr) << test.name;
EXPECT_EQ(count, 0u) << test.name;
EXPECT_FALSE(g_last_error.empty()) << test.name;
}
}
TEST_F(ColladaPluginFixture, OversizeFileRejected) {
VdeBody* bodies = nullptr;
std::size_t count = 0;
const char tiny[] = "<COLLADA/>";
EXPECT_EQ(format_->read_memory(&host_, tiny, 600u * 1024u * 1024u, &bodies, &count),
VDE_PLUGIN_ERR_PARSE);
EXPECT_EQ(bodies, nullptr);
}
TEST_F(ColladaPluginFixture, GeometryCountLimitRejected) {
std::string text =
"<?xml version=\"1.0\"?><COLLADA version=\"1.4.1\"><library_geometries>";
text.reserve(4097 * 250);
for (int i = 0; i < 4097; ++i) {
text += "<geometry><mesh><source id=\"p\"><float_array count=\"9\">"
"0 0 0 1 0 0 0 1 0</float_array></source>"
"<vertices id=\"v\"><input semantic=\"POSITION\" source=\"#p\"/></vertices>"
"<triangles><input semantic=\"VERTEX\" source=\"#v\"/><p>0 1 2</p>"
"</triangles></mesh></geometry>";
}
text += "</library_geometries></COLLADA>";
VdeBody* bodies = nullptr;
std::size_t count = 0;
EXPECT_EQ(read_text(text, &bodies, &count), VDE_PLUGIN_ERR_PARSE);
EXPECT_EQ(bodies, nullptr);
EXPECT_FALSE(g_last_error.empty());
}
// ── 5. 宿主能力 / 参数校验 ─────────────────────────────────────────
TEST_F(ColladaPluginFixture, MissingHostServicesReturnUnsupported) {
VdePluginHost read_host = host_;
read_host.body_from_mesh = nullptr;
VdeBody* bodies = nullptr;
std::size_t count = 0;
EXPECT_EQ(format_->read_memory(&read_host, kSimpleCollada, std::strlen(kSimpleCollada),
&bodies, &count),
VDE_PLUGIN_ERR_UNSUPPORTED);
EXPECT_EQ(bodies, nullptr);
MockBody triangle = make_triangle();
std::vector<VdeBody> body_list{make_body(triangle)};
VdePluginHost write_host = host_;
write_host.body_tessellate = nullptr;
VdeBuffer buffer{};
EXPECT_EQ(format_->write_memory(&write_host, body_list.data(), body_list.size(), &buffer),
VDE_PLUGIN_ERR_UNSUPPORTED);
EXPECT_EQ(buffer.data, nullptr);
destroy_bodies(body_list);
}
TEST_F(ColladaPluginFixture, NullAndBadArguments) {
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, &registration), VDE_PLUGIN_ERR_INVALID_ARG);
EXPECT_EQ(api_.init(&host_, nullptr), VDE_PLUGIN_ERR_ABI);
registration.struct_size = 1u;
EXPECT_EQ(api_.init(&host_, &registration), VDE_PLUGIN_ERR_ABI);
VdeBody* bodies = nullptr;
std::size_t count = 0;
EXPECT_EQ(format_->read_memory(&host_, nullptr, 0, &bodies, &count),
VDE_PLUGIN_ERR_PARSE);
EXPECT_EQ(format_->read_memory(&host_, kSimpleCollada, std::strlen(kSimpleCollada),
nullptr, &count),
VDE_PLUGIN_ERR_INVALID_ARG);
EXPECT_EQ(format_->read_memory(&host_, kSimpleCollada, std::strlen(kSimpleCollada),
&bodies, nullptr),
VDE_PLUGIN_ERR_INVALID_ARG);
EXPECT_EQ(format_->read_file(&host_, nullptr, &bodies, &count),
VDE_PLUGIN_ERR_INVALID_ARG);
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);
VdeBuffer buffer{};
EXPECT_EQ(format_->write_memory(&host_, nullptr, 0, &buffer),
VDE_PLUGIN_ERR_INVALID_ARG);
std::vector<VdeBody> empty;
MockBody triangle = make_triangle();
std::vector<VdeBody> one{make_body(triangle)};
EXPECT_EQ(format_->write_memory(&host_, empty.data(), 0, &buffer),
VDE_PLUGIN_ERR_INVALID_ARG);
EXPECT_EQ(format_->write_file(&host_, temp_path("p424_empty.dae").c_str(),
empty.data(), 0),
VDE_PLUGIN_ERR_INVALID_ARG);
destroy_bodies(one);
}
TEST_F(ColladaPluginFixture, TessellateFailureAndEmptyTessellation) {
MockBody triangle = make_triangle();
std::vector<VdeBody> failing{make_body(triangle)};
ASSERT_NE(failing[0], nullptr);
as_mock(failing[0])->fail_tessellate = true;
VdeBuffer buffer{};
EXPECT_EQ(format_->write_memory(&host_, failing.data(), failing.size(), &buffer),
VDE_PLUGIN_ERR_INTERNAL);
EXPECT_EQ(buffer.data, nullptr);
destroy_bodies(failing);
MockBody empty = make_triangle();
std::vector<VdeBody> empty_bodies{make_body(empty)};
ASSERT_NE(empty_bodies[0], nullptr);
as_mock(empty_bodies[0])->empty_tessellate = true;
EXPECT_EQ(format_->write_memory(&host_, empty_bodies.data(), empty_bodies.size(),
&buffer),
VDE_PLUGIN_ERR_INTERNAL);
destroy_bodies(empty_bodies);
}
// ── P4-31:polylist / polygons / 混合 / vcount 不匹配 ─────────────
namespace {
std::string collada_mesh_with(const std::string& primitives,
const std::string& positions,
std::size_t position_count) {
return std::string("<?xml version=\"1.0\"?>\n") +
"<COLLADA version=\"1.4.1\"><library_geometries><geometry><mesh>\n" +
" <source id=\"p\"><float_array id=\"pa\" count=\"" +
std::to_string(position_count * 3) + "\">" + positions +
"</float_array></source>\n"
" <vertices id=\"v\"><input semantic=\"POSITION\" source=\"#p\"/></vertices>\n" +
primitives +
"</mesh></geometry></library_geometries></COLLADA>\n";
}
std::string collada_polylist(const std::string& vcount, const std::string& p,
const std::string& inputs =
" <input semantic=\"VERTEX\" source=\"#v\" offset=\"0\"/>\n") {
return " <polylist count=\"" + std::to_string(std::count(vcount.begin(), vcount.end(), ' ') + 1) +
"\">\n" + inputs + " <vcount>" + vcount + "</vcount>\n <p>" + p +
"</p>\n </polylist>\n";
}
} // namespace
TEST_F(ColladaPluginFixture, PolylistTriangleQuadPentagon) {
// 12 顶点;vcount 3/4/5 → 1 + 2 + 3 = 6 个三角形
const std::string positions =
"0 0 0 1 0 0 0 1 0 "
"2 0 0 3 0 0 3 1 0 2 1 0 "
"4 0 0 5 0 0 5 1 0 4 1 0 4.5 1.5 0";
const std::string text = collada_mesh_with(
collada_polylist("3 4 5",
"0 1 2 3 4 5 6 7 8 9 10 11"),
positions, 12);
VdeBody* bodies = nullptr;
std::size_t count = 0;
ASSERT_EQ(read_text(text, &bodies, &count), VDE_PLUGIN_OK) << g_last_error;
ASSERT_EQ(count, 1u);
MockBody* body = as_mock(bodies[0]);
EXPECT_EQ(body->positions.size() / 3u, 12u);
EXPECT_EQ(body->indices.size() / 3u, 6u);
std::vector<VdeBody> owned(bodies, bodies + count);
destroy_bodies(owned);
}
TEST_F(ColladaPluginFixture, PolygonsTriangleQuadAndIgnoreHoles) {
const std::string positions = "0 0 0 1 0 0 0 1 0 2 0 0 3 0 0 3 1 0 2 1 0";
const std::string text = collada_mesh_with(
" <polygons count=\"2\">\n"
" <input semantic=\"VERTEX\" source=\"#v\" offset=\"0\"/>\n"
" <p>0 1 2</p>\n"
" <ph>0 1</ph>\n"
" <p>3 4 5 6</p>\n"
" </polygons>\n",
positions, 7);
VdeBody* bodies = nullptr;
std::size_t count = 0;
ASSERT_EQ(read_text(text, &bodies, &count), VDE_PLUGIN_OK) << g_last_error;
ASSERT_EQ(count, 1u);
MockBody* body = as_mock(bodies[0]);
EXPECT_EQ(body->positions.size() / 3u, 7u);
EXPECT_EQ(body->indices.size() / 3u, 3u); // 三角形 1 + 四边形 2
std::vector<VdeBody> owned(bodies, bodies + count);
destroy_bodies(owned);
}
TEST_F(ColladaPluginFixture, MixedTrianglesAndPolylist) {
const std::string positions = "0 0 0 1 0 0 0 1 0 1 1 0";
const std::string primitives =
" <triangles count=\"1\">\n"
" <input semantic=\"VERTEX\" source=\"#v\" offset=\"0\"/>\n"
" <p>0 1 2</p>\n"
" </triangles>\n" +
collada_polylist("4", "0 1 2 3");
const std::string text = collada_mesh_with(primitives, positions, 4);
VdeBody* bodies = nullptr;
std::size_t count = 0;
ASSERT_EQ(read_text(text, &bodies, &count), VDE_PLUGIN_OK) << g_last_error;
ASSERT_EQ(count, 1u);
MockBody* body = as_mock(bodies[0]);
EXPECT_EQ(body->indices.size() / 3u, 3u); // 1 triangle + quad→2
std::vector<VdeBody> owned(bodies, bodies + count);
destroy_bodies(owned);
}
TEST_F(ColladaPluginFixture, PolylistWithInputOffsets) {
const std::string positions = "0 0 0 1 0 0 0 1 0";
// VERTEX offset=1,NORMAL offset=0;p 交错:n0 v0 n0 v1 n0 v2
const std::string text = collada_mesh_with(
collada_polylist("3", "0 0 0 1 0 2",
" <input semantic=\"VERTEX\" source=\"#v\" offset=\"1\"/>\n"
" <input semantic=\"NORMAL\" source=\"#p\" offset=\"0\"/>\n"),
positions, 3);
VdeBody* bodies = nullptr;
std::size_t count = 0;
ASSERT_EQ(read_text(text, &bodies, &count), VDE_PLUGIN_OK) << g_last_error;
ASSERT_EQ(count, 1u);
MockBody* body = as_mock(bodies[0]);
EXPECT_EQ(body->positions.size() / 3u, 3u);
EXPECT_EQ(body->indices.size() / 3u, 1u);
ASSERT_EQ(body->indices, (std::vector<std::uint32_t>{0u, 1u, 2u}));
std::vector<VdeBody> owned(bodies, bodies + count);
destroy_bodies(owned);
}
TEST_F(ColladaPluginFixture, PolylistMultipleGeometriesToMultipleBodies) {
const std::string text =
"<?xml version=\"1.0\"?>\n"
"<COLLADA version=\"1.4.1\"><library_geometries>\n"
" <geometry><mesh>\n"
" <source id=\"p1\"><float_array id=\"a1\" count=\"9\">0 0 0 1 0 0 0 1 0"
"</float_array></source>\n"
" <vertices id=\"v1\"><input semantic=\"POSITION\" source=\"#p1\"/></vertices>\n"
" <polylist count=\"1\"><input semantic=\"VERTEX\" source=\"#v1\" offset=\"0\"/>"
"<vcount>3</vcount><p>0 1 2</p></polylist>\n"
" </mesh></geometry>\n"
" <geometry><mesh>\n"
" <source id=\"p2\"><float_array id=\"a2\" count=\"12\">20 0 0 21 0 0 21 1 0 "
"20 1 0</float_array></source>\n"
" <vertices id=\"v2\"><input semantic=\"POSITION\" source=\"#p2\"/></vertices>\n"
" <polylist count=\"1\"><input semantic=\"VERTEX\" source=\"#v2\" offset=\"0\"/>"
"<vcount>4</vcount><p>0 1 2 3</p></polylist>\n"
" </mesh></geometry>\n"
"</library_geometries></COLLADA>\n";
VdeBody* bodies = nullptr;
std::size_t count = 0;
ASSERT_EQ(read_text(text, &bodies, &count), VDE_PLUGIN_OK) << g_last_error;
ASSERT_EQ(count, 2u);
EXPECT_EQ(as_mock(bodies[0])->indices.size() / 3u, 1u);
EXPECT_EQ(as_mock(bodies[1])->indices.size() / 3u, 2u);
std::vector<VdeBody> owned(bodies, bodies + count);
destroy_bodies(owned);
}
TEST_F(ColladaPluginFixture, RejectsVcountMismatchAndBadIndex) {
const std::string positions = "0 0 0 1 0 0 0 1 0 1 1 0";
{
// vcount 声明 3 4 共 7 顶点,但 p 只有 6 个索引
const std::string text = collada_mesh_with(
collada_polylist("3 4", "0 1 2 0 1 2"), positions, 4);
VdeBody* bodies = nullptr;
std::size_t count = 0;
g_last_error.clear();
EXPECT_EQ(read_text(text, &bodies, &count), VDE_PLUGIN_ERR_PARSE);
EXPECT_NE(g_last_error.find("vcount"), std::string::npos) << g_last_error;
}
{
// 索引越界(位置只有 4 个)
const std::string text = collada_mesh_with(
collada_polylist("3", "0 1 9"), positions, 4);
VdeBody* bodies = nullptr;
std::size_t count = 0;
g_last_error.clear();
EXPECT_EQ(read_text(text, &bodies, &count), VDE_PLUGIN_ERR_PARSE);
EXPECT_FALSE(g_last_error.empty());
}
{
// polygons 单面只有 2 个顶点(<3 → 非法)
const std::string text = collada_mesh_with(
" <polygons count=\"1\"><input semantic=\"VERTEX\" source=\"#v\" offset=\"0\"/>"
"<p>0 1</p></polygons>\n",
positions, 4);
VdeBody* bodies = nullptr;
std::size_t count = 0;
g_last_error.clear();
EXPECT_EQ(read_text(text, &bodies, &count), VDE_PLUGIN_ERR_PARSE);
EXPECT_FALSE(g_last_error.empty());
}
}
TEST_F(ColladaPluginFixture, RejectsUnsupportedPrimitive) {
const std::string text = collada_mesh_with(
" <tristrips count=\"1\"><input semantic=\"VERTEX\" source=\"#v\" offset=\"0\"/>"
"<p>0 1 2</p></tristrips>\n",
"0 0 0 1 0 0 0 1 0", 3);
VdeBody* bodies = nullptr;
std::size_t count = 0;
g_last_error.clear();
EXPECT_EQ(read_text(text, &bodies, &count), VDE_PLUGIN_ERR_PARSE);
EXPECT_NE(g_last_error.find("tristrips"), std::string::npos) << g_last_error;
}