srctree

Robin Linden parent e8e13a4b a5f84a9b
net: Yoink FakeSocket from //protocol

net/BUILD added: 79, removed: 76, total 3
@@ -31,6 +31,13 @@ cc_library(
],
)
 
cc_library(
name = "test",
testonly = True,
hdrs = glob(["test/*.h"]),
visibility = ["//visibility:public"],
)
 
[cc_test(
name = src[:-4],
size = "small",
 
filename was Deleted added: 79, removed: 76, total 3
@@ -0,0 +1,56 @@
// SPDX-FileCopyrightText: 2021-2022 Mikael Larsson <c.mikael.larsson@gmail.com>
// SPDX-FileCopyrightText: 2023-2024 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
#ifndef NET_TEST_FAKE_SOCKET_H_
#define NET_TEST_FAKE_SOCKET_H_
 
#include <cstddef>
#include <string>
#include <string_view>
 
namespace net {
 
struct FakeSocket {
bool connect(std::string_view h, std::string_view s) {
host = h;
service = s;
return connect_result;
}
 
std::size_t write(std::string_view data) {
write_data = data;
return write_data.size();
}
 
std::string read_all() const { return read_data; }
 
std::string read_until(std::string_view d) {
delimiter = 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;
}
 
std::string host{};
std::string service{};
std::string write_data{};
std::string read_data{};
std::string delimiter{};
bool connect_result{true};
};
 
} // namespace net
 
#endif
 
protocol/BUILD added: 79, removed: 76, total 3
@@ -27,7 +27,10 @@ cc_library(
srcs = [src],
copts = HASTUR_COPTS,
target_compatible_with = HASTUR_FUZZ_PLATFORMS,
deps = [":protocol"],
deps = [
":protocol",
"//net:test",
],
) for src in glob(["*_fuzz_test.cpp"])]
 
[cc_test(
@@ -38,6 +41,7 @@ cc_library(
deps = [
":protocol",
"//etest",
"//net:test",
"//uri",
"@expected",
"@fmt",
 
protocol/http_fuzz_test.cpp added: 79, removed: 76, total 3
@@ -1,46 +1,20 @@
// SPDX-FileCopyrightText: 2023 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2023-2024 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
#include "protocol/http.h"
 
#include "net/test/fake_socket.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}};
net::FakeSocket socket{.read_data{reinterpret_cast<char const *>(data), size}};
std::ignore = protocol::Http::get(socket, {}, std::nullopt);
return 0;
}
 
protocol/http_test.cpp added: 79, removed: 76, total 3
@@ -6,11 +6,11 @@
#include "protocol/http.h"
 
#include "etest/etest.h"
#include "net/test/fake_socket.h"
#include "protocol/response.h"
#include "uri/uri.h"
 
#include <cassert>
#include <cstddef>
#include <optional>
#include <string>
#include <string_view>
@@ -21,48 +21,10 @@ using namespace std::string_view_literals;
 
using etest::expect_eq;
using etest::require;
using net::FakeSocket;
 
namespace {
 
struct FakeSocket {
bool connect(std::string_view h, std::string_view s) {
host = h;
service = s;
return connect_result;
}
 
std::size_t write(std::string_view data) {
write_data = data;
return write_data.size();
}
 
std::string read_all() const { return read_data; }
 
std::string read_until(std::string_view d) {
delimiter = 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;
}
 
std::string host{};
std::string service{};
std::string write_data{};
std::string read_data{};
std::string delimiter{};
bool connect_result{true};
};
 
uri::Uri create_uri(std::string url = "http://example.com") {
auto parsed = uri::Uri::parse(std::move(url));
assert(parsed.has_value());