srctree

Robin Linden parent 02ca8141 b2275e35
protocol/http: Set up fuzz testing

inlinesplit
protocol/BUILD added: 63, removed: 4, total 59
@@ -1,5 +1,6 @@
load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
load("//bzl:copts.bzl", "HASTUR_COPTS")
load("@rules_fuzzing//fuzzing:cc_defs.bzl", "cc_fuzz_test")
load("//bzl:copts.bzl", "HASTUR_COPTS", "HASTUR_FUZZ_PLATFORMS")
 
cc_library(
name = "protocol",
@@ -18,6 +19,15 @@ cc_library(
],
)
 
[cc_fuzz_test(
name = src[:-4],
size = "small",
srcs = [src],
copts = HASTUR_COPTS,
target_compatible_with = HASTUR_FUZZ_PLATFORMS,
deps = [":protocol"],
) for src in glob(["*_fuzz_test.cpp"])]
 
[cc_test(
name = src[:-4],
size = "small",
@@ -29,4 +39,7 @@ cc_library(
"//uri",
"@fmt",
],
) for src in glob(["*_test.cpp"])]
) for src in glob(
include = ["*_test.cpp"],
exclude = ["*_fuzz_test.cpp"],
)]
 
filename was Deleted added: 63, removed: 4, total 59
@@ -0,0 +1,46 @@
// SPDX-FileCopyrightText: 2023 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
#include "protocol/http.h"
 
#include <cstddef>
#include <stddef.h> // NOLINT
#include <stdint.h> // NOLINT
#include <string>
#include <string_view>
#include <tuple>
 
namespace {
struct FakeSocket {
std::string read_data{};
 
bool connect(std::string_view, std::string_view) { return true; }
std::size_t write(std::string_view data) { return data.size(); }
std::string read_all() const { return read_data; }
 
std::string read_until(std::string_view d) {
std::string result{};
if (auto pos = read_data.find(d); pos != std::string::npos) {
pos += d.size();
result = read_data.substr(0, pos);
read_data.erase(0, pos);
}
return result;
}
 
std::string read_bytes(std::size_t bytes) {
std::string result = read_data.substr(0, bytes);
read_data.erase(0, bytes);
return result;
}
};
} // namespace
 
extern "C" int LLVMFuzzerTestOneInput(uint8_t const *data, size_t size); // NOLINT
 
extern "C" int LLVMFuzzerTestOneInput(uint8_t const *data, size_t size) {
FakeSocket socket{.read_data{reinterpret_cast<char const *>(data), size}};
std::ignore = protocol::Http::get(socket, {}, std::nullopt);
return 0;
}