// tests/plugin/test_plugin_tool_remote_install.cpp // P3-17c:vde_plugin_tool 远程安装/依赖解析/卸载/原子升级。 // 使用本地索引 + 本地文件/HTTP(127.0.0.1 内建 server)验证: // --fetch-packages 下载/复制 + SHA-256 校验、requires 依赖解析(缺失/冲突/循环)、 // --uninstall、--upgrade 原子替换与失败回滚。 #include #include #include #include #include "vde/fs_compat.h" #include #include #include #include #include #include #if defined(_WIN32) // winsock2.h 必须先于 windows.h,否则 sockaddr/fd_set 等会重定义(MSVC) #include #include #include #else #include #include #include #endif namespace fs = vde::fs; #ifndef VDE_PLUGIN_TOOL_EXE #define VDE_PLUGIN_TOOL_EXE "" #endif namespace { std::string read_text(const fs::path& path) { std::ifstream in(path.string(), std::ios::binary); std::ostringstream ss; ss << in.rdbuf(); return ss.str(); } bool write_text(const fs::path& path, const std::string& text) { fs::create_directories(path.parent_path()); std::ofstream out(path.string(), std::ios::binary | std::ios::trunc); if (!out) return false; out << text; return static_cast(out); } std::string unique_suffix() { return std::to_string(static_cast( std::chrono::steady_clock::now().time_since_epoch().count())); } struct ToolResult { int exit_code = -1; std::string output; }; #if defined(_WIN32) std::string quote_arg(const std::string& value) { std::string out = "\""; out += value; out += "\""; return out; } #else std::string quote_arg(const std::string& value) { return value; } #endif ToolResult run_tool(const std::vector& args, const fs::path& cwd) { ToolResult result; const fs::path output_path = fs::temp_directory_path() / ("vde_tool_out_" + unique_suffix() + ".log"); #if defined(_WIN32) std::string command_line; for (const auto& arg : args) { if (!command_line.empty()) command_line += ' '; command_line += quote_arg(arg); } SECURITY_ATTRIBUTES attrs{}; attrs.nLength = sizeof(attrs); attrs.bInheritHandle = TRUE; HANDLE out_handle = CreateFileA(output_path.string().c_str(), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, &attrs, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); if (out_handle == INVALID_HANDLE_VALUE) return result; STARTUPINFOA startup{}; startup.cb = sizeof(startup); startup.dwFlags = STARTF_USESTDHANDLES; startup.hStdOutput = out_handle; startup.hStdError = out_handle; startup.hStdInput = INVALID_HANDLE_VALUE; PROCESS_INFORMATION process{}; std::vector command(command_line.begin(), command_line.end()); command.push_back('\0'); const BOOL created = CreateProcessA(nullptr, command.data(), nullptr, nullptr, TRUE, CREATE_NO_WINDOW, nullptr, cwd.string().c_str(), &startup, &process); CloseHandle(out_handle); if (!created) return result; WaitForSingleObject(process.hProcess, 120000); DWORD exit_code = 1; GetExitCodeProcess(process.hProcess, &exit_code); CloseHandle(process.hThread); CloseHandle(process.hProcess); result.exit_code = static_cast(exit_code); #else std::string command_line; for (const auto& arg : args) { if (!command_line.empty()) command_line += ' '; command_line += quote_arg(arg); } const std::string redirect = " > '" + output_path.string() + "' 2>&1"; const int rc = std::system((command_line + redirect).c_str()); result.exit_code = WIFEXITED(rc) ? WEXITSTATUS(rc) : rc; #endif result.output = read_text(output_path); std::error_code ec; fs::remove(output_path, ec); return result; } std::string make_plugin_manifest(const std::string& id, const std::string& version, const std::string& requires_json) { std::string text = "{\n"; text += " \"id\": \"" + id + "\",\n"; text += " \"name\": \"" + id + "\",\n"; text += " \"version\": \"" + version + "\",\n"; text += " \"kind\": \"format\",\n"; text += " \"entry\": { \"windows\": \"plugin.dll\", \"linux\": \"libplugin.so\" },\n"; text += " \"abi\": { \"major\": 1, \"minor\": 3 },\n"; text += " \"capabilities\": [\"test:" + id + "\"]"; if (!requires_json.empty()) text += ",\n \"requires\": " + requires_json; text += "\n}\n"; return text; } fs::path make_plugin_package(const fs::path& root, const std::string& id, const std::string& version, const std::string& requires_json, const std::string& payload) { const fs::path dir = root / id; fs::create_directories(dir); write_text(dir / "vde-plugin.json", make_plugin_manifest(id, version, requires_json)); write_text(dir / "data.txt", payload + "\n"); // 生成一个占位二进制产物(索引会对其计算 SHA-256) write_text(dir / "plugin.dll", "binary:" + payload); return dir; } int build_index(const fs::path& plugins_dir, const fs::path& index_path, const fs::path& cwd) { const ToolResult result = run_tool( {VDE_PLUGIN_TOOL_EXE, "--build-index", plugins_dir.string(), "-o", index_path.string()}, cwd); EXPECT_EQ(result.exit_code, 0) << result.output; return result.exit_code; } ToolResult install_args(const std::string& index_or_url, const std::string& dest, const std::string& id, const std::string& version, bool fetch_packages = true) { std::vector args{VDE_PLUGIN_TOOL_EXE, "--install", index_or_url, "--dest", dest, "--allow-unsigned"}; if (!id.empty()) { args.push_back("--id"); args.push_back(id); } if (!version.empty()) { args.push_back("--version"); args.push_back(version); } if (fetch_packages) args.push_back("--fetch-packages"); return ToolResult{0, ""}; // placeholder (不使用) } std::string read_installed_version(const fs::path& dest, const std::string& id) { const fs::path manifest = dest / id / "vde-plugin.json"; const std::string text = read_text(manifest); const std::string marker = "\"version\""; const std::size_t pos = text.find(marker); if (pos == std::string::npos) return std::string(); const std::size_t quote = text.find('"', pos + marker.size()); if (quote == std::string::npos) return std::string(); const std::size_t end = text.find('"', quote + 1); if (end == std::string::npos) return std::string(); return text.substr(quote + 1, end - quote - 1); } bool contains_dir_with_suffix(const fs::path& root, const std::string& suffix) { std::error_code ec; for (const auto& entry : fs::directory_iterator(root, ec)) { if (entry.path().filename().string().find(suffix) != std::string::npos && entry.is_directory()) return true; } return false; } // ── 极简本地 HTTP server(Windows;POSIX 测试 skip)────────────────── #if defined(_WIN32) class HttpFileServer { public: explicit HttpFileServer(fs::path root) : root_(std::move(root)) {} ~HttpFileServer() { stop(); } bool start() { WSAData data{}; if (WSAStartup(MAKEWORD(2, 2), &data) != 0) return false; listen_socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (listen_socket_ == INVALID_SOCKET) return false; sockaddr_in address{}; address.sin_family = AF_INET; address.sin_addr.s_addr = htonl(INADDR_LOOPBACK); address.sin_port = 0; if (bind(listen_socket_, reinterpret_cast(&address), sizeof(address)) != 0) return false; if (listen(listen_socket_, 8) != 0) return false; int length = sizeof(address); if (getsockname(listen_socket_, reinterpret_cast(&address), &length) != 0) return false; port_ = ntohs(address.sin_port); running_ = true; thread_ = std::thread([this]() { serve_loop(); }); return true; } void stop() { if (!running_) return; running_ = false; if (listen_socket_ != INVALID_SOCKET) { closesocket(listen_socket_); listen_socket_ = INVALID_SOCKET; } if (thread_.joinable()) thread_.join(); WSACleanup(); } std::string url(const std::string& relative) const { return "http://127.0.0.1:" + std::to_string(port_) + "/" + relative; } private: void serve_loop() { while (running_) { SOCKET client = accept(listen_socket_, nullptr, nullptr); if (client == INVALID_SOCKET) break; char buffer[4096] = {}; const int received = recv(client, buffer, sizeof(buffer) - 1, 0); if (received > 0) { std::string request(buffer, static_cast(received)); std::string path = "/"; const std::size_t first_space = request.find(' '); if (first_space != std::string::npos) { const std::size_t second_space = request.find(' ', first_space + 1); if (second_space != std::string::npos) path = request.substr(first_space + 1, second_space - first_space - 1); } const std::size_t query = path.find('?'); if (query != std::string::npos) path = path.substr(0, query); if (!path.empty() && path[0] == '/') path = path.substr(1); std::string body; bool ok = !path.empty() && path.find("..") == std::string::npos; if (ok) { std::error_code ec; const fs::path file = root_ / path; if (fs::is_regular_file(file, ec) && !ec) body = read_text(file); else ok = false; } std::string response = ok ? "HTTP/1.1 200 OK\r\n" : "HTTP/1.1 404 Not Found\r\n"; response += "Content-Length: " + std::to_string(body.size()) + "\r\n"; response += "Connection: close\r\n\r\n"; response += body; send(client, response.data(), static_cast(response.size()), 0); } closesocket(client); } } fs::path root_; SOCKET listen_socket_ = INVALID_SOCKET; int port_ = 0; std::atomic running_{false}; std::thread thread_; }; #endif class PluginToolRemoteInstallTest : public ::testing::Test { protected: void SetUp() override { root_ = fs::temp_directory_path() / ("vde_tool_remote_" + unique_suffix()); fs::create_directories(root_); } void TearDown() override { std::error_code ec; fs::remove_all(root_, ec); } fs::path root_; }; TEST_F(PluginToolRemoteInstallTest, InstallFetchesDependencyChain) { const fs::path packages = root_ / "packages"; fs::create_directories(packages); make_plugin_package(packages, "com.vde.test.dep_b", "1.0.0", "", "dep-b-payload"); make_plugin_package(packages, "com.vde.test.plugin_a", "1.0.0", "[{\"id\": \"com.vde.test.dep_b\", \"version_range\": \">=1.0.0\"}]", "plugin-a-payload"); const fs::path index = root_ / "index.json"; ASSERT_EQ(build_index(packages, index, root_), 0); const fs::path dest = root_ / "install"; const ToolResult result = run_tool( {VDE_PLUGIN_TOOL_EXE, "--install", index.string(), "--dest", dest.string(), "--id", "com.vde.test.plugin_a", "--allow-unsigned", "--fetch-packages"}, root_); ASSERT_EQ(result.exit_code, 0) << result.output; EXPECT_TRUE(fs::is_regular_file(dest / "com.vde.test.plugin_a" / "vde-plugin.json")) << result.output; EXPECT_TRUE(fs::is_regular_file(dest / "com.vde.test.dep_b" / "vde-plugin.json")); EXPECT_EQ(read_installed_version(dest, "com.vde.test.dep_b"), "1.0.0"); } TEST_F(PluginToolRemoteInstallTest, MissingAndConflictingDependenciesRejected) { const fs::path packages = root_ / "packages"; fs::create_directories(packages); make_plugin_package(packages, "com.vde.test.missing_dep_plugin", "1.0.0", "[{\"id\": \"com.vde.test.absent\", \"version_range\": \"^1.0.0\"}]", "p"); make_plugin_package(packages, "com.vde.test.conflict_plugin", "1.0.0", "[{\"id\": \"com.vde.test.dep_b\", \"version_range\": \">=2.0.0\"}]", "p"); make_plugin_package(packages, "com.vde.test.dep_b", "1.0.0", "", "b"); const fs::path index = root_ / "index.json"; ASSERT_EQ(build_index(packages, index, root_), 0); const fs::path dest = root_ / "install"; ToolResult result = run_tool( {VDE_PLUGIN_TOOL_EXE, "--install", index.string(), "--dest", dest.string(), "--id", "com.vde.test.missing_dep_plugin", "--allow-unsigned", "--fetch-packages"}, root_); EXPECT_EQ(result.exit_code, 3) << result.output; EXPECT_NE(result.output.find("unresolved dependency"), std::string::npos) << result.output; EXPECT_FALSE(fs::exists(dest / "com.vde.test.missing_dep_plugin")); result = run_tool( {VDE_PLUGIN_TOOL_EXE, "--install", index.string(), "--dest", dest.string(), "--id", "com.vde.test.conflict_plugin", "--allow-unsigned", "--fetch-packages"}, root_); EXPECT_EQ(result.exit_code, 3) << result.output; EXPECT_NE(result.output.find("unresolved dependency"), std::string::npos) << result.output; } TEST_F(PluginToolRemoteInstallTest, DependencyCycleRejected) { const fs::path packages = root_ / "packages"; fs::create_directories(packages); make_plugin_package(packages, "com.vde.test.cycle_a", "1.0.0", "[{\"id\": \"com.vde.test.cycle_b\", \"version_range\": \"^1.0.0\"}]", "a"); make_plugin_package(packages, "com.vde.test.cycle_b", "1.0.0", "[{\"id\": \"com.vde.test.cycle_a\", \"version_range\": \"^1.0.0\"}]", "b"); const fs::path index = root_ / "index.json"; ASSERT_EQ(build_index(packages, index, root_), 0); const ToolResult result = run_tool( {VDE_PLUGIN_TOOL_EXE, "--install", index.string(), "--dest", (root_ / "install").string(), "--id", "com.vde.test.cycle_a", "--allow-unsigned", "--fetch-packages"}, root_); EXPECT_EQ(result.exit_code, 3) << result.output; EXPECT_NE(result.output.find("cycle"), std::string::npos) << result.output; } TEST_F(PluginToolRemoteInstallTest, UninstallRemovesPluginSafely) { const fs::path packages = root_ / "packages"; fs::create_directories(packages); make_plugin_package(packages, "com.vde.test.uninstall_me", "1.0.0", "", "u"); const fs::path index = root_ / "index.json"; ASSERT_EQ(build_index(packages, index, root_), 0); const fs::path dest = root_ / "install"; ASSERT_EQ(run_tool({VDE_PLUGIN_TOOL_EXE, "--install", index.string(), "--dest", dest.string(), "--allow-unsigned", "--fetch-packages"}, root_) .exit_code, 0); ASSERT_TRUE(fs::is_directory(dest / "com.vde.test.uninstall_me")); ToolResult result = run_tool( {VDE_PLUGIN_TOOL_EXE, "--uninstall", "com.vde.test.uninstall_me", "--dest", dest.string()}, root_); EXPECT_EQ(result.exit_code, 0) << result.output; EXPECT_FALSE(fs::exists(dest / "com.vde.test.uninstall_me")); result = run_tool( {VDE_PLUGIN_TOOL_EXE, "--uninstall", "com.vde.test.not_installed", "--dest", dest.string()}, root_); EXPECT_EQ(result.exit_code, 3) << result.output; } TEST_F(PluginToolRemoteInstallTest, UpgradeAtomicallyReplacesOldVersion) { const fs::path packages_v1 = root_ / "v1" / "packages"; fs::create_directories(packages_v1); make_plugin_package(packages_v1, "com.vde.test.upgrade_me", "1.0.0", "", "v1"); const fs::path index_v1 = root_ / "v1" / "index.json"; ASSERT_EQ(build_index(packages_v1, index_v1, root_), 0); const fs::path dest = root_ / "install"; ASSERT_EQ(run_tool({VDE_PLUGIN_TOOL_EXE, "--install", index_v1.string(), "--dest", dest.string(), "--allow-unsigned", "--fetch-packages"}, root_) .exit_code, 0); ASSERT_EQ(read_installed_version(dest, "com.vde.test.upgrade_me"), "1.0.0"); const fs::path packages_v2 = root_ / "v2" / "packages"; fs::create_directories(packages_v2); make_plugin_package(packages_v2, "com.vde.test.upgrade_me", "1.1.0", "", "v2"); const fs::path index_v2 = root_ / "v2" / "index.json"; ASSERT_EQ(build_index(packages_v2, index_v2, root_), 0); ToolResult result = run_tool( {VDE_PLUGIN_TOOL_EXE, "--upgrade", index_v2.string(), "--id", "com.vde.test.upgrade_me", "--dest", dest.string(), "--allow-unsigned", "--fetch-packages"}, root_); EXPECT_EQ(result.exit_code, 0) << result.output; EXPECT_EQ(read_installed_version(dest, "com.vde.test.upgrade_me"), "1.1.0"); EXPECT_FALSE(contains_dir_with_suffix(dest, ".vde-bak-")); } TEST_F(PluginToolRemoteInstallTest, UpgradeFailureKeepsOldVersion) { const fs::path packages_v1 = root_ / "v1" / "packages"; fs::create_directories(packages_v1); make_plugin_package(packages_v1, "com.vde.test.rollback_me", "1.0.0", "", "v1"); const fs::path index_v1 = root_ / "v1" / "index.json"; ASSERT_EQ(build_index(packages_v1, index_v1, root_), 0); const fs::path dest = root_ / "install"; ASSERT_EQ(run_tool({VDE_PLUGIN_TOOL_EXE, "--install", index_v1.string(), "--dest", dest.string(), "--allow-unsigned", "--fetch-packages"}, root_) .exit_code, 0); const fs::path packages_v2 = root_ / "v2" / "packages"; fs::create_directories(packages_v2); make_plugin_package(packages_v2, "com.vde.test.rollback_me", "1.1.0", "", "v2"); const fs::path index_v2 = root_ / "v2" / "index.json"; ASSERT_EQ(build_index(packages_v2, index_v2, root_), 0); // 篡改索引中的 SHA-256(模拟下载内容损坏/被替换) std::string index_text = read_text(index_v2); const std::string marker = "\"sha256\": \""; const std::size_t pos = index_text.find(marker); ASSERT_NE(pos, std::string::npos); const std::size_t value_start = pos + marker.size(); const std::size_t value_end = index_text.find('"', value_start); ASSERT_NE(value_end, std::string::npos); index_text.replace(value_start, value_end - value_start, std::string(64, '0')); ASSERT_TRUE(write_text(index_v2, index_text)); ToolResult result = run_tool( {VDE_PLUGIN_TOOL_EXE, "--upgrade", index_v2.string(), "--id", "com.vde.test.rollback_me", "--dest", dest.string(), "--allow-unsigned", "--fetch-packages"}, root_); EXPECT_EQ(result.exit_code, 3) << result.output; EXPECT_EQ(read_installed_version(dest, "com.vde.test.rollback_me"), "1.0.0"); EXPECT_FALSE(contains_dir_with_suffix(dest, ".vde-bak-")); } TEST_F(PluginToolRemoteInstallTest, HashMismatchRejectedAndNoPartialInstall) { const fs::path packages = root_ / "packages"; fs::create_directories(packages); make_plugin_package(packages, "com.vde.test.bad_hash", "1.0.0", "", "original"); const fs::path index = root_ / "index.json"; ASSERT_EQ(build_index(packages, index, root_), 0); write_text(packages / "com.vde.test.bad_hash" / "data.txt", "tampered"); const fs::path dest = root_ / "install"; const ToolResult result = run_tool( {VDE_PLUGIN_TOOL_EXE, "--install", index.string(), "--dest", dest.string(), "--allow-unsigned", "--fetch-packages"}, root_); EXPECT_EQ(result.exit_code, 3) << result.output; EXPECT_NE(result.output.find("SHA-256 mismatch"), std::string::npos) << result.output; EXPECT_FALSE(fs::exists(dest / "com.vde.test.bad_hash")); } TEST_F(PluginToolRemoteInstallTest, RemoteIndexRequiresFetchPackages) { const fs::path packages = root_ / "packages"; fs::create_directories(packages); make_plugin_package(packages, "com.vde.test.remote_missing_flag", "1.0.0", "", "r"); const fs::path index = root_ / "index.json"; ASSERT_EQ(build_index(packages, index, root_), 0); const ToolResult result = run_tool( {VDE_PLUGIN_TOOL_EXE, "--install", "http://127.0.0.1:1/index.json", "--dest", (root_ / "install").string(), "--allow-unsigned"}, root_); EXPECT_EQ(result.exit_code, 3) << result.output; EXPECT_NE(result.output.find("--fetch-packages"), std::string::npos) << result.output; } TEST_F(PluginToolRemoteInstallTest, HttpIndexAndArtifactsInstall) { #if defined(_WIN32) const fs::path packages = root_ / "packages"; fs::create_directories(packages); make_plugin_package(packages, "com.vde.test.http_dep", "1.0.0", "", "http-dep"); make_plugin_package(packages, "com.vde.test.http_plugin", "1.0.0", "[{\"id\": \"com.vde.test.http_dep\", \"version_range\": \"~1.0.0\"}]", "http-plugin"); const fs::path index = root_ / "index.json"; ASSERT_EQ(build_index(packages, index, root_), 0); HttpFileServer server(root_); ASSERT_TRUE(server.start()); const std::string url = server.url("index.json"); const fs::path dest = root_ / "install"; const ToolResult result = run_tool( {VDE_PLUGIN_TOOL_EXE, "--install", url, "--dest", dest.string(), "--id", "com.vde.test.http_plugin", "--allow-unsigned", "--fetch-packages"}, root_); server.stop(); ASSERT_EQ(result.exit_code, 0) << result.output; EXPECT_TRUE(fs::is_regular_file(dest / "com.vde.test.http_plugin" / "vde-plugin.json")) << result.output; EXPECT_TRUE(fs::is_regular_file(dest / "com.vde.test.http_dep" / "vde-plugin.json")); #else GTEST_SKIP() << "local HTTP server test is Windows-only"; #endif } } // namespace