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,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
|
||||
Reference in new issue
Block a user