diff --git a/include/Void/Testing/Corpus.hpp b/include/Void/Testing/Corpus.hpp new file mode 100644 index 0000000..864f244 --- /dev/null +++ b/include/Void/Testing/Corpus.hpp @@ -0,0 +1,51 @@ +#pragma once + +#include + +#include +#include +#include +#include +#include +#include + +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 failures{}; + }; + + /// A single corpus case: success means the input was handled correctly. + using CorpusCase = + std::function(std::span)>; + + /// 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 + run_corpus(const std::filesystem::path& directory, + const CorpusCase& run, + CorpusLimits limits = {}); +} // namespace Void::Testing diff --git a/src/Testing/Corpus.cpp b/src/Testing/Corpus.cpp new file mode 100644 index 0000000..97e6a1f --- /dev/null +++ b/src/Testing/Corpus.cpp @@ -0,0 +1,118 @@ +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +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 + 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 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 data(static_cast(size)); + if (!data.empty()) { + in.read(reinterpret_cast(data.data()), + static_cast(data.size())); + } + if (!in) { + ++report.failed; + report.failures.push_back( + CorpusFailure{path, "cannot read input"}); + continue; + } + + try { + if (auto outcome = run(std::span{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 diff --git a/tests/standalone/Testing/Corpus.cpp b/tests/standalone/Testing/Corpus.cpp new file mode 100644 index 0000000..623cef3 --- /dev/null +++ b/tests/standalone/Testing/Corpus.cpp @@ -0,0 +1,94 @@ +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +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(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 input) -> Void::Result::Result { + const std::string text{ + reinterpret_cast(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) { return Void::Result::Result{}; }, + 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) { return Void::Result::Result{}; }); + Void::Testing::expect(!missing.has_value()); + + std::filesystem::remove_all(directory); +}