// tests/plugin/test_plugin_crl.cpp // P4-08:签名 CRL(key 吊销列表)分发与验证。 // 覆盖:verify-index/install/upgrade 合并 CRL 吊销、不同 key 放行、过期/未生效、 // 篡改/错误公钥验签失败、格式错误、本地 HTTP 获取(Windows 内建 server;POSIX skip)。 #include #include #include #include #include #include #include #include #include #include #if defined(_WIN32) #include #include #include #else #include #include #include #endif namespace fs = std::filesystem; #ifndef VDE_PLUGIN_TOOL_EXE #define VDE_PLUGIN_TOOL_EXE "" #endif namespace { std::string read_text(const fs::path& path) { std::ifstream in(path, 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, 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) { return "\"" + value + "\""; } #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_crl_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) { 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 + "\"]\n"; text += "}\n"; return text; } fs::path make_plugin_package(const fs::path& root, const std::string& id, const std::string& version) { const fs::path dir = root / id; fs::create_directories(dir); write_text(dir / "vde-plugin.json", make_plugin_manifest(id, version)); write_text(dir / "plugin.dll", "binary:" + id + ":" + version); return dir; } std::string make_crl_payload(const std::string& issued_at, const std::string& expires_at, const std::vector& revoked_key_ids) { std::string text = "{\n"; text += " \"version\": 1,\n"; text += " \"issued_at\": \"" + issued_at + "\",\n"; text += " \"expires_at\": \"" + expires_at + "\",\n"; text += " \"revoked_key_ids\": ["; for (std::size_t i = 0; i < revoked_key_ids.size(); ++i) { if (i) text += ", "; text += "\"" + revoked_key_ids[i] + "\""; } text += "]\n}\n"; return text; } std::string write_signed_crl(const fs::path& root, const std::string& name, const std::string& issued_at, const std::string& expires_at, const std::vector& revoked_key_ids, const std::string& seed_path) { const fs::path payload = root / (name + ".payload.json"); const fs::path crl = root / (name + ".json"); EXPECT_TRUE(write_text(payload, make_crl_payload(issued_at, expires_at, revoked_key_ids))); const ToolResult result = run_tool({VDE_PLUGIN_TOOL_EXE, "--sign-crl", payload.string(), "-o", crl.string(), "--sign", seed_path}, root); EXPECT_EQ(result.exit_code, 0) << result.output; return crl.string(); } #if defined(_WIN32) // 极简本地 HTTP server(Windows;POSIX 下 CRL HTTP 用例 skip,本地文件用例已覆盖)。 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 PluginToolCrlTest : public ::testing::Test { protected: void SetUp() override { root_ = fs::temp_directory_path() / ("vde_crl_test_" + unique_suffix()); fs::create_directories(root_); packages_ = root_ / "packages"; fs::create_directories(packages_); make_plugin_package(packages_, "com.vde.test.crl_plugin", "1.0.0"); keys_a_ = root_ / "keysA"; keys_b_ = root_ / "keysB"; ASSERT_EQ(run_tool({VDE_PLUGIN_TOOL_EXE, "--gen-keypair", "--out-dir", keys_a_.string()}, root_) .exit_code, 0); ASSERT_EQ(run_tool({VDE_PLUGIN_TOOL_EXE, "--gen-keypair", "--out-dir", keys_b_.string()}, root_) .exit_code, 0); seed_a_ = (keys_a_ / "plugin_signing.key").string(); pub_a_ = (keys_a_ / "plugin_signing.pub").string(); pub_b_ = (keys_b_ / "plugin_signing.pub").string(); index_ = root_ / "index.json"; const ToolResult build = run_tool( {VDE_PLUGIN_TOOL_EXE, "--build-index", packages_.string(), "-o", index_.string(), "--sign", seed_a_, "--key-id", "key-A", "--publisher", "CRL Test"}, root_); ASSERT_EQ(build.exit_code, 0) << build.output; } void TearDown() override { std::error_code ec; fs::remove_all(root_, ec); } fs::path root_; fs::path packages_; fs::path keys_a_; fs::path keys_b_; fs::path index_; std::string seed_a_; std::string pub_a_; std::string pub_b_; }; TEST_F(PluginToolCrlTest, VerifyIndexRejectsCrlRevokedKey) { const std::string crl = write_signed_crl(root_, "crlA", "2026-01-01T00:00:00Z", "2099-01-01T00:00:00Z", {"key-A"}, seed_a_); const ToolResult result = run_tool( {VDE_PLUGIN_TOOL_EXE, "--verify-index", index_.string(), "--pubkey", pub_a_, "--crl", crl, "--crl-pubkey", pub_a_}, root_); EXPECT_EQ(result.exit_code, 3) << result.output; EXPECT_NE(result.output.find("revoked"), std::string::npos) << result.output; } TEST_F(PluginToolCrlTest, VerifyIndexAllowsDifferentKey) { const std::string crl = write_signed_crl(root_, "crlB", "2026-01-01T00:00:00Z", "2099-01-01T00:00:00Z", {"key-B"}, seed_a_); const ToolResult result = run_tool( {VDE_PLUGIN_TOOL_EXE, "--verify-index", index_.string(), "--pubkey", pub_a_, "--crl", crl, "--crl-pubkey", pub_a_}, root_); EXPECT_EQ(result.exit_code, 0) << result.output; EXPECT_NE(result.output.find("CRL merged 1"), std::string::npos) << result.output; } TEST_F(PluginToolCrlTest, ExpiredCrlRejected) { const std::string crl = write_signed_crl(root_, "crlExpired", "2020-01-01T00:00:00Z", "2021-01-01T00:00:00Z", {"key-A"}, seed_a_); const ToolResult result = run_tool( {VDE_PLUGIN_TOOL_EXE, "--verify-index", index_.string(), "--pubkey", pub_a_, "--crl", crl, "--crl-pubkey", pub_a_}, root_); EXPECT_EQ(result.exit_code, 3) << result.output; EXPECT_NE(result.output.find("expired"), std::string::npos) << result.output; } TEST_F(PluginToolCrlTest, NotYetValidCrlRejected) { const std::string crl = write_signed_crl(root_, "crlFuture", "2099-01-01T00:00:00Z", "2100-01-01T00:00:00Z", {"key-A"}, seed_a_); const ToolResult result = run_tool( {VDE_PLUGIN_TOOL_EXE, "--verify-index", index_.string(), "--pubkey", pub_a_, "--crl", crl, "--crl-pubkey", pub_a_}, root_); EXPECT_EQ(result.exit_code, 3) << result.output; EXPECT_NE(result.output.find("not yet valid"), std::string::npos) << result.output; } TEST_F(PluginToolCrlTest, TamperedCrlSignatureRejected) { const std::string crl = write_signed_crl(root_, "crlTamper", "2026-01-01T00:00:00Z", "2099-01-01T00:00:00Z", {"key-A"}, seed_a_); std::string text = read_text(crl); const std::size_t pos = text.find("\"key-A\""); ASSERT_NE(pos, std::string::npos); text.replace(pos, 7, "\"key-C\""); ASSERT_TRUE(write_text(crl, text)); const ToolResult result = run_tool( {VDE_PLUGIN_TOOL_EXE, "--verify-index", index_.string(), "--pubkey", pub_a_, "--crl", crl, "--crl-pubkey", pub_a_}, root_); EXPECT_EQ(result.exit_code, 3) << result.output; EXPECT_NE(result.output.find("signature"), std::string::npos) << result.output; } TEST_F(PluginToolCrlTest, WrongCrlPublicKeyRejected) { const std::string crl = write_signed_crl(root_, "crlWrongKey", "2026-01-01T00:00:00Z", "2099-01-01T00:00:00Z", {"key-A"}, seed_a_); const ToolResult result = run_tool( {VDE_PLUGIN_TOOL_EXE, "--verify-index", index_.string(), "--pubkey", pub_a_, "--crl", crl, "--crl-pubkey", pub_b_}, root_); EXPECT_EQ(result.exit_code, 3) << result.output; EXPECT_NE(result.output.find("signature"), std::string::npos) << result.output; } TEST_F(PluginToolCrlTest, MissingOrMalformedCrlRejected) { ToolResult result = run_tool( {VDE_PLUGIN_TOOL_EXE, "--verify-index", index_.string(), "--pubkey", pub_a_, "--crl", (root_ / "absent.json").string(), "--crl-pubkey", pub_a_}, root_); EXPECT_EQ(result.exit_code, 4) << result.output; const fs::path bad = root_ / "bad-crl.json"; ASSERT_TRUE(write_text(bad, "{not-json\n")); result = run_tool({VDE_PLUGIN_TOOL_EXE, "--verify-index", index_.string(), "--pubkey", pub_a_, "--crl", bad.string(), "--crl-pubkey", pub_a_}, root_); EXPECT_EQ(result.exit_code, 3) << result.output; EXPECT_NE(result.output.find("invalid CRL"), std::string::npos) << result.output; } TEST_F(PluginToolCrlTest, InstallRejectsCrlRevokedKey) { const std::string crl = write_signed_crl(root_, "crlInstall", "2026-01-01T00:00:00Z", "2099-01-01T00:00:00Z", {"key-A"}, seed_a_); const fs::path dest = root_ / "install"; const ToolResult result = run_tool( {VDE_PLUGIN_TOOL_EXE, "--install", index_.string(), "--dest", dest.string(), "--pubkey", pub_a_, "--crl", crl, "--crl-pubkey", pub_a_}, root_); EXPECT_EQ(result.exit_code, 3) << result.output; EXPECT_NE(result.output.find("revoked"), std::string::npos) << result.output; EXPECT_FALSE(fs::exists(dest / "com.vde.test.crl_plugin")); } TEST_F(PluginToolCrlTest, UpgradeRejectsCrlRevokedKey) { const fs::path dest = root_ / "install"; const ToolResult install = run_tool( {VDE_PLUGIN_TOOL_EXE, "--install", index_.string(), "--dest", dest.string(), "--pubkey", pub_a_}, root_); ASSERT_EQ(install.exit_code, 0) << install.output; const std::string crl = write_signed_crl(root_, "crlUpgrade", "2026-01-01T00:00:00Z", "2099-01-01T00:00:00Z", {"key-A"}, seed_a_); const ToolResult result = run_tool( {VDE_PLUGIN_TOOL_EXE, "--upgrade", index_.string(), "--id", "com.vde.test.crl_plugin", "--dest", dest.string(), "--pubkey", pub_a_, "--crl", crl, "--crl-pubkey", pub_a_}, root_); EXPECT_EQ(result.exit_code, 3) << result.output; EXPECT_NE(result.output.find("revoked"), std::string::npos) << result.output; } #if defined(_WIN32) TEST_F(PluginToolCrlTest, LocalHttpCrlFetchedAndEnforced) { const std::string crl = write_signed_crl(root_, "crlHttp", "2026-01-01T00:00:00Z", "2099-01-01T00:00:00Z", {"key-A"}, seed_a_); HttpFileServer server(root_); ASSERT_TRUE(server.start()); const ToolResult result = run_tool( {VDE_PLUGIN_TOOL_EXE, "--verify-index", index_.string(), "--pubkey", pub_a_, "--crl", server.url("crlHttp.json"), "--crl-pubkey", pub_a_}, root_); EXPECT_EQ(result.exit_code, 3) << result.output; EXPECT_NE(result.output.find("revoked"), std::string::npos) << result.output; } #else TEST_F(PluginToolCrlTest, LocalHttpCrlFetchedAndEnforced) { GTEST_SKIP() << "local HTTP server fixture is Windows-only; file:// CRL path is covered " "by the file-based cases (read_location_bytes)"; } #endif } // namespace