409 lines
16 KiB
C++
409 lines
16 KiB
C++
#include "vde/foundation/format_detect.h"
|
||
|
||
#include <gtest/gtest.h>
|
||
|
||
#include <array>
|
||
#include <cstdio>
|
||
#include <fstream>
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
namespace {
|
||
|
||
using vde::io::DetectedFormat;
|
||
using vde::io::FileFormat;
|
||
using vde::io::detect_format;
|
||
using vde::io::detect_format_file;
|
||
using vde::io::format_name;
|
||
using vde::io::cad_format_info;
|
||
using vde::io::cad_format_info_for_path;
|
||
using vde::io::cad_format_registry;
|
||
using vde::io::FormatAccess;
|
||
|
||
std::vector<uint8_t> bytes_of(std::string_view s) {
|
||
return std::vector<uint8_t>(s.begin(), s.end());
|
||
}
|
||
|
||
void append_u32le(std::vector<uint8_t>& out, uint32_t v) {
|
||
out.push_back(static_cast<uint8_t>(v & 0xFF));
|
||
out.push_back(static_cast<uint8_t>((v >> 8) & 0xFF));
|
||
out.push_back(static_cast<uint8_t>((v >> 16) & 0xFF));
|
||
out.push_back(static_cast<uint8_t>((v >> 24) & 0xFF));
|
||
}
|
||
|
||
DetectedFormat expect_magic(const std::vector<uint8_t>& data, FileFormat expected,
|
||
const std::string& hint = {}) {
|
||
const DetectedFormat r = detect_format(data, hint);
|
||
EXPECT_EQ(r.format, expected) << "reason=" << r.reason;
|
||
EXPECT_GT(r.confidence, 0.5) << "reason=" << r.reason;
|
||
EXPECT_LE(r.confidence, 1.0);
|
||
EXPECT_FALSE(r.reason.empty());
|
||
return r;
|
||
}
|
||
|
||
std::string temp_path(const char* name) {
|
||
return std::string(VDE_TEST_TEMP_DIR) + name;
|
||
}
|
||
|
||
} // namespace
|
||
|
||
TEST(FormatDetect, StepIfcMagic) {
|
||
const auto step = bytes_of("ISO-10303-21;\nHEADER;\nFILE_SCHEMA(('AUTOMOTIVE_DESIGN'));\nENDSEC;\n");
|
||
expect_magic(step, FileFormat::Step);
|
||
const auto ifc = bytes_of("ISO-10303-21;\nHEADER;\nFILE_SCHEMA(('IFC4'));\nENDSEC;\n");
|
||
const DetectedFormat r = detect_format(ifc);
|
||
EXPECT_EQ(r.format, FileFormat::Ifc) << r.reason;
|
||
EXPECT_GT(r.confidence, 0.9);
|
||
}
|
||
|
||
TEST(FormatDetect, IgesMagic) {
|
||
std::vector<uint8_t> data;
|
||
for (const char* section : {"S", "G", "D"}) {
|
||
std::string line(72, ' ');
|
||
line += section;
|
||
line += " 1";
|
||
line.resize(80, ' ');
|
||
data.insert(data.end(), line.begin(), line.end());
|
||
data.push_back('\n');
|
||
}
|
||
expect_magic(data, FileFormat::Iges);
|
||
}
|
||
|
||
TEST(FormatDetect, DxfMagic) {
|
||
const auto ascii = bytes_of("0\nSECTION\n2\nHEADER\n$ACADVER\nAC1027\n0\nENDSEC\n0\nEOF\n");
|
||
expect_magic(ascii, FileFormat::Dxf);
|
||
const auto binary = bytes_of("AutoCAD Binary DXF\r\n\x1a\x00rest");
|
||
expect_magic(binary, FileFormat::Dxf);
|
||
}
|
||
|
||
TEST(FormatDetect, DwgMagic) {
|
||
auto data = bytes_of("AC1032");
|
||
data.resize(128, 0);
|
||
expect_magic(data, FileFormat::Dwg);
|
||
}
|
||
|
||
TEST(FormatDetect, StlAsciiAndBinary) {
|
||
const auto ascii = bytes_of("solid test\nfacet normal 0 0 1\nouter loop\nendloop\nendsolid\n");
|
||
expect_magic(ascii, FileFormat::Stl);
|
||
std::vector<uint8_t> binary(80, 0);
|
||
binary.resize(80);
|
||
append_u32le(binary, 2); // 2 triangles
|
||
binary.resize(84 + 50 * 2, 0); // 84 + 100 bytes
|
||
expect_magic(binary, FileFormat::Stl);
|
||
}
|
||
|
||
TEST(FormatDetect, PlyObjMagic) {
|
||
expect_magic(bytes_of("ply\nformat ascii 1.0\nelement vertex 4\nend_header\n"), FileFormat::Ply);
|
||
expect_magic(bytes_of("# comment\nv 0 0 0\nv 1 0 0\nf 1 2 3\n"), FileFormat::Obj);
|
||
}
|
||
|
||
TEST(FormatDetect, GltfJsonAndGlb) {
|
||
expect_magic(bytes_of("{\n \"asset\": {\"version\": \"2.0\", \"generator\": \"glTF\"}\n}\n"),
|
||
FileFormat::Gltf);
|
||
std::vector<uint8_t> glb = bytes_of("glTF");
|
||
append_u32le(glb, 2); // version
|
||
append_u32le(glb, 20); // length
|
||
glb.resize(20, 0);
|
||
expect_magic(glb, FileFormat::Gltf);
|
||
}
|
||
|
||
TEST(FormatDetect, ThreeMfZipMagic) {
|
||
auto data = bytes_of("PK\x03\x04");
|
||
data.resize(64, 0);
|
||
const std::string payload = "[Content_Types].xml" "3D/3dmodel.model" "3dmanufacturing";
|
||
data.insert(data.end(), payload.begin(), payload.end());
|
||
expect_magic(data, FileFormat::ThreeMf);
|
||
}
|
||
|
||
TEST(FormatDetect, PdfMagic) {
|
||
expect_magic(bytes_of("%PDF-1.7\n1 0 obj\n<< /Type /Catalog >>\nendobj\n"), FileFormat::Pdf3d);
|
||
}
|
||
|
||
TEST(FormatDetect, LasLazMagic) {
|
||
auto las = bytes_of("LASF");
|
||
las.resize(256, 0);
|
||
expect_magic(las, FileFormat::Las);
|
||
auto laz = bytes_of("LASF");
|
||
laz.resize(256, 0);
|
||
const std::string vlr = "laszip encoded";
|
||
laz.insert(laz.end(), vlr.begin(), vlr.end());
|
||
expect_magic(laz, FileFormat::Laz);
|
||
}
|
||
|
||
TEST(FormatDetect, E57AndVdeMagic) {
|
||
auto e57 = bytes_of("ASTM-E57");
|
||
e57.resize(64, 0);
|
||
expect_magic(e57, FileFormat::E57);
|
||
auto vde = bytes_of("VDE1");
|
||
vde.resize(64, 0);
|
||
expect_magic(vde, FileFormat::Vde);
|
||
}
|
||
|
||
TEST(FormatDetect, XtSatJtMagic) {
|
||
expect_magic(bytes_of("**ABCDEFGHIJKLMNOPQRSTUVWXYZ\nPARASOLID TRANSMIT FILE\n"),
|
||
FileFormat::Xt);
|
||
expect_magic(bytes_of("ACIS SAT 22000 0 0 0\nbody $1 $-1 $-1\n"), FileFormat::Sat);
|
||
expect_magic(bytes_of("JT File Version 10.0\nJT B-Rep segment\n"), FileFormat::Jt);
|
||
}
|
||
|
||
TEST(FormatDetect, VrmlColladaAmfMagic) {
|
||
expect_magic(bytes_of("#VRML V2.0 utf8\nGroup { children [] }\n"), FileFormat::Vrml);
|
||
expect_magic(bytes_of("<?xml version=\"1.0\"?>\n<COLLADA xmlns=\"http://www.collada.org\">\n"),
|
||
FileFormat::Collada);
|
||
expect_magic(bytes_of("<?xml version=\"1.0\"?>\n<amf unit=\"millimeter\">\n"), FileFormat::Amf);
|
||
}
|
||
|
||
TEST(FormatDetect, ExtensionFallback) {
|
||
const auto unknown = bytes_of("\x01\x02\x03unknown-payload");
|
||
const DetectedFormat igs = detect_format(unknown, "part.igs");
|
||
EXPECT_EQ(igs.format, FileFormat::Iges);
|
||
EXPECT_LT(igs.confidence, 0.5);
|
||
EXPECT_NE(igs.reason.find("扩展名"), std::string::npos);
|
||
|
||
EXPECT_EQ(detect_format(unknown, "scan.PTX").format, FileFormat::Ptx);
|
||
EXPECT_EQ(detect_format(unknown, "part.x_t").format, FileFormat::Xt);
|
||
EXPECT_EQ(detect_format(unknown, "part.gcode").format, FileFormat::Gcode);
|
||
EXPECT_EQ(detect_format(unknown, "part.msh").format, FileFormat::Mesh);
|
||
EXPECT_EQ(detect_format(unknown, "drawing.dwg").format, FileFormat::Dwg);
|
||
}
|
||
|
||
TEST(FormatDetect, MagicWinsOverConflictingExtension) {
|
||
const auto step = bytes_of("ISO-10303-21;\nHEADER;\nENDSEC;\n");
|
||
const DetectedFormat r = detect_format(step, "drawing.dwg");
|
||
EXPECT_EQ(r.format, FileFormat::Step) << r.reason;
|
||
EXPECT_GT(r.confidence, 0.9);
|
||
EXPECT_NE(r.reason.find("dwg"), std::string::npos) << r.reason;
|
||
|
||
auto dwg = bytes_of("AC1032");
|
||
dwg.resize(64, 0);
|
||
const DetectedFormat r2 = detect_format(dwg, "part.stl");
|
||
EXPECT_EQ(r2.format, FileFormat::Dwg) << r2.reason;
|
||
EXPECT_NE(r2.reason.find("stl"), std::string::npos) << r2.reason;
|
||
}
|
||
|
||
TEST(FormatDetect, UnknownFormatAndEmptyInput) {
|
||
const DetectedFormat unknown = detect_format(bytes_of("hello world, not a CAD file"), "file.xyz");
|
||
EXPECT_EQ(unknown.format, FileFormat::Unknown);
|
||
EXPECT_DOUBLE_EQ(unknown.confidence, 0.0);
|
||
EXPECT_FALSE(unknown.reason.empty());
|
||
|
||
const DetectedFormat empty = detect_format({});
|
||
EXPECT_EQ(empty.format, FileFormat::Unknown);
|
||
EXPECT_FALSE(empty.reason.empty());
|
||
// 空输入即使有扩展名提示也保守返回 Unknown(不臆测内容格式)
|
||
EXPECT_EQ(detect_format({}, "part.step").format, FileFormat::Unknown);
|
||
EXPECT_EQ(detect_format({}, "part.stl").format, FileFormat::Unknown);
|
||
}
|
||
|
||
TEST(FormatDetect, NulBytesSafe) {
|
||
std::vector<uint8_t> nul(64, 0);
|
||
const DetectedFormat r = detect_format(nul);
|
||
EXPECT_EQ(r.format, FileFormat::Unknown); // 纯 NUL 不匹配任何魔数
|
||
EXPECT_FALSE(r.reason.empty());
|
||
// NUL 前缀 + 扩展名回退(非空内容)
|
||
EXPECT_EQ(detect_format(nul, "part.las").format, FileFormat::Las);
|
||
// NUL 与文本/魔数混合不应崩溃
|
||
std::vector<uint8_t> mixed = bytes_of("ISO-10303-21;");
|
||
mixed.insert(mixed.end(), 32, 0);
|
||
EXPECT_EQ(detect_format(mixed).format, FileFormat::Step);
|
||
}
|
||
|
||
|
||
TEST(FormatDetect, AllRequestedExtensionsRecognized) {
|
||
const auto unknown = bytes_of("\x01\x02\x03unknown-payload");
|
||
struct Row { const char* path; FileFormat fmt; };
|
||
const Row rows[] = {
|
||
{"a.dwg", FileFormat::Dwg}, {"a.dxf", FileFormat::Dxf},
|
||
{"a.dwt", FileFormat::Dwt}, {"a.dws", FileFormat::Dws},
|
||
{"a.dwf", FileFormat::Dwf}, {"a.dwfx", FileFormat::Dwf},
|
||
{"a.3dxml", FileFormat::ThreeDXml}, {"a.ifc", FileFormat::Ifc},
|
||
{"a.prt", FileFormat::Prt}, {"a.3dm", FileFormat::ThreeDm},
|
||
{"a.model", FileFormat::Model}, {"a.obj", FileFormat::Obj},
|
||
{"a.vda", FileFormat::Vda}, {"a.jt", FileFormat::Jt},
|
||
{"a.stl", FileFormat::Stl}, {"a.sat", FileFormat::Sat},
|
||
{"a.sab", FileFormat::Sat}, {"a.stp", FileFormat::Step},
|
||
{"a.step", FileFormat::Step}, {"a.igs", FileFormat::Iges},
|
||
{"a.iges", FileFormat::Iges}, {"a.x_t", FileFormat::Xt},
|
||
{"a.x_b", FileFormat::Xt}, {"a.sldprt", FileFormat::Sldprt},
|
||
{"a.sldasm", FileFormat::Sldasm}, {"a.par", FileFormat::Par},
|
||
{"a.asm", FileFormat::Asm}, {"a.psm", FileFormat::Psm},
|
||
{"a.CATPart", FileFormat::CatPart},
|
||
{"a.CATProduct", FileFormat::CatProduct},
|
||
{"a.cgr", FileFormat::Cgr}, {"a.ipt", FileFormat::Ipt},
|
||
{"a.iam", FileFormat::Iam}, {"a.rvt", FileFormat::Rvt},
|
||
{"a.prt.1", FileFormat::Prt}, {"a.asm.12", FileFormat::Asm},
|
||
};
|
||
for (const auto& row : rows) {
|
||
const DetectedFormat r = detect_format(unknown, row.path);
|
||
EXPECT_EQ(r.format, row.fmt) << row.path << " reason=" << r.reason;
|
||
EXPECT_NE(cad_format_info_for_path(row.path), nullptr) << row.path;
|
||
}
|
||
}
|
||
|
||
TEST(FormatDetect, NewMagicSignatures) {
|
||
auto dwg = bytes_of("AC1032");
|
||
dwg.resize(64, 0);
|
||
const DetectedFormat dwt = detect_format(dwg, "template.dwt");
|
||
EXPECT_EQ(dwt.format, FileFormat::Dwt) << dwt.reason;
|
||
|
||
expect_magic(bytes_of("(DWF V06.00)\r\n(End of DWF)\r\n"), FileFormat::Dwf);
|
||
expect_magic(bytes_of("3D Geometry File Format 5\r\n"), FileFormat::ThreeDm);
|
||
expect_magic(bytes_of("VDAFS 2.0\n"), FileFormat::Vda);
|
||
|
||
// 最小 ZIP 头 + 3DXML 标记(检测器只做容器前缀扫描)
|
||
std::string zip = "PK\x03\x04";
|
||
zip += std::string(60, '\0');
|
||
zip += "3DXML";
|
||
const DetectedFormat r = detect_format(bytes_of(zip), "part.3dxml");
|
||
EXPECT_EQ(r.format, FileFormat::ThreeDXml) << r.reason;
|
||
}
|
||
|
||
TEST(FormatDetect, CapabilityRegistryCoversAll) {
|
||
EXPECT_EQ(cad_format_registry().size(), 43u);
|
||
EXPECT_EQ(cad_format_info(FileFormat::Unknown), nullptr);
|
||
const auto* dwg = cad_format_info(FileFormat::Dwg);
|
||
ASSERT_NE(dwg, nullptr);
|
||
EXPECT_TRUE(dwg->native());
|
||
EXPECT_TRUE(dwg->can_read());
|
||
EXPECT_TRUE(dwg->can_write());
|
||
|
||
const auto* sldprt = cad_format_info(FileFormat::Sldprt);
|
||
ASSERT_NE(sldprt, nullptr);
|
||
EXPECT_FALSE(sldprt->native());
|
||
EXPECT_TRUE(sldprt->can_read()); // 允许外部转换器
|
||
EXPECT_FALSE(sldprt->can_write());
|
||
|
||
const auto* dwf = cad_format_info(FileFormat::Dwf);
|
||
ASSERT_NE(dwf, nullptr);
|
||
EXPECT_EQ(dwf->access, FormatAccess::ConverterOnly);
|
||
|
||
for (const auto& info : cad_format_registry()) {
|
||
EXPECT_NE(info.format, FileFormat::Unknown) << info.name;
|
||
EXPECT_FALSE(info.extensions.empty()) << info.name;
|
||
EXPECT_NE(cad_format_info(info.format), nullptr) << info.name;
|
||
}
|
||
}
|
||
|
||
TEST(FormatDetect, FormatNames) {
|
||
EXPECT_STREQ(format_name(FileFormat::Unknown), "Unknown");
|
||
EXPECT_STREQ(format_name(FileFormat::Step), "STEP");
|
||
EXPECT_STREQ(format_name(FileFormat::Dwg), "DWG");
|
||
EXPECT_STREQ(format_name(FileFormat::ThreeMf), "3MF");
|
||
EXPECT_STREQ(format_name(FileFormat::Pdf3d), "PDF3D");
|
||
EXPECT_STREQ(format_name(FileFormat::Vde), "VDE");
|
||
EXPECT_STREQ(format_name(FileFormat::Gcode), "GCODE");
|
||
}
|
||
|
||
|
||
TEST(FormatDetect, VdeTextJsonByMagic) {
|
||
const std::string path = temp_path("fmt_detect_vde_text.vde");
|
||
{
|
||
std::ofstream f(path, std::ios::binary);
|
||
ASSERT_TRUE(f.good());
|
||
const auto data = bytes_of(
|
||
"{\n \"format\": \"VDE-TEXT\",\n \"spec_version\": \"1.0\",\n"
|
||
" \"geometry_included\": true\n}\n");
|
||
f.write(reinterpret_cast<const char*>(data.data()),
|
||
static_cast<std::streamsize>(data.size()));
|
||
}
|
||
const DetectedFormat d = detect_format_file(path);
|
||
std::remove(path.c_str());
|
||
EXPECT_EQ(d.format, FileFormat::Vde) << d.reason;
|
||
EXPECT_GT(d.confidence, 0.9);
|
||
EXPECT_NE(d.reason.find("VDE-TEXT"), std::string::npos);
|
||
}
|
||
|
||
TEST(FormatDetect, FileApi) {
|
||
const std::string step_path = temp_path("fmt_detect_step.stp");
|
||
{
|
||
std::ofstream f(step_path, std::ios::binary);
|
||
ASSERT_TRUE(f.good());
|
||
const auto data = bytes_of("ISO-10303-21;\nHEADER;\nFILE_SCHEMA(('AP214'));\nENDSEC;\n");
|
||
f.write(reinterpret_cast<const char*>(data.data()),
|
||
static_cast<std::streamsize>(data.size()));
|
||
}
|
||
const DetectedFormat step = detect_format_file(step_path);
|
||
std::remove(step_path.c_str());
|
||
EXPECT_EQ(step.format, FileFormat::Step) << step.reason;
|
||
EXPECT_GT(step.confidence, 0.9);
|
||
|
||
const DetectedFormat missing = detect_format_file(temp_path("fmt_detect_missing_XXXX.bin"));
|
||
EXPECT_EQ(missing.format, FileFormat::Unknown);
|
||
EXPECT_FALSE(missing.reason.empty());
|
||
|
||
const std::string empty_path = temp_path("fmt_detect_empty.dat");
|
||
{ std::ofstream f(empty_path, std::ios::binary); }
|
||
const DetectedFormat empty = detect_format_file(empty_path);
|
||
std::remove(empty_path.c_str());
|
||
EXPECT_EQ(empty.format, FileFormat::Unknown);
|
||
EXPECT_FALSE(empty.reason.empty());
|
||
}
|
||
|
||
TEST(FormatDetect, BinaryStlNotIgesOnColumn73Byte) {
|
||
// 80 字节头 + u32 count + 50 字节三角 = 134 字节;第 73 字节是 'S',
|
||
// 旧顺序会先命中 IGES 的 80 列弱启发式。
|
||
std::vector<uint8_t> bytes(134, 0);
|
||
bytes[72] = 'S';
|
||
bytes[80] = 1;
|
||
const DetectedFormat d = detect_format(bytes, "inertia_boom.stl");
|
||
EXPECT_EQ(d.format, FileFormat::Stl) << d.reason;
|
||
EXPECT_GE(d.confidence, 0.9);
|
||
}
|
||
|
||
TEST(FormatDetect, ConfidenceBounds) {
|
||
const auto step = bytes_of("ISO-10303-21;\nHEADER;\nENDSEC;\n");
|
||
const DetectedFormat magic = detect_format(step);
|
||
EXPECT_GE(magic.confidence, 0.9);
|
||
EXPECT_LE(magic.confidence, 1.0);
|
||
const auto junk = bytes_of("0123456789abcdef");
|
||
const DetectedFormat fb = detect_format(junk, "x.e57");
|
||
EXPECT_EQ(fb.format, FileFormat::E57);
|
||
EXPECT_GE(fb.confidence, 0.0);
|
||
EXPECT_LT(fb.confidence, 0.5);
|
||
}
|
||
|
||
|
||
TEST(FormatDetect, CatPartNotBinaryStl) {
|
||
// 旧 detect_stl 的宽松尺寸启发式会把该 CATPart 头误判为 binary STL:
|
||
// 200 B >= 84 + 50*1,且偏移 80 的随机字节可被当作三角面数。
|
||
std::string fake(200, '\0');
|
||
fake.replace(0, 7, "V5_CFV2");
|
||
fake[80] = 1;
|
||
std::vector<uint8_t> bytes(fake.begin(), fake.end());
|
||
const DetectedFormat d = detect_format(bytes, "sample.CATPart");
|
||
EXPECT_EQ(d.format, FileFormat::CatPart) << d.reason;
|
||
|
||
// 即使尺寸精确等于 84+50*1,也必须由 V5_CFV2 容器头优先。
|
||
std::string exact(134, '\0');
|
||
exact.replace(0, 7, "V5_CFV2");
|
||
exact[80] = 1;
|
||
std::vector<uint8_t> exact_bytes(exact.begin(), exact.end());
|
||
const DetectedFormat d2 = detect_format(exact_bytes, "sample2.CATPart");
|
||
EXPECT_EQ(d2.format, FileFormat::CatPart) << d2.reason;
|
||
}
|
||
|
||
TEST(FormatDetect, StlStrongCriteriaWinOverIgesWeakHeuristic) {
|
||
// P0-2:binary STL 头/三角数据可随机命中 IGES 第 73 列 S/G/D/P/T;
|
||
// 精确尺寸判据必须先于 IGES 弱启发式,保证合法 STL 不被误判。
|
||
std::vector<uint8_t> binary(80, 0);
|
||
for (size_t i = 0; i < binary.size(); ++i) binary[i] = static_cast<uint8_t>(i * 37 + 11);
|
||
binary[72] = 'S'; // 旧 detect_iges 单行启发式会命中
|
||
binary[73] = 'G';
|
||
append_u32le(binary, 1); // 1 triangle
|
||
binary.resize(84 + 50 * 1, 7); // 随机三角数据
|
||
const DetectedFormat r = detect_format(binary);
|
||
EXPECT_EQ(r.format, FileFormat::Stl) << r.reason;
|
||
EXPECT_GT(r.confidence, 0.9);
|
||
}
|
||
|
||
TEST(FormatDetect, IgesNeedsAtLeastTwoValidCards) {
|
||
// 单行第 73 列字母不足以判定 IGES(P0-2 硬化)。
|
||
std::vector<uint8_t> data(80, ' ');
|
||
data[72] = 'S';
|
||
data.push_back('\n');
|
||
data.insert(data.end(), 40, 'x');
|
||
const DetectedFormat r = detect_format(data);
|
||
EXPECT_NE(r.format, FileFormat::Iges) << r.reason;
|
||
}
|
||
|