Testing: add bounded corpus runner (M-TESTING-05)
This commit is contained in:
1 parent
d338305cc7
commit
80e6ed9afc
3 files changed
+263
No files matched your search
@@ -0,0 +1,51 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Functional/Result.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <filesystem>
|
||||
#include <functional>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Testing
|
||||
{
|
||||
/// Bounds for one corpus replay.
|
||||
struct CorpusLimits {
|
||||
/// Files larger than this are rejected instead of read.
|
||||
std::size_t max_input_bytes{1024ULL * 1024ULL};
|
||||
/// Maximum number of files replayed; the rest are reported as
|
||||
/// truncated so a huge directory cannot stall a run.
|
||||
std::size_t max_files{10000};
|
||||
};
|
||||
|
||||
/// One failing input and why it failed.
|
||||
struct CorpusFailure {
|
||||
std::filesystem::path path{};
|
||||
std::string message{};
|
||||
};
|
||||
|
||||
struct CorpusReport {
|
||||
std::size_t files{0};
|
||||
std::size_t passed{0};
|
||||
std::size_t failed{0};
|
||||
/// True when max_files cut the replay short.
|
||||
bool truncated{false};
|
||||
std::vector<CorpusFailure> failures{};
|
||||
};
|
||||
|
||||
/// A single corpus case: success means the input was handled correctly.
|
||||
using CorpusCase =
|
||||
std::function<Void::Result::Result<void>(std::span<const std::byte>)>;
|
||||
|
||||
/// Replay every regular file in `directory` (sorted, non-recursive).
|
||||
///
|
||||
/// Each file is bounded by `limits.max_input_bytes`; a case failure,
|
||||
/// exception or oversized file is recorded and the replay continues, so
|
||||
/// one bad input never hides the rest. A missing directory is an error.
|
||||
[[nodiscard]] Void::Result::Result<CorpusReport>
|
||||
run_corpus(const std::filesystem::path& directory,
|
||||
const CorpusCase& run,
|
||||
CorpusLimits limits = {});
|
||||
} // namespace Void::Testing
|
||||
@@ -0,0 +1,118 @@
|
||||
#include <Void/Testing/Corpus.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <exception>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <system_error>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Testing
|
||||
{
|
||||
namespace
|
||||
{
|
||||
[[nodiscard]] Void::Result::Error corpus_error(std::string message)
|
||||
{
|
||||
return Void::Result::Error{
|
||||
Void::Result::ErrorKind::invalid_argument, std::move(message)};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Void::Result::Result<CorpusReport>
|
||||
run_corpus(const std::filesystem::path& directory,
|
||||
const CorpusCase& run,
|
||||
CorpusLimits limits)
|
||||
{
|
||||
std::error_code ec;
|
||||
if (!std::filesystem::is_directory(directory, ec) || ec) {
|
||||
return std::unexpected{corpus_error(
|
||||
"run_corpus: '" + directory.string() + "' is not a directory")};
|
||||
}
|
||||
if (!run) {
|
||||
return std::unexpected{
|
||||
corpus_error("run_corpus: case callback must be set")};
|
||||
}
|
||||
|
||||
std::vector<std::filesystem::path> paths;
|
||||
for (std::filesystem::directory_iterator it{directory, ec}, end;
|
||||
it != end && !ec; it.increment(ec)) {
|
||||
std::error_code type_ec;
|
||||
if (it->is_regular_file(type_ec) && !type_ec) {
|
||||
paths.push_back(it->path());
|
||||
}
|
||||
}
|
||||
if (ec) {
|
||||
return std::unexpected{corpus_error(
|
||||
"run_corpus: cannot list '" + directory.string() +
|
||||
"': " + ec.message())};
|
||||
}
|
||||
std::sort(paths.begin(), paths.end());
|
||||
|
||||
CorpusReport report;
|
||||
if (paths.size() > limits.max_files) {
|
||||
paths.resize(limits.max_files);
|
||||
report.truncated = true;
|
||||
}
|
||||
|
||||
for (const auto& path : paths) {
|
||||
++report.files;
|
||||
|
||||
const auto size = std::filesystem::file_size(path, ec);
|
||||
if (ec) {
|
||||
++report.failed;
|
||||
report.failures.push_back(
|
||||
CorpusFailure{path, "cannot stat: " + ec.message()});
|
||||
continue;
|
||||
}
|
||||
if (size > limits.max_input_bytes) {
|
||||
++report.failed;
|
||||
report.failures.push_back(
|
||||
CorpusFailure{path, "input exceeds max_input_bytes"});
|
||||
continue;
|
||||
}
|
||||
|
||||
std::ifstream in{path, std::ios::binary};
|
||||
if (!in) {
|
||||
++report.failed;
|
||||
report.failures.push_back(
|
||||
CorpusFailure{path, "cannot open input"});
|
||||
continue;
|
||||
}
|
||||
std::vector<std::byte> data(static_cast<std::size_t>(size));
|
||||
if (!data.empty()) {
|
||||
in.read(reinterpret_cast<char*>(data.data()),
|
||||
static_cast<std::streamsize>(data.size()));
|
||||
}
|
||||
if (!in) {
|
||||
++report.failed;
|
||||
report.failures.push_back(
|
||||
CorpusFailure{path, "cannot read input"});
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
if (auto outcome = run(std::span<const std::byte>{data});
|
||||
!outcome) {
|
||||
++report.failed;
|
||||
report.failures.push_back(
|
||||
CorpusFailure{path, outcome.error().message()});
|
||||
continue;
|
||||
}
|
||||
} catch (const std::exception& error) {
|
||||
++report.failed;
|
||||
report.failures.push_back(
|
||||
CorpusFailure{path, std::string{"threw: "} + error.what()});
|
||||
continue;
|
||||
} catch (...) {
|
||||
++report.failed;
|
||||
report.failures.push_back(
|
||||
CorpusFailure{path, "threw an unknown exception"});
|
||||
continue;
|
||||
}
|
||||
++report.passed;
|
||||
}
|
||||
return report;
|
||||
}
|
||||
} // namespace Void::Testing
|
||||
@@ -0,0 +1,94 @@
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Corpus.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
std::filesystem::path make_corpus_dir()
|
||||
{
|
||||
const auto directory =
|
||||
std::filesystem::temp_directory_path() /
|
||||
("void-corpus-" + std::to_string(::getpid()));
|
||||
std::filesystem::remove_all(directory);
|
||||
std::filesystem::create_directories(directory);
|
||||
return directory;
|
||||
}
|
||||
|
||||
void write_file(const std::filesystem::path& path, std::string_view text)
|
||||
{
|
||||
std::ofstream out{path, std::ios::binary | std::ios::trunc};
|
||||
out.write(text.data(), static_cast<std::streamsize>(text.size()));
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test corpus_replays_and_captures_failures()
|
||||
{
|
||||
const auto directory = make_corpus_dir();
|
||||
write_file(directory / "a_ok.txt", "alpha");
|
||||
write_file(directory / "b_bad.txt", "reject");
|
||||
write_file(directory / "c_throw.txt", "boom");
|
||||
std::filesystem::create_directories(directory / "nested");
|
||||
write_file(directory / "nested" / "ignored.txt", "nested");
|
||||
|
||||
auto report = Void::Testing::run_corpus(
|
||||
directory, [](std::span<const std::byte> input) -> Void::Result::Result<void> {
|
||||
const std::string text{
|
||||
reinterpret_cast<const char*>(input.data()), input.size()};
|
||||
if (text == "reject") {
|
||||
return std::unexpected{Void::Result::Error{
|
||||
Void::Result::ErrorKind::invalid_argument, "rejected input"}};
|
||||
}
|
||||
if (text == "boom") {
|
||||
throw std::runtime_error{"case exploded"};
|
||||
}
|
||||
return {};
|
||||
});
|
||||
Void::Testing::require(report.has_value());
|
||||
// Non-recursive: the nested directory is not replayed.
|
||||
Void::Testing::expect_eq(report->files, std::size_t{3});
|
||||
Void::Testing::expect_eq(report->passed, std::size_t{1});
|
||||
Void::Testing::expect_eq(report->failed, std::size_t{2});
|
||||
Void::Testing::expect(!report->truncated);
|
||||
Void::Testing::require(report->failures.size() == 2);
|
||||
Void::Testing::expect_eq(report->failures[0].path.filename().string(),
|
||||
std::string{"b_bad.txt"});
|
||||
Void::Testing::expect(report->failures[0].message.find("rejected") !=
|
||||
std::string::npos);
|
||||
Void::Testing::expect(report->failures[1].message.find("case exploded") !=
|
||||
std::string::npos);
|
||||
std::filesystem::remove_all(directory);
|
||||
}
|
||||
|
||||
Void::Testing::Test corpus_enforces_bounds_and_missing_directory()
|
||||
{
|
||||
const auto directory = make_corpus_dir();
|
||||
write_file(directory / "small.txt", "x");
|
||||
write_file(directory / "large.txt", std::string(64, 'y'));
|
||||
|
||||
auto bounded = Void::Testing::run_corpus(
|
||||
directory,
|
||||
[](std::span<const std::byte>) { return Void::Result::Result<void>{}; },
|
||||
Void::Testing::CorpusLimits{.max_input_bytes = 16, .max_files = 1});
|
||||
Void::Testing::require(bounded.has_value());
|
||||
// max_files truncates to one file (sorted: large.txt).
|
||||
Void::Testing::expect(bounded->truncated);
|
||||
Void::Testing::expect_eq(bounded->files, std::size_t{1});
|
||||
Void::Testing::expect_eq(bounded->failed, std::size_t{1});
|
||||
Void::Testing::require(bounded->failures.size() == 1);
|
||||
Void::Testing::expect(bounded->failures[0].message.find("max_input_bytes") !=
|
||||
std::string::npos);
|
||||
|
||||
auto missing = Void::Testing::run_corpus(
|
||||
directory / "does-not-exist",
|
||||
[](std::span<const std::byte>) { return Void::Result::Result<void>{}; });
|
||||
Void::Testing::expect(!missing.has_value());
|
||||
|
||||
std::filesystem::remove_all(directory);
|
||||
}
|
||||
Reference in new issue
Block a user