// WP-P4-35 官方 CAM G-code 后处理插件测试(确定性 mock 宿主 + 真实 C ABI 动态加载)。 // // 覆盖:manifest/query/init(ABI 1.2 cam_strategy);Fanuc/Grbl 头尾差异;G21/G90/G17、 // G0 安全 Z、G1 raster、G0 退刀、M30;行号开关;确定性;参数非法/未知 post; // 空模型/反序列化失败/bbox 失败/宿主能力缺失;body_destroy 生命周期。 #include #include #include #include #include #include #include #include #if defined(_WIN32) #ifndef NOMINMAX #define NOMINMAX #endif #include #else #include #endif #include "vde/plugin/vde_plugin_api.h" #ifndef VDE_CAM_PLUGIN_PATH #if defined(VDE_PLUGIN_OFFICIAL_DIR) #if defined(_WIN32) #define VDE_CAM_PLUGIN_PATH VDE_PLUGIN_OFFICIAL_DIR "/cam_gcode/vde_official_cam_gcode.dll" #elif defined(__APPLE__) #define VDE_CAM_PLUGIN_PATH VDE_PLUGIN_OFFICIAL_DIR "/cam_gcode/libvde_official_cam_gcode.dylib" #else #define VDE_CAM_PLUGIN_PATH VDE_PLUGIN_OFFICIAL_DIR "/cam_gcode/libvde_official_cam_gcode.so" #endif #elif defined(_WIN32) #define VDE_CAM_PLUGIN_PATH "build-fb-cam2/cam_gcode/vde_official_cam_gcode.dll" #elif defined(__APPLE__) #define VDE_CAM_PLUGIN_PATH "build-fb-cam2/cam_gcode/libvde_official_cam_gcode.dylib" #else #define VDE_CAM_PLUGIN_PATH "build-fb-cam2/cam_gcode/libvde_official_cam_gcode.so" #endif #endif #ifndef VDE_CAM_MANIFEST_PATH #if defined(VDE_PLUGIN_OFFICIAL_DIR) #define VDE_CAM_MANIFEST_PATH VDE_PLUGIN_OFFICIAL_DIR "/cam_gcode/vde-plugin.json" #else #define VDE_CAM_MANIFEST_PATH "build-fb-cam2/cam_gcode/vde-plugin.json" #endif #endif namespace { constexpr const char* kPluginId = "com.vde.official.cam.gcode"; #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(::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(::dlsym(library, name)); } void close_library(LibraryHandle library) { if (library) ::dlclose(library); } #endif struct MockBody { double bbox_min[3]; double bbox_max[3]; }; struct MockState { bool fail_deserialize = false; bool fail_bbox = false; bool fail_alloc = false; double bbox_min[3] = {-10.0, -5.0, 0.0}; double bbox_max[3] = {10.0, 5.0, 2.0}; int live_bodies = 0; }; MockState g_state; std::string g_last_error; void* mock_alloc(std::size_t size) { if (g_state.fail_alloc) return nullptr; return std::malloc(size); } void* mock_realloc(void* ptr, std::size_t size) { return std::realloc(ptr, size); } void mock_free(void* ptr) { std::free(ptr); } void mock_free_buffer(void* ptr) { std::free(ptr); } void mock_log(int, const char*) {} void mock_set_error(const char* message) { g_last_error = message != nullptr ? message : ""; } VdeBody mock_body_from_serialized(const void* data, std::size_t size) { if (g_state.fail_deserialize || data == nullptr || size == 0) return nullptr; MockBody* body = new MockBody(); std::memcpy(body->bbox_min, g_state.bbox_min, sizeof(body->bbox_min)); std::memcpy(body->bbox_max, g_state.bbox_max, sizeof(body->bbox_max)); ++g_state.live_bodies; return reinterpret_cast(body); } int mock_body_bbox(VdeBody handle, double out_min[3], double out_max[3]) { if (handle == nullptr || out_min == nullptr || out_max == nullptr) return -1; if (g_state.fail_bbox) return -1; const MockBody* body = reinterpret_cast(handle); std::memcpy(out_min, body->bbox_min, sizeof(body->bbox_min)); std::memcpy(out_max, body->bbox_max, sizeof(body->bbox_max)); return 0; } void mock_body_destroy(VdeBody handle) { if (handle == nullptr) return; delete reinterpret_cast(handle); --g_state.live_bodies; } VdePluginHost make_mock_host() { VdePluginHost host{}; host.struct_size = static_cast(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.free_buffer = &mock_free_buffer; host.body_from_serialized = &mock_body_from_serialized; host.body_bbox = &mock_body_bbox; host.body_destroy = &mock_body_destroy; return host; } struct PluginApi { LibraryHandle library = nullptr; VdePluginQueryFn query = nullptr; VdePluginInitFn init = nullptr; VdePluginShutdownFn shutdown = nullptr; const VdeCamStrategyPlugin* strategy = nullptr; ~PluginApi() { close(); } bool open(std::string* error) { const std::string path = std::string(VDE_CAM_PLUGIN_PATH); library = open_library(path.c_str()); if (!library) { if (error) *error = "cannot load plugin: " + path; return false; } query = reinterpret_cast(find_symbol(library, "vde_plugin_query")); init = reinterpret_cast(find_symbol(library, "vde_plugin_init")); shutdown = reinterpret_cast(find_symbol(library, "vde_plugin_shutdown")); if (!query || !init || !shutdown) { if (error) *error = "plugin missing vde_plugin_query/init/shutdown"; close(); return false; } return true; } void close() { if (library) { close_library(library); library = nullptr; } query = nullptr; init = nullptr; shutdown = nullptr; strategy = nullptr; } }; std::string read_file(const char* path) { std::ifstream in(path, std::ios::binary); if (!in.good()) return {}; std::ostringstream ss; ss << in.rdbuf(); return ss.str(); } std::size_t count_substring(const std::string& text, const std::string& needle) { std::size_t count = 0; for (std::size_t pos = 0; (pos = text.find(needle, pos)) != std::string::npos; pos += needle.size()) { ++count; } return count; } class CamGcodePluginTest : public ::testing::Test { protected: void SetUp() override { g_state = MockState{}; g_last_error.clear(); host_ = make_mock_host(); ASSERT_TRUE(api_.open(&load_error_)) << load_error_; VdePluginInfo info{}; info.struct_size = static_cast(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(sizeof(VdePluginRegistration)); ASSERT_EQ(api_.init(&host_, ®istration), VDE_PLUGIN_OK) << g_last_error; ASSERT_EQ(registration.kind, VDE_PLUGIN_KIND_CAM_STRATEGY); ASSERT_NE(registration.cam_strategy, nullptr); strategy_ = registration.cam_strategy; } void TearDown() override { if (api_.shutdown) api_.shutdown(); api_.close(); strategy_ = nullptr; EXPECT_EQ(g_state.live_bodies, 0) << "mock bodies leaked"; } struct InvokeResult { int code = 0; std::string output; std::string error; }; InvokeResult invoke(const std::string& model, const char* args = nullptr) { InvokeResult result; VdeBuffer buffer{}; g_last_error.clear(); result.code = strategy_->invoke( &host_, model.empty() ? nullptr : model.data(), model.size(), args, &buffer); result.error = g_last_error; if (buffer.data != nullptr) { result.output.assign(static_cast(buffer.data), buffer.size); host_.free_buffer(buffer.data); } return result; } PluginApi api_; VdePluginHost host_{}; const VdeCamStrategyPlugin* strategy_ = nullptr; std::string load_error_; }; TEST_F(CamGcodePluginTest, ManifestAndAbiMetadata) { const std::string manifest = read_file(VDE_CAM_MANIFEST_PATH); ASSERT_FALSE(manifest.empty()) << VDE_CAM_MANIFEST_PATH; EXPECT_NE(manifest.find(kPluginId), std::string::npos); EXPECT_NE(manifest.find("\"kind\": \"cam_strategy\""), std::string::npos); EXPECT_NE(manifest.find("\"minor\": 2"), std::string::npos); EXPECT_NE(manifest.find("cam_strategy:post.gcode"), std::string::npos); VdePluginInfo info{}; info.struct_size = static_cast(sizeof(VdePluginInfo)); ASSERT_EQ(api_.query(VDE_PLUGIN_ABI_MAJOR, &info), VDE_PLUGIN_OK); EXPECT_EQ(info.kind, VDE_PLUGIN_KIND_CAM_STRATEGY); EXPECT_EQ(info.abi_major, 1u); EXPECT_EQ(info.abi_minor, 2u); ASSERT_NE(strategy_, nullptr); EXPECT_STREQ(strategy_->strategy_id, "cam.post.gcode"); EXPECT_STREQ(strategy_->postprocessor_id, "fanuc"); EXPECT_STREQ(strategy_->machine_id, "generic-3axis"); EXPECT_NE(std::string(strategy_->describe()).find("G-code"), std::string::npos); } TEST_F(CamGcodePluginTest, FanucRasterGcodeContainsRequiredBlocks) { const char* args = "{\"stepover\":5,\"feed\":300,\"rapid\":1000,\"clearance\":2," "\"postprocessor\":\"fanuc\",\"line_numbers\":true}"; const auto result = invoke("fake-serialized-box", args); ASSERT_EQ(result.code, VDE_PLUGIN_OK) << result.error; ASSERT_FALSE(result.output.empty()); EXPECT_NE(result.output.find("G21\n"), std::string::npos); EXPECT_NE(result.output.find("G90\n"), std::string::npos); EXPECT_NE(result.output.find("G17\n"), std::string::npos); EXPECT_NE(result.output.find("G0 Z4.000000"), std::string::npos); EXPECT_NE(result.output.find("G1 Z2.000000 F300.000000"), std::string::npos); EXPECT_NE(result.output.find("M30"), std::string::npos); EXPECT_NE(result.output.find("%"), std::string::npos); EXPECT_NE(result.output.find("O0001"), std::string::npos); EXPECT_NE(result.output.find("; postprocessor: fanuc"), std::string::npos); EXPECT_NE(result.output.find("N10 G21"), std::string::npos); EXPECT_NE(result.output.find("rapid=1000.000000"), std::string::npos); // y: -5,0,5 => 3 passes => 3 条 G1 X,2 条 G1 Y。 EXPECT_EQ(count_substring(result.output, "G1 X"), 3u); EXPECT_EQ(count_substring(result.output, "G1 Y"), 2u); } TEST_F(CamGcodePluginTest, GrblRasterGcodeDiffersInHeaderAndTail) { const char* args = "{\"stepover\":5,\"feed\":300,\"rapid\":1000,\"clearance\":2," "\"postprocessor\":\"grbl\",\"line_numbers\":false}"; const auto result = invoke("fake-serialized-box", args); ASSERT_EQ(result.code, VDE_PLUGIN_OK) << result.error; EXPECT_NE(result.output.find("; GRBL post"), std::string::npos); EXPECT_NE(result.output.find("; postprocessor: grbl"), std::string::npos); EXPECT_NE(result.output.find("M5"), std::string::npos); EXPECT_NE(result.output.find("M30"), std::string::npos); EXPECT_EQ(result.output.find("%"), std::string::npos); EXPECT_EQ(result.output.find("O0001"), std::string::npos); EXPECT_EQ(result.output.find("N10 "), std::string::npos); EXPECT_EQ(count_substring(result.output, "G1 X"), 3u); } TEST_F(CamGcodePluginTest, RasterDirectionYGeneratesOrthogonalRows) { const char* args = "{\"strategy\":\"raster\",\"direction\":\"y\",\"stepover\":5," "\"postprocessor\":\"fanuc\",\"line_numbers\":false}"; const auto result = invoke("fake-serialized-box", args); ASSERT_EQ(result.code, VDE_PLUGIN_OK) << result.error; EXPECT_NE(result.output.find("strategy=raster, direction=y, passes=1"), std::string::npos); // x: -10,-5,0,5,10 => 5 rows;第一行/每行 Y 向进给 5 次,换行 G1 X 4 次, // 但每行主移动也带 X,故 G1 X 合计 9。 EXPECT_EQ(count_substring(result.output, "G1 Y"), 5u); EXPECT_EQ(count_substring(result.output, "G1 X"), 4u); EXPECT_NE(result.output.find("G1 Y5.000000 X-10.000000"), std::string::npos); EXPECT_NE(result.output.find("G1 Y5.000000 X10.000000"), std::string::npos); } TEST_F(CamGcodePluginTest, ContourClosedRectangleAndToolCompensation) { const char* args = "{\"strategy\":\"contour\",\"tool_radius\":0.5,\"finish_allowance\":0.25," "\"postprocessor\":\"fanuc\",\"line_numbers\":false}"; const auto result = invoke("fake-serialized-box", args); ASSERT_EQ(result.code, VDE_PLUGIN_OK) << result.error; EXPECT_NE(result.output.find("strategy=contour"), std::string::npos); EXPECT_NE(result.output.find("tool_radius=0.500000 finish_allowance=0.250000"), std::string::npos); // bbox X[-10,10] Y[-5,5] 外扩 0.75 => 闭合矩形。 EXPECT_NE(result.output.find("G1 X10.750000 Y-5.750000"), std::string::npos); EXPECT_NE(result.output.find("G1 Y5.750000"), std::string::npos); EXPECT_NE(result.output.find("G1 X-10.750000"), std::string::npos); EXPECT_NE(result.output.find("G1 Y-5.750000"), std::string::npos); EXPECT_EQ(count_substring(result.output, "G1 X"), 2u); EXPECT_EQ(count_substring(result.output, "G1 Y"), 2u); const auto grbl = invoke( "fake-serialized-box", "{\"strategy\":\"contour\",\"postprocessor\":\"grbl\"}"); ASSERT_EQ(grbl.code, VDE_PLUGIN_OK) << grbl.error; EXPECT_NE(grbl.output.find("; GRBL post"), std::string::npos); EXPECT_EQ(grbl.output.find("%"), std::string::npos); EXPECT_NE(grbl.output.find("M5"), std::string::npos); } TEST_F(CamGcodePluginTest, MultiPassZLevelsAndRasterInset) { const char* raster_args = "{\"strategy\":\"raster\",\"tool_radius\":0.5,\"finish_allowance\":0.25," "\"stepover\":5,\"passes\":1}"; const auto raster = invoke("fake-serialized-box", raster_args); ASSERT_EQ(raster.code, VDE_PLUGIN_OK) << raster.error; // 外扩/内缩:raster 刀心向 bbox 内缩 0.75;X[-9.25,9.25] Y[-4.25,4.25]。 EXPECT_NE(raster.output.find("G1 X9.250000 Y-4.250000"), std::string::npos); EXPECT_NE(raster.output.find("G1 Y4.250000"), std::string::npos); EXPECT_NE(raster.output.find("G0 X-9.250000 Y-4.250000"), std::string::npos); const char* multi_args = "{\"passes\":3,\"strategy\":\"raster\",\"stepover\":5}"; const auto multi = invoke("fake-serialized-box", multi_args); ASSERT_EQ(multi.code, VDE_PLUGIN_OK) << multi.error; EXPECT_NE(multi.output.find("G1 Z2.000000"), std::string::npos); EXPECT_NE(multi.output.find("G1 Z1.000000"), std::string::npos); EXPECT_NE(multi.output.find("G1 Z0.000000"), std::string::npos); EXPECT_EQ(count_substring(multi.output, "G1 Z"), 3u); EXPECT_EQ(count_substring(multi.output, "G0 Z"), 4u); // 3 次安全/进刀 + 最终退刀 EXPECT_NE(multi.output.find("passes: 3"), std::string::npos); const auto again = invoke("fake-serialized-box", multi_args); ASSERT_EQ(again.code, VDE_PLUGIN_OK) << again.error; EXPECT_EQ(multi.output, again.output); } TEST_F(CamGcodePluginTest, DefaultsAndExplicitSafeZ) { const auto defaults = invoke("fake-serialized-box"); ASSERT_EQ(defaults.code, VDE_PLUGIN_OK) << defaults.error; EXPECT_NE(defaults.output.find("; postprocessor: fanuc"), std::string::npos); EXPECT_NE(defaults.output.find("G0 Z7.000000"), std::string::npos); // 2 + 5 clearance EXPECT_EQ(defaults.output.find("N10 "), std::string::npos); const auto explicit_safe_z = invoke( "fake-serialized-box", "{\"postprocessor\":\"grbl\",\"safe_z\":9.5,\"clearance\":100," "\"line_numbers\":false}"); ASSERT_EQ(explicit_safe_z.code, VDE_PLUGIN_OK) << explicit_safe_z.error; EXPECT_NE(explicit_safe_z.output.find("G0 Z9.500000"), std::string::npos); } TEST_F(CamGcodePluginTest, SameInvocationIsDeterministic) { const char* args = "{\"stepover\":2.5,\"feed\":120,\"clearance\":1,\"postprocessor\":\"fanuc\"}"; const auto first = invoke("deterministic", args); const auto second = invoke("deterministic", args); ASSERT_EQ(first.code, VDE_PLUGIN_OK) << first.error; ASSERT_EQ(second.code, VDE_PLUGIN_OK) << second.error; EXPECT_EQ(first.output, second.output); } TEST_F(CamGcodePluginTest, RejectsInvalidArguments) { const char* invalid_args[] = { "{\"stepover\":0}", "{\"stepover\":-1}", "{\"feed\":0}", "{\"rapid\":-2}", "{\"clearance\":-0.5}", "{\"postprocessor\":\"mazak\"}", "{\"line_numbers\":\"yes\"}", "{\"safe_z\":1.0}", "{\"stepover\":", "{\"stepover\":1} trailing", "{\"strategy\":\"pocket\"}", "{\"strategy\":1}", "{\"direction\":\"z\"}", "{\"passes\":0}", "{\"passes\":21}", "{\"passes\":1.5}", "{\"tool_radius\":-0.1}", "{\"finish_allowance\":-0.1}", "{\"tool_radius\":6.0}", // raster 内缩后宽度 <= 0 }; for (const char* args : invalid_args) { const auto result = invoke("fake-serialized-box", args); EXPECT_EQ(result.code, VDE_PLUGIN_ERR_INVALID_ARG) << "args should be rejected: " << args; EXPECT_FALSE(result.error.empty()) << args; EXPECT_TRUE(result.output.empty()) << args; EXPECT_EQ(g_state.live_bodies, 0) << args; } } TEST_F(CamGcodePluginTest, RejectsModelAndHostFailures) { const auto empty_model = invoke(""); EXPECT_EQ(empty_model.code, VDE_PLUGIN_ERR_INVALID_ARG); EXPECT_FALSE(empty_model.error.empty()); g_state.fail_deserialize = true; const auto deserialize_fail = invoke("fake-serialized-box"); EXPECT_EQ(deserialize_fail.code, VDE_PLUGIN_ERR_PARSE); EXPECT_NE(deserialize_fail.error.find("body_from_serialized"), std::string::npos); g_state.fail_deserialize = false; g_state.fail_bbox = true; const auto bbox_fail = invoke("fake-serialized-box"); EXPECT_EQ(bbox_fail.code, VDE_PLUGIN_ERR_INTERNAL); EXPECT_NE(bbox_fail.error.find("body_bbox"), std::string::npos); g_state.fail_bbox = false; g_state.fail_alloc = true; const auto alloc_fail = invoke("fake-serialized-box"); EXPECT_EQ(alloc_fail.code, VDE_PLUGIN_ERR_BUFFER); EXPECT_NE(alloc_fail.error.find("allocate"), std::string::npos); g_state.fail_alloc = false; // 缺少 body_bbox 的宿主:init 明确拒绝。 VdePluginHost incomplete = host_; incomplete.body_bbox = nullptr; VdePluginRegistration registration{}; registration.struct_size = static_cast(sizeof(VdePluginRegistration)); const int init_code = api_.init(&incomplete, ®istration); EXPECT_LT(init_code, 0); EXPECT_FALSE(g_last_error.empty()); } } // namespace