From 48e5e223afe98bf0d5011f2e5a62a871db87485c Mon Sep 17 00:00:00 2001 From: "Aleksei (Faumaray) Selikhov" Date: Fri, 4 Dec 2026 02:21:23 +0300 Subject: [PATCH] Testing: add bounded fault-file plan port (M-TESTING-06) --- include/Void/Testing/FaultFile.hpp | 89 +++++++++ src/Testing/FaultFile.cpp | 247 +++++++++++++++++++++++++ tests/standalone/Testing/FaultFile.cpp | 109 +++++++++++ 3 files changed, 445 insertions(+) create mode 100644 include/Void/Testing/FaultFile.hpp create mode 100644 src/Testing/FaultFile.cpp create mode 100644 tests/standalone/Testing/FaultFile.cpp diff --git a/include/Void/Testing/FaultFile.hpp b/include/Void/Testing/FaultFile.hpp new file mode 100644 index 0000000..073d542 --- /dev/null +++ b/include/Void/Testing/FaultFile.hpp @@ -0,0 +1,89 @@ +#pragma once + +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace Void::Testing +{ + struct FaultFileLimits { + std::size_t max_bytes{64ULL * 1024ULL}; + std::size_t max_entries{256}; + std::size_t max_boundary_bytes{64}; + }; + + enum class FaultAction : std::uint8_t { + /// Fail the operation at this boundary. + Error, + /// Write at most `partial_bytes` then fail. + PartialWrite, + /// Block at this boundary until released. + Pause + }; + + struct FaultSpec { + std::string boundary{}; + /// 1-based hit among matching boundary occurrences. + std::uint64_t hit_index{1}; + FaultAction action{FaultAction::Error}; + std::optional partial_bytes{}; + std::string error_code{}; + }; + + /// Parse a fault file. One `boundary hit action [partial=N] [code=NAME]` + /// per line; `#` starts a comment and blank lines are ignored. + [[nodiscard]] Void::Result::Result> + parse_fault_file(std::string_view text, FaultFileLimits limits = {}); + + struct FaultHit { + bool triggered{false}; + FaultAction action{FaultAction::Error}; + std::optional partial_bytes{}; + std::string error_code{}; + }; + + struct FaultTotals { + std::size_t configured{0}; + std::size_t fired{0}; + std::size_t remaining{0}; + }; + + /// Applies a parsed fault list at named boundaries. + /// + /// Each spec fires only on its `hit_index`-th occurrence of its boundary. + /// Boundary names are bounded so a hostile or buggy caller cannot grow + /// memory without limit. + class FaultPlan + { + public: + + explicit FaultPlan(std::vector specs, + std::size_t max_boundaries = 1024); + + /// Count one occurrence of `boundary` and report an injected fault + /// when one is due. + [[nodiscard]] FaultHit check(std::string_view boundary); + + /// Configured, fired and remaining counts (they reconcile: + /// configured == fired + remaining). + [[nodiscard]] FaultTotals totals() const noexcept; + + [[nodiscard]] std::size_t occurrences(std::string_view boundary) const; + + private: + + struct Entry { + FaultSpec spec{}; + bool fired{false}; + }; + + std::map, std::less<>> m_entries; + std::map> m_occurrences; + }; +} // namespace Void::Testing diff --git a/src/Testing/FaultFile.cpp b/src/Testing/FaultFile.cpp new file mode 100644 index 0000000..9731a51 --- /dev/null +++ b/src/Testing/FaultFile.cpp @@ -0,0 +1,247 @@ +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace Void::Testing +{ + namespace + { + [[nodiscard]] Void::Result::Error fault_error(std::string message) + { + return Void::Result::Error{ + Void::Result::ErrorKind::invalid_argument, std::move(message)}; + } + + [[nodiscard]] std::string_view trim(std::string_view text) noexcept + { + while (!text.empty() && + (text.front() == ' ' || text.front() == '\t' || + text.front() == '\r')) { + text.remove_prefix(1); + } + while (!text.empty() && + (text.back() == ' ' || text.back() == '\t' || + text.back() == '\r')) { + text.remove_suffix(1); + } + return text; + } + + [[nodiscard]] std::vector + split_fields(std::string_view line) noexcept + { + std::vector fields; + std::size_t pos = 0; + while (pos < line.size()) { + while (pos < line.size() && + (line[pos] == ' ' || line[pos] == '\t')) { + ++pos; + } + const std::size_t start = pos; + while (pos < line.size() && line[pos] != ' ' && line[pos] != '\t') { + ++pos; + } + if (pos > start) { + fields.push_back(line.substr(start, pos - start)); + } + } + return fields; + } + + [[nodiscard]] bool safe_boundary(std::string_view name, + std::size_t max_bytes) noexcept + { + if (name.empty() || name.size() > max_bytes) { + return false; + } + for (const char ch : name) { + const bool ok = (ch >= 'a' && ch <= 'z') || + (ch >= 'A' && ch <= 'Z') || + (ch >= '0' && ch <= '9') || ch == '_' || ch == '-'; + if (!ok) { + return false; + } + } + return true; + } + } // namespace + + Void::Result::Result> + parse_fault_file(std::string_view text, FaultFileLimits limits) + { + if (limits.max_bytes == 0 || limits.max_entries == 0 || + limits.max_boundary_bytes == 0) { + return std::unexpected{ + fault_error("parse_fault_file: limits must be non-zero")}; + } + if (text.size() > limits.max_bytes) { + return std::unexpected{ + fault_error("parse_fault_file: file exceeds max_bytes")}; + } + + std::vector specs; + std::size_t line_number = 0; + std::size_t pos = 0; + while (pos <= text.size()) { + const auto newline = text.find('\n', pos); + const auto raw = text.substr( + pos, newline == std::string_view::npos ? std::string_view::npos + : newline - pos); + pos = newline == std::string_view::npos ? text.size() + 1 + : newline + 1; + ++line_number; + + auto line = trim(raw); + if (const auto comment = line.find('#'); + comment != std::string_view::npos) { + line = trim(line.substr(0, comment)); + } + if (line.empty()) { + continue; + } + + const auto fields = split_fields(line); + if (fields.size() < 3) { + return std::unexpected{fault_error(std::format( + "parse_fault_file:{}: expected 'boundary hit action'", + line_number))}; + } + if (!safe_boundary(fields[0], limits.max_boundary_bytes)) { + return std::unexpected{fault_error(std::format( + "parse_fault_file:{}: invalid boundary name", line_number))}; + } + + FaultSpec spec; + spec.boundary = std::string{fields[0]}; + const auto hit = std::from_chars( + fields[1].data(), fields[1].data() + fields[1].size(), + spec.hit_index); + if (hit.ec != std::errc{} || + hit.ptr != fields[1].data() + fields[1].size() || + spec.hit_index == 0) { + return std::unexpected{fault_error(std::format( + "parse_fault_file:{}: hit must be a positive integer", + line_number))}; + } + + const auto action = fields[2]; + if (action == "error") { + spec.action = FaultAction::Error; + } else if (action == "partial") { + spec.action = FaultAction::PartialWrite; + } else if (action == "pause") { + spec.action = FaultAction::Pause; + } else { + return std::unexpected{fault_error(std::format( + "parse_fault_file:{}: unknown action '{}'", line_number, + std::string{action}))}; + } + + for (std::size_t i = 3; i < fields.size(); ++i) { + const auto field = fields[i]; + if (field.starts_with("partial=")) { + const auto digits = field.substr(8); + std::size_t bytes = 0; + const auto parsed = std::from_chars( + digits.data(), digits.data() + digits.size(), bytes); + if (parsed.ec != std::errc{} || + parsed.ptr != digits.data() + digits.size()) { + return std::unexpected{fault_error(std::format( + "parse_fault_file:{}: invalid partial size", + line_number))}; + } + spec.partial_bytes = bytes; + } else if (field.starts_with("code=")) { + spec.error_code = std::string{field.substr(5)}; + } else { + return std::unexpected{fault_error(std::format( + "parse_fault_file:{}: unknown field '{}'", line_number, + std::string{field}))}; + } + } + if (spec.action == FaultAction::PartialWrite && + !spec.partial_bytes.has_value()) { + return std::unexpected{fault_error(std::format( + "parse_fault_file:{}: partial requires partial=N", + line_number))}; + } + + const bool duplicate = std::any_of( + specs.begin(), specs.end(), [&](const FaultSpec& other) { + return other.boundary == spec.boundary && + other.hit_index == spec.hit_index; + }); + if (duplicate) { + return std::unexpected{fault_error(std::format( + "parse_fault_file:{}: duplicate boundary/hit", line_number))}; + } + specs.push_back(std::move(spec)); + if (specs.size() > limits.max_entries) { + return std::unexpected{ + fault_error("parse_fault_file: too many entries")}; + } + } + return specs; + } + + FaultPlan::FaultPlan(std::vector specs, std::size_t max_boundaries) + { + for (auto& spec : specs) { + if (m_entries.size() >= max_boundaries && + !m_entries.contains(spec.boundary)) { + continue; // bounded: extra boundaries are ignored + } + m_entries[spec.boundary].push_back(Entry{std::move(spec), false}); + } + } + + FaultHit FaultPlan::check(std::string_view boundary) + { + const std::string name{boundary}; + const std::size_t occurrence = ++m_occurrences[name]; + + const auto found = m_entries.find(name); + if (found == m_entries.end()) { + return FaultHit{}; + } + for (auto& entry : found->second) { + if (entry.fired || entry.spec.hit_index != occurrence) { + continue; + } + entry.fired = true; + return FaultHit{.triggered = true, + .action = entry.spec.action, + .partial_bytes = entry.spec.partial_bytes, + .error_code = entry.spec.error_code}; + } + return FaultHit{}; + } + + FaultTotals FaultPlan::totals() const noexcept + { + FaultTotals totals; + for (const auto& [name, entries] : m_entries) { + (void)name; + for (const auto& entry : entries) { + ++totals.configured; + if (entry.fired) { + ++totals.fired; + } + } + } + totals.remaining = totals.configured - totals.fired; + return totals; + } + + std::size_t FaultPlan::occurrences(std::string_view boundary) const + { + const auto found = m_occurrences.find(boundary); + return found == m_occurrences.end() ? 0 : found->second; + } +} // namespace Void::Testing diff --git a/tests/standalone/Testing/FaultFile.cpp b/tests/standalone/Testing/FaultFile.cpp new file mode 100644 index 0000000..396184e --- /dev/null +++ b/tests/standalone/Testing/FaultFile.cpp @@ -0,0 +1,109 @@ +#include +#include +#include + +#include +#include +#include +#include + +namespace +{ + using namespace Void::Testing; +} // namespace + +Void::Testing::Test testing_fault_file_parses_valid_specs() +{ + constexpr std::string_view text = + "# storage fault plan\n" + "\n" + "sync_file 1 error code=EIO\n" + "append 2 partial partial=16\n" + "rename 1 pause # block once\n"; + + auto specs = parse_fault_file(text); + Void::Testing::require(specs.has_value()); + Void::Testing::expect_eq(specs->size(), std::size_t{3}); + + Void::Testing::expect_eq((*specs)[0].boundary, std::string{"sync_file"}); + Void::Testing::expect_eq((*specs)[0].hit_index, std::uint64_t{1}); + Void::Testing::expect((*specs)[0].action == FaultAction::Error); + Void::Testing::expect_eq((*specs)[0].error_code, std::string{"EIO"}); + + Void::Testing::expect((*specs)[1].action == FaultAction::PartialWrite); + Void::Testing::require((*specs)[1].partial_bytes.has_value()); + Void::Testing::expect_eq(*(*specs)[1].partial_bytes, std::size_t{16}); + + Void::Testing::expect((*specs)[2].action == FaultAction::Pause); +} + +Void::Testing::Test testing_fault_file_rejects_invalid_input() +{ + const auto rejects = [](std::string_view text, FaultFileLimits limits = {}) { + return !parse_fault_file(text, limits).has_value(); + }; + + Void::Testing::expect(rejects("sync_file 1")); // missing action + Void::Testing::expect(rejects("sync_file 0 error")); // hit 0 + Void::Testing::expect(rejects("sync_file x error")); // bad hit + Void::Testing::expect(rejects("sync_file 1 explode")); // unknown action + Void::Testing::expect(rejects("sync_file 1 partial")); // no size + Void::Testing::expect(rejects("sync_file 1 error partial=x")); // bad size + Void::Testing::expect(rejects("sync_file 1 error nope=1")); // unknown field + Void::Testing::expect(rejects("bad name 1 error")); // invalid boundary + Void::Testing::expect( + rejects("sync_file 1 error\nsync_file 1 error")); // duplicate + Void::Testing::expect( + rejects("a 1 error\nb 1 error", FaultFileLimits{.max_entries = 1})); + Void::Testing::expect(rejects(std::string(128, 'a') + " 1 error", + FaultFileLimits{.max_bytes = 32})); +} + +Void::Testing::Test testing_fault_plan_fires_by_hit_index_and_reconciles() +{ + auto specs = parse_fault_file( + "io 1 error code=EAGAIN\n" + "io 3 partial partial=8\n" + "sync 2 pause\n"); + Void::Testing::require(specs.has_value()); + FaultPlan plan{std::move(*specs)}; + + // io occurrence 1 fires the error. + auto first = plan.check("io"); + Void::Testing::expect(first.triggered); + Void::Testing::expect(first.action == FaultAction::Error); + Void::Testing::expect_eq(first.error_code, std::string{"EAGAIN"}); + + // Occurrence 2 does not fire; occurrence 3 fires the partial write. + Void::Testing::expect(!plan.check("io").triggered); + auto third = plan.check("io"); + Void::Testing::expect(third.triggered); + Void::Testing::expect(third.action == FaultAction::PartialWrite); + Void::Testing::require(third.partial_bytes.has_value()); + Void::Testing::expect_eq(*third.partial_bytes, std::size_t{8}); + + // Each spec fires once. + Void::Testing::expect(!plan.check("io").triggered); + Void::Testing::expect_eq(plan.occurrences("io"), std::size_t{4}); + + // sync fires on its second occurrence. + Void::Testing::expect(!plan.check("sync").triggered); + Void::Testing::expect(plan.check("sync").triggered); + + auto totals = plan.totals(); + Void::Testing::expect_eq(totals.configured, std::size_t{3}); + Void::Testing::expect_eq(totals.fired, std::size_t{3}); + Void::Testing::expect_eq(totals.remaining, std::size_t{0}); + Void::Testing::expect_eq(totals.configured, totals.fired + totals.remaining); +} + +Void::Testing::Test testing_fault_plan_bounds_boundary_names() +{ + FaultPlan plan{{FaultSpec{"a", 1, FaultAction::Error, std::nullopt, {}}, + FaultSpec{"b", 1, FaultAction::Error, std::nullopt, {}}}, + 1}; + // Only the first boundary is retained. + Void::Testing::expect(plan.check("a").triggered); + Void::Testing::expect(!plan.check("b").triggered); + Void::Testing::expect_eq(plan.totals().configured, std::size_t{1}); +}