100 lines
3.4 KiB
C++
100 lines
3.4 KiB
C++
#include <catch2/catch_test_macros.hpp>
|
|
|
|
#include <yams/integrity/verifier.h>
|
|
|
|
#include <chrono>
|
|
|
|
using namespace yams::integrity;
|
|
|
|
namespace {
|
|
|
|
struct MonitorFixture {
|
|
VerificationMonitor monitor;
|
|
|
|
VerificationResult makeResult(bool success, const std::string& hash = "test-hash") {
|
|
VerificationResult result;
|
|
result.blockHash = hash;
|
|
result.status = success ? VerificationStatus::Passed : VerificationStatus::Failed;
|
|
result.timestamp = std::chrono::system_clock::now();
|
|
result.blockSize = 1024;
|
|
return result;
|
|
}
|
|
};
|
|
|
|
} // namespace
|
|
|
|
TEST_CASE_METHOD(MonitorFixture, "VerificationMonitor metric tracking", "[integrity][monitor]") {
|
|
SECTION("initial metrics are zero") {
|
|
auto metrics = monitor.getCurrentMetrics();
|
|
CHECK(metrics.totalVerifications == 0u);
|
|
CHECK(metrics.totalErrors == 0u);
|
|
}
|
|
|
|
SECTION("records successful verifications") {
|
|
monitor.recordVerification(makeResult(true));
|
|
monitor.recordVerification(makeResult(true));
|
|
monitor.recordVerification(makeResult(true));
|
|
|
|
auto metrics = monitor.getCurrentMetrics();
|
|
CHECK(metrics.totalVerifications == 3u);
|
|
CHECK(metrics.totalErrors == 0u);
|
|
}
|
|
|
|
SECTION("records failed verifications") {
|
|
monitor.recordVerification(makeResult(false));
|
|
monitor.recordVerification(makeResult(false));
|
|
|
|
auto metrics = monitor.getCurrentMetrics();
|
|
CHECK(metrics.totalVerifications == 2u);
|
|
CHECK(metrics.totalErrors == 2u);
|
|
}
|
|
|
|
SECTION("mixed success and failure tracking") {
|
|
monitor.recordVerification(makeResult(true));
|
|
monitor.recordVerification(makeResult(false));
|
|
monitor.recordVerification(makeResult(true));
|
|
monitor.recordVerification(makeResult(false));
|
|
monitor.recordVerification(makeResult(true));
|
|
|
|
auto metrics = monitor.getCurrentMetrics();
|
|
CHECK(metrics.totalVerifications == 5u);
|
|
CHECK(metrics.totalErrors == 2u);
|
|
}
|
|
}
|
|
|
|
TEST_CASE_METHOD(MonitorFixture, "VerificationMonitor repair tracking", "[integrity][monitor]") {
|
|
SECTION("tracks repair attempts") {
|
|
monitor.recordRepairAttempt("hash_1", true);
|
|
monitor.recordRepairAttempt("hash_2", false);
|
|
monitor.recordRepairAttempt("hash_3", true);
|
|
|
|
auto metrics = monitor.getCurrentMetrics();
|
|
CHECK(metrics.repairAttempts == 3u);
|
|
CHECK(metrics.successfulRepairs == 2u);
|
|
}
|
|
}
|
|
|
|
TEST_CASE_METHOD(MonitorFixture, "VerificationMonitor alert system", "[integrity][monitor]") {
|
|
SECTION("alert threshold can be configured") {
|
|
monitor.setErrorRateThreshold(0.1); // Very low threshold
|
|
// Add failures
|
|
for (int i = 0; i < 10; ++i) {
|
|
monitor.recordVerification(makeResult(false));
|
|
}
|
|
// Alert behavior may depend on internal rate calculation
|
|
// Just verify the method works without crashing
|
|
[[maybe_unused]] bool alert = monitor.shouldAlert();
|
|
CHECK(true);
|
|
}
|
|
|
|
SECTION("no alert when error rate is below threshold") {
|
|
monitor.setErrorRateThreshold(0.9);
|
|
// Mostly successful
|
|
for (int i = 0; i < 9; ++i) {
|
|
monitor.recordVerification(makeResult(true));
|
|
}
|
|
monitor.recordVerification(makeResult(false));
|
|
// With 10% error rate and 90% threshold, shouldn't alert
|
|
CHECK_FALSE(monitor.shouldAlert());
|
|
}
|
|
}
|