Testing: add bounded fault-file plan port (M-TESTING-06)
This commit is contained in:
1 parent
4b2bc8b25f
commit
48e5e223af
3 files changed
+445
No files matched your search
@@ -0,0 +1,247 @@
|
||||
#include <Void/Testing/FaultFile.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <charconv>
|
||||
#include <cstddef>
|
||||
#include <format>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
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<std::string_view>
|
||||
split_fields(std::string_view line) noexcept
|
||||
{
|
||||
std::vector<std::string_view> 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<std::vector<FaultSpec>>
|
||||
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<FaultSpec> 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<FaultSpec> 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
|
||||
Reference in new issue
Block a user