-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_plugin_manager_comprehensive.cpp
More file actions
336 lines (278 loc) · 13.5 KB
/
test_plugin_manager_comprehensive.cpp
File metadata and controls
336 lines (278 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
╔═════════════════════════════════════════════════════════════════════╗
║ ThemisDB - Hybrid Database System ║
╠═════════════════════════════════════════════════════════════════════╣
File: test_plugin_manager_comprehensive.cpp ║
Version: 0.0.36 ║
Last Modified: 2026-03-30 04:31:11 ║
Author: unknown ║
╠═════════════════════════════════════════════════════════════════════╣
Quality Metrics: ║
• Maturity Level: 🟢 PRODUCTION-READY ║
• Quality Score: 100.0/100 ║
• Total Lines: 316 ║
• Open Issues: TODOs: 0, Stubs: 0 ║
╠═════════════════════════════════════════════════════════════════════╣
Revision History: ║
• b5d343902 2026-03-09 feat(plugins): implement test_plugin_manager stubs, updat... ║
• 2a1fb0423 2026-03-03 Merge branch 'develop' into copilot/audit-src-module-docu... ║
╠═════════════════════════════════════════════════════════════════════╣
Status: ✅ Production Ready ║
╚═════════════════════════════════════════════════════════════════════╝
*/
/**
* @file test_plugin_manager_comprehensive.cpp
* @brief Comprehensive integration-style tests for PluginManager
*
* Tests cover multi-plugin scenarios, dependency edge cases, type-indexed
* lookups, reload listener call counts, metrics access, and attachHealthMonitor.
*/
#include <gtest/gtest.h>
#include "plugins/plugin_manager.h"
#include "plugins/plugin_interface.h"
#include "plugins/plugin_health_monitor.h"
#include "plugins/self_healing_plugin.h"
#include "acceleration/plugin_security.h"
#include "utils/error_registry.h"
#include <nlohmann/json.hpp>
#include <filesystem>
#include <fstream>
#include <string>
#include <vector>
#include <atomic>
#include <chrono>
#include <thread>
using namespace themis::plugins;
namespace fs = std::filesystem;
// ============================================================================
// Test fixture
// ============================================================================
class PluginManagerComprehensiveTest : public ::testing::Test {
protected:
std::string test_dir_;
PluginManager* manager_;
void SetUp() override {
test_dir_ = (fs::temp_directory_path() / "themis_test_pm_comp").string();
if (fs::exists(test_dir_)) {
fs::remove_all(test_dir_);
}
fs::create_directories(test_dir_);
manager_ = &PluginManager::instance();
manager_->disableHotPlug();
manager_->unloadAllPlugins();
manager_->clearReloadListeners();
}
void TearDown() override {
manager_->disableHotPlug();
manager_->unloadAllPlugins();
manager_->clearReloadListeners();
if (fs::exists(test_dir_)) {
// Hot-plug monitor shutdown can release handles asynchronously on Windows.
std::error_code ec;
fs::remove_all(test_dir_, ec);
if (ec) {
std::this_thread::sleep_for(std::chrono::milliseconds(50));
ec.clear();
fs::remove_all(test_dir_, ec);
}
}
}
void createManifest(const std::string& name,
const std::string& type = "custom",
bool auto_load = false,
const std::vector<std::string>& deps = {},
const std::string& version = "1.0.0") {
std::string plugin_dir = test_dir_ + "/" + name;
fs::create_directories(plugin_dir);
nlohmann::json manifest = {
{"name", name},
{"version", version},
{"type", type},
{"description", "Comprehensive test plugin: " + name},
{"binary", {
{"windows", name + ".dll"},
{"linux", name + ".so"},
{"macos", name + ".dylib"}
}},
{"capabilities", {
{"thread_safe", true},
{"streaming", false},
{"batching", true},
{"gpu_accelerated", false}
}},
{"auto_load", auto_load},
{"load_priority", 100}
};
if (!deps.empty()) {
manifest["dependencies"] = deps;
}
std::string manifest_path = plugin_dir + "/plugin.json";
std::ofstream file(manifest_path);
file << manifest.dump(2);
file.close();
// Release builds require manifest signature files; write expected hash.
themis::acceleration::PluginSecurityPolicy policy;
themis::acceleration::PluginSecurityVerifier verifier(policy);
std::string manifest_hash = verifier.calculateFileHash(manifest_path);
std::ofstream sig_file(manifest_path + ".sig");
sig_file << manifest_hash;
}
};
// ============================================================================
// Multi-plugin scanning
// ============================================================================
TEST_F(PluginManagerComprehensiveTest, ScanDirectoryWithMixedTypesDiscoversAll) {
createManifest("pmc_blob_001", "blob_storage");
createManifest("pmc_custom_001", "custom");
createManifest("pmc_importer_001","importer");
auto result = manager_->scanPluginDirectory(test_dir_);
EXPECT_TRUE(result.has_value());
EXPECT_GE(*result, 3u);
}
TEST_F(PluginManagerComprehensiveTest, ListPluginsAfterScanIncludesDiscoveredNames) {
createManifest("pmc_list_001");
createManifest("pmc_list_002");
manager_->scanPluginDirectory(test_dir_);
auto all = manager_->listPlugins();
// Ensure both discovered plugins are present in the registry
bool found_001 = false, found_002 = false;
for (const auto& m : all) {
if (m.name == "pmc_list_001") found_001 = true;
if (m.name == "pmc_list_002") found_002 = true;
}
EXPECT_TRUE(found_001);
EXPECT_TRUE(found_002);
}
// ============================================================================
// Type-based lookups
// ============================================================================
TEST_F(PluginManagerComprehensiveTest, GetPluginsByTypeReturnsEmptyForUnloadedPlugins) {
createManifest("pmc_type_blob_001", "blob_storage");
manager_->scanPluginDirectory(test_dir_);
// Plugin is discovered but not loaded (no binary) — must return empty
auto result = manager_->getPluginsByType(PluginType::BLOB_STORAGE);
EXPECT_TRUE(result.empty());
}
TEST_F(PluginManagerComprehensiveTest, GetPluginsByTypeReturnsEmptyForUnusedType) {
createManifest("pmc_type_custom_002", "custom");
manager_->scanPluginDirectory(test_dir_);
// EMBEDDING type has no registered plugin
auto result = manager_->getPluginsByType(PluginType::EMBEDDING);
EXPECT_TRUE(result.empty());
}
// ============================================================================
// autoLoadPlugins — no auto-load plugins
// ============================================================================
TEST_F(PluginManagerComprehensiveTest, AutoLoadSkipsManualPlugins) {
createManifest("pmc_manual_001", "custom", false);
createManifest("pmc_manual_002", "custom", false);
manager_->scanPluginDirectory(test_dir_);
auto result = manager_->autoLoadPlugins();
EXPECT_TRUE(result.has_value());
EXPECT_EQ(*result, 0u); // None should have been loaded
}
// ============================================================================
// Dependency edge cases
// ============================================================================
TEST_F(PluginManagerComprehensiveTest, LoadPluginWithCircularDependencyFails) {
// A → B → A (circular)
createManifest("pmc_circ_a_001", "custom", false, {"pmc_circ_b_001"});
createManifest("pmc_circ_b_001", "custom", false, {"pmc_circ_a_001"});
manager_->scanPluginDirectory(test_dir_);
auto result = manager_->loadPlugin("pmc_circ_a_001");
EXPECT_FALSE(result.has_value());
EXPECT_EQ(result.error().code(),
themis::errors::ErrorCode::ERR_PLUGIN_CIRCULAR_DEPENDENCY);
}
TEST_F(PluginManagerComprehensiveTest, LoadPluginWithUnregisteredDependencyFails) {
createManifest("pmc_dep_child_001", "custom", false, {"pmc_dep_parent_missing"});
manager_->scanPluginDirectory(test_dir_);
auto result = manager_->loadPlugin("pmc_dep_child_001");
EXPECT_FALSE(result.has_value());
EXPECT_EQ(result.error().code(),
themis::errors::ErrorCode::ERR_PLUGIN_MISSING_DEPENDENCY);
}
// ============================================================================
// getManifest — version field
// ============================================================================
TEST_F(PluginManagerComprehensiveTest, GetManifestReturnsCorrectVersion) {
createManifest("pmc_ver_test_001", "custom", false, {}, "2.3.1");
manager_->scanPluginDirectory(test_dir_);
auto result = manager_->getManifest("pmc_ver_test_001");
EXPECT_TRUE(result.has_value());
if (result.has_value()) {
EXPECT_EQ(result->version, "2.3.1");
}
}
// ============================================================================
// Reload listeners
// ============================================================================
TEST_F(PluginManagerComprehensiveTest, MultipleReloadListenersCanBeRegistered) {
std::atomic<int> call_count{0};
manager_->registerReloadListener(
[&call_count](const std::string&, PluginReloadPhase) { call_count++; });
manager_->registerReloadListener(
[&call_count](const std::string&, PluginReloadPhase) { call_count++; });
// Listeners exist but no reload triggered yet
EXPECT_EQ(call_count.load(), 0);
// Clearing must not invoke listeners
manager_->clearReloadListeners();
EXPECT_EQ(call_count.load(), 0);
}
TEST_F(PluginManagerComprehensiveTest, ClearListenersIsIdempotent) {
manager_->clearReloadListeners();
manager_->clearReloadListeners(); // Should not crash
EXPECT_TRUE(true);
}
// ============================================================================
// Metrics access
// ============================================================================
TEST_F(PluginManagerComprehensiveTest, GetMetricsReturnsSameReference) {
const PluginMetrics& m1 = manager_->getMetrics();
const PluginMetrics& m2 = manager_->getMetrics();
EXPECT_EQ(&m1, &m2);
}
TEST_F(PluginManagerComprehensiveTest, GetMutableMetricsReturnsSameAsConst) {
PluginMetrics& mut = manager_->getMetricsMutable();
const PluginMetrics& cst = manager_->getMetrics();
// Both should point to the same underlying object
EXPECT_EQ(&mut, &cst);
}
// ============================================================================
// attachHealthMonitor
// ============================================================================
TEST_F(PluginManagerComprehensiveTest, AttachNullHealthMonitorDoesNotCrash) {
manager_->attachHealthMonitor(nullptr);
EXPECT_TRUE(true); // No exception
}
TEST_F(PluginManagerComprehensiveTest, AttachAndDetachHealthMonitor) {
PluginHealthMonitor monitor;
manager_->attachHealthMonitor(&monitor);
manager_->attachHealthMonitor(nullptr); // Detach
EXPECT_TRUE(true);
}
// ============================================================================
// Hot-plug with scan
// ============================================================================
TEST_F(PluginManagerComprehensiveTest, EnableHotPlugThenScanDiscoversPlugins) {
createManifest("pmc_hotplug_scan_001");
bool ok = manager_->enableHotPlug(test_dir_);
EXPECT_TRUE(ok);
auto result = manager_->scanPluginDirectory(test_dir_);
EXPECT_TRUE(result.has_value());
EXPECT_GE(*result, 1u);
manager_->disableHotPlug();
}
// ============================================================================
// negotiateCapabilities — with requirements
// ============================================================================
TEST_F(PluginManagerComprehensiveTest, NegotiateCapabilitiesRequirementsForUnloadedPlugin) {
createManifest("pmc_neg_disc_001");
manager_->scanPluginDirectory(test_dir_);
PluginCapabilityRequirement req;
req.capability_name = "thread_safe";
PluginNegotiationResult result =
manager_->negotiateCapabilities("pmc_neg_disc_001", {req});
// Plugin is not loaded — negotiation must fail
EXPECT_FALSE(result.success);
}