1 parent
fa30098129
commit
8583a479dd
11 files changed
+1763
-129
No files matched your search
@@ -19,6 +19,25 @@ namespace
|
||||
std::ifstream f(p);
|
||||
return {std::istreambuf_iterator<char>(f), {}};
|
||||
}
|
||||
|
||||
std::size_t archive_count(const std::filesystem::path& path)
|
||||
{
|
||||
const auto directory = path.parent_path() / "history";
|
||||
if (!std::filesystem::exists(directory)) {
|
||||
return 0;
|
||||
}
|
||||
const auto prefix = path.stem().string() + "-";
|
||||
std::size_t count = 0;
|
||||
for (const auto& entry :
|
||||
std::filesystem::directory_iterator(directory)) {
|
||||
const auto filename = entry.path().filename().string();
|
||||
if (entry.is_regular_file() && filename.starts_with(prefix) &&
|
||||
entry.path().extension() == path.extension()) {
|
||||
++count;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
class SplitFileLoggerTests : public Void::Testing::Fixture
|
||||
@@ -135,8 +154,8 @@ class HistoryLoggerTests : public Void::Testing::Fixture
|
||||
std::to_string(i));
|
||||
}
|
||||
}
|
||||
auto rotated = path;
|
||||
rotated += ".1";
|
||||
Void::Testing::expect(std::filesystem::exists(rotated));
|
||||
const auto archives = archive_count(path);
|
||||
Void::Testing::expect(archives >= 1);
|
||||
Void::Testing::expect(archives <= 3);
|
||||
}
|
||||
};
|
||||
+144
-125
@@ -1,7 +1,7 @@
|
||||
#include <Void/Log/Sinks.hpp>
|
||||
#include <Void/Log/ConsoleLogger.hpp>
|
||||
#include <Void/Log/Formatter.hpp>
|
||||
#include <Void/Log/RotatingFileLogger.hpp>
|
||||
#include <Void/Log/Sinks.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Fixture.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
@@ -14,170 +14,189 @@
|
||||
|
||||
using namespace Void::Logging;
|
||||
|
||||
// ── NullLogger / CallbackLogger ───────────────────────────────────────────────
|
||||
namespace
|
||||
{
|
||||
std::size_t archive_count(const std::filesystem::path& path)
|
||||
{
|
||||
const auto directory = path.parent_path() / "history";
|
||||
if (!std::filesystem::exists(directory)) {
|
||||
return 0;
|
||||
}
|
||||
const auto prefix = path.stem().string() + "-";
|
||||
std::size_t count = 0;
|
||||
for (const auto& entry :
|
||||
std::filesystem::directory_iterator(directory)) {
|
||||
const auto filename = entry.path().filename().string();
|
||||
if (entry.is_regular_file() && filename.starts_with(prefix) &&
|
||||
entry.path().extension() == path.extension()) {
|
||||
++count;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
// ── NullLogger / CallbackLogger
|
||||
// ───────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test null_logger_swallows_everything()
|
||||
{
|
||||
NullLogger log;
|
||||
log.fatal("into the void");
|
||||
log.flush();
|
||||
Void::Testing::expect(true); // reached without crashing
|
||||
NullLogger log;
|
||||
log.fatal("into the void");
|
||||
log.flush();
|
||||
Void::Testing::expect(true); // reached without crashing
|
||||
}
|
||||
|
||||
Void::Testing::Test callback_logger_receives_entry_fields()
|
||||
{
|
||||
Entry got;
|
||||
CallbackLogger log{[&](const Entry& e) { got = e; },
|
||||
Mode::Immediate};
|
||||
log.warning("payload");
|
||||
Void::Testing::expect(got.level == Level::Warning);
|
||||
Void::Testing::expect_eq(got.message, std::string{"payload"});
|
||||
Entry got;
|
||||
CallbackLogger log{[&](const Entry& e) { got = e; }, Mode::Immediate};
|
||||
log.warning("payload");
|
||||
Void::Testing::expect(got.level == Level::Warning);
|
||||
Void::Testing::expect_eq(got.message, std::string{"payload"});
|
||||
}
|
||||
|
||||
// ── MultiLogger ───────────────────────────────────────────────────────────────
|
||||
// ── MultiLogger
|
||||
// ───────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test multi_logger_fans_out_to_all_children()
|
||||
{
|
||||
int a = 0;
|
||||
int b = 0;
|
||||
MultiLogger multi;
|
||||
multi.add(std::make_shared<CallbackLogger>(
|
||||
[&](const Entry&) { ++a; }, Mode::Immediate));
|
||||
multi.add(std::make_shared<CallbackLogger>(
|
||||
[&](const Entry&) { ++b; }, Mode::Immediate));
|
||||
int a = 0;
|
||||
int b = 0;
|
||||
MultiLogger multi;
|
||||
multi.add(std::make_shared<CallbackLogger>([&](const Entry&) { ++a; },
|
||||
Mode::Immediate));
|
||||
multi.add(std::make_shared<CallbackLogger>([&](const Entry&) { ++b; },
|
||||
Mode::Immediate));
|
||||
|
||||
multi.info("to both");
|
||||
Void::Testing::expect_eq(a, 1);
|
||||
Void::Testing::expect_eq(b, 1);
|
||||
Void::Testing::expect_eq(multi.child_count(), std::size_t{2});
|
||||
multi.info("to both");
|
||||
Void::Testing::expect_eq(a, 1);
|
||||
Void::Testing::expect_eq(b, 1);
|
||||
Void::Testing::expect_eq(multi.child_count(), std::size_t{2});
|
||||
}
|
||||
|
||||
Void::Testing::Test multi_logger_children_keep_own_levels()
|
||||
{
|
||||
int verbose_count = 0;
|
||||
int errors_only = 0;
|
||||
auto verbose = std::make_shared<CallbackLogger>(
|
||||
[&](const Entry&) { ++verbose_count; }, Mode::Immediate);
|
||||
auto strict = std::make_shared<CallbackLogger>(
|
||||
[&](const Entry&) { ++errors_only; }, Mode::Immediate);
|
||||
strict->set_level(Level::Error);
|
||||
int verbose_count = 0;
|
||||
int errors_only = 0;
|
||||
auto verbose = std::make_shared<CallbackLogger>(
|
||||
[&](const Entry&) { ++verbose_count; }, Mode::Immediate);
|
||||
auto strict = std::make_shared<CallbackLogger>(
|
||||
[&](const Entry&) { ++errors_only; }, Mode::Immediate);
|
||||
strict->set_level(Level::Error);
|
||||
|
||||
MultiLogger multi;
|
||||
multi.add(verbose);
|
||||
multi.add(strict);
|
||||
MultiLogger multi;
|
||||
multi.add(verbose);
|
||||
multi.add(strict);
|
||||
|
||||
multi.info("info");
|
||||
multi.error("error");
|
||||
Void::Testing::expect_eq(verbose_count, 2);
|
||||
Void::Testing::expect_eq(errors_only, 1);
|
||||
multi.info("info");
|
||||
multi.error("error");
|
||||
Void::Testing::expect_eq(verbose_count, 2);
|
||||
Void::Testing::expect_eq(errors_only, 1);
|
||||
}
|
||||
|
||||
Void::Testing::Test multi_logger_relays_async_children_with_flush()
|
||||
{
|
||||
int delivered = 0;
|
||||
auto async_child = std::make_shared<CallbackLogger>(
|
||||
[&](const Entry&) { ++delivered; }); // Async by default
|
||||
MultiLogger multi;
|
||||
multi.add(async_child);
|
||||
int delivered = 0;
|
||||
auto async_child = std::make_shared<CallbackLogger>(
|
||||
[&](const Entry&) { ++delivered; }); // Async by default
|
||||
MultiLogger multi;
|
||||
multi.add(async_child);
|
||||
|
||||
multi.info("queued in child");
|
||||
multi.flush(); // flushes children too
|
||||
Void::Testing::expect_eq(delivered, 1);
|
||||
multi.info("queued in child");
|
||||
multi.flush(); // flushes children too
|
||||
Void::Testing::expect_eq(delivered, 1);
|
||||
}
|
||||
|
||||
// ── ConsoleLogger + Formatter ─────────────────────────────────────────────────
|
||||
// ── ConsoleLogger + Formatter
|
||||
// ─────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test console_logger_uses_installed_formatter()
|
||||
{
|
||||
std::ostringstream out;
|
||||
{
|
||||
ConsoleLogger log{out, Mode::Immediate};
|
||||
log.set_formatter(
|
||||
std::make_shared<PatternFormatter>("{level}>{message}"));
|
||||
log.info("custom format");
|
||||
}
|
||||
Void::Testing::expect_eq(out.str(),
|
||||
std::string{"Info>custom format\n"});
|
||||
std::ostringstream out;
|
||||
{
|
||||
ConsoleLogger log{out, Mode::Immediate};
|
||||
log.set_formatter(
|
||||
std::make_shared<PatternFormatter>("{level}>{message}"));
|
||||
log.info("custom format");
|
||||
}
|
||||
Void::Testing::expect_eq(out.str(), std::string{"Info>custom format\n"});
|
||||
}
|
||||
|
||||
// ── RotatingFileLogger ────────────────────────────────────────────────────────
|
||||
// ── RotatingFileLogger
|
||||
// ────────────────────────────────────────────────────────
|
||||
|
||||
class RotatingFileLoggerTests : public Void::Testing::Fixture
|
||||
{
|
||||
public:
|
||||
public:
|
||||
|
||||
std::filesystem::path dir;
|
||||
std::filesystem::path file;
|
||||
std::filesystem::path dir;
|
||||
std::filesystem::path file;
|
||||
|
||||
void setup() override
|
||||
{
|
||||
dir = std::filesystem::temp_directory_path() /
|
||||
"void_rotating_test";
|
||||
std::filesystem::remove_all(dir);
|
||||
std::filesystem::create_directories(dir);
|
||||
file = dir / "app.log";
|
||||
}
|
||||
void setup() override
|
||||
{
|
||||
dir = std::filesystem::temp_directory_path() / "void_rotating_test";
|
||||
std::filesystem::remove_all(dir);
|
||||
std::filesystem::create_directories(dir);
|
||||
file = dir / "app.log";
|
||||
}
|
||||
|
||||
void teardown() override { std::filesystem::remove_all(dir); }
|
||||
void teardown() override { std::filesystem::remove_all(dir); }
|
||||
|
||||
static std::string slurp(const std::filesystem::path& p)
|
||||
{
|
||||
std::ifstream f(p);
|
||||
return {std::istreambuf_iterator<char>(f), {}};
|
||||
}
|
||||
static std::string slurp(const std::filesystem::path& p)
|
||||
{
|
||||
std::ifstream f(p);
|
||||
return {std::istreambuf_iterator<char>(f), {}};
|
||||
}
|
||||
|
||||
Void::Testing::Test writes_and_flushes()
|
||||
{
|
||||
{
|
||||
RotatingFileLogger log{file};
|
||||
log.info("rotating hello");
|
||||
}
|
||||
Void::Testing::expect(slurp(file).find("rotating hello") !=
|
||||
std::string::npos);
|
||||
}
|
||||
Void::Testing::Test writes_and_flushes()
|
||||
{
|
||||
{
|
||||
RotatingFileLogger log{file};
|
||||
log.info("rotating hello");
|
||||
}
|
||||
Void::Testing::expect(slurp(file).find("rotating hello") !=
|
||||
std::string::npos);
|
||||
}
|
||||
|
||||
Void::Testing::Test rotates_when_size_exceeded()
|
||||
{
|
||||
{
|
||||
RotatingFileLogger::Options opt;
|
||||
opt.max_bytes = 256;
|
||||
opt.max_files = 3;
|
||||
opt.mode = Mode::Immediate;
|
||||
RotatingFileLogger log{file, opt};
|
||||
for (int i = 0; i < 40; ++i) {
|
||||
log.info("a reasonably long line to fill the file " +
|
||||
std::to_string(i));
|
||||
}
|
||||
}
|
||||
Void::Testing::expect(std::filesystem::exists(file));
|
||||
auto rotated = file;
|
||||
rotated += ".1";
|
||||
Void::Testing::expect(std::filesystem::exists(rotated));
|
||||
// The bound holds: no file beyond .3
|
||||
auto beyond = file;
|
||||
beyond += ".4";
|
||||
Void::Testing::expect(!std::filesystem::exists(beyond));
|
||||
}
|
||||
Void::Testing::Test rotates_when_size_exceeded()
|
||||
{
|
||||
{
|
||||
RotatingFileLogger::Options opt;
|
||||
opt.max_bytes = 256;
|
||||
opt.max_files = 3;
|
||||
opt.mode = Mode::Immediate;
|
||||
RotatingFileLogger log{file, opt};
|
||||
for (int i = 0; i < 40; ++i) {
|
||||
log.info("a reasonably long line to fill the file " +
|
||||
std::to_string(i));
|
||||
}
|
||||
}
|
||||
Void::Testing::expect(std::filesystem::exists(file));
|
||||
const auto archives = archive_count(file);
|
||||
Void::Testing::expect(archives >= 1);
|
||||
Void::Testing::expect(archives <= 3);
|
||||
}
|
||||
|
||||
Void::Testing::Test open_factory_rejects_bad_path()
|
||||
{
|
||||
auto r = RotatingFileLogger::open(
|
||||
"/this/path/does/not/exist/app.log");
|
||||
Void::Testing::expect(!r.has_value());
|
||||
}
|
||||
Void::Testing::Test open_factory_rejects_bad_path()
|
||||
{
|
||||
auto r =
|
||||
RotatingFileLogger::open("/this/path/does/not/exist/app.log");
|
||||
Void::Testing::expect(!r.has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test async_rotation_under_load()
|
||||
{
|
||||
{
|
||||
RotatingFileLogger::Options opt;
|
||||
opt.max_bytes = 512;
|
||||
opt.max_files = 2;
|
||||
RotatingFileLogger log{file, opt}; // Async default
|
||||
for (int i = 0; i < 200; ++i) {
|
||||
log.info("async rotation line " + std::to_string(i));
|
||||
}
|
||||
log.flush();
|
||||
}
|
||||
Void::Testing::expect(std::filesystem::exists(file));
|
||||
}
|
||||
Void::Testing::Test async_rotation_under_load()
|
||||
{
|
||||
{
|
||||
RotatingFileLogger::Options opt;
|
||||
opt.max_bytes = 512;
|
||||
opt.max_files = 2;
|
||||
RotatingFileLogger log{file, opt}; // Async default
|
||||
for (int i = 0; i < 200; ++i) {
|
||||
log.info("async rotation line " + std::to_string(i));
|
||||
}
|
||||
log.flush();
|
||||
}
|
||||
Void::Testing::expect(std::filesystem::exists(file));
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,330 @@
|
||||
#include <Void/Net/Server.hpp>
|
||||
#include <Void/System/TempDir.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Suite.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
using Void::Net::Method;
|
||||
using Void::Net::Router;
|
||||
using Void::Net::ServerRequest;
|
||||
using Void::Net::ServerResponse;
|
||||
using Void::Net::StaticFileResolver;
|
||||
using Void::Net::WebSocketClose;
|
||||
using Void::Net::WebSocketConnection;
|
||||
using Void::Net::WebSocketConnectionId;
|
||||
using Void::Net::WebSocketEvents;
|
||||
using Void::Net::WebSocketSessions;
|
||||
using Void::Testing::Test;
|
||||
|
||||
void write_file(const std::filesystem::path& path, std::string_view body)
|
||||
{
|
||||
std::filesystem::create_directories(path.parent_path());
|
||||
std::ofstream output{path, std::ios::binary};
|
||||
output << body;
|
||||
}
|
||||
|
||||
struct FakeConnection final : WebSocketConnection {
|
||||
explicit FakeConnection(std::uint64_t value) : id_{value} {}
|
||||
|
||||
[[nodiscard]] WebSocketConnectionId id() const noexcept override
|
||||
{
|
||||
return id_;
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void>
|
||||
send_text(std::string_view message) override
|
||||
{
|
||||
sent_text.emplace_back(message);
|
||||
return {};
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void>
|
||||
send_binary(std::span<const std::byte> message) override
|
||||
{
|
||||
sent_binary.emplace_back(message.begin(), message.end());
|
||||
return {};
|
||||
}
|
||||
|
||||
void close(const WebSocketClose& info) noexcept override
|
||||
{
|
||||
closed = true;
|
||||
close_code = info.code;
|
||||
}
|
||||
|
||||
WebSocketConnectionId id_;
|
||||
std::vector<std::string> sent_text;
|
||||
std::vector<std::vector<std::byte>> sent_binary;
|
||||
bool closed{false};
|
||||
std::uint16_t close_code{};
|
||||
};
|
||||
|
||||
struct RouterTests : Void::Testing::Suite<RouterTests> {
|
||||
Test routes_by_method_and_extracts_parameters()
|
||||
{
|
||||
Router router;
|
||||
auto added = router.add(
|
||||
Method::Get, "/rooms/:room/members/:member",
|
||||
[](const ServerRequest& request) {
|
||||
return Void::Result::ok(ServerResponse{
|
||||
.status = 200,
|
||||
.body = request.parameters.at("room") + ":" +
|
||||
request.parameters.at("member")});
|
||||
});
|
||||
Void::Testing::require(added.has_value());
|
||||
|
||||
auto response = router.dispatch(
|
||||
ServerRequest{.method = Method::Get,
|
||||
.path = "/rooms/alpha/members/7?full=1"});
|
||||
Void::Testing::require(response.has_value());
|
||||
Void::Testing::require(response->has_value());
|
||||
Void::Testing::expect_eq((*response)->body,
|
||||
std::string{"alpha:7"});
|
||||
|
||||
auto miss = router.dispatch(ServerRequest{
|
||||
.method = Method::Post, .path = "/rooms/alpha/members/7"});
|
||||
Void::Testing::require(miss.has_value());
|
||||
Void::Testing::expect(!miss->has_value());
|
||||
}
|
||||
|
||||
Test final_catch_all_captures_remaining_path()
|
||||
{
|
||||
Router router;
|
||||
Void::Testing::require(
|
||||
router
|
||||
.add(Method::Get, "/assets/*path",
|
||||
[](const ServerRequest& request) {
|
||||
return Void::Result::ok(ServerResponse{
|
||||
.body = request.parameters.at("path")});
|
||||
})
|
||||
.has_value());
|
||||
auto response = router.dispatch(ServerRequest{
|
||||
.method = Method::Get, .path = "/assets/js/app.js"});
|
||||
Void::Testing::require(response.has_value());
|
||||
Void::Testing::require(response->has_value());
|
||||
Void::Testing::expect_eq((*response)->body,
|
||||
std::string{"js/app.js"});
|
||||
}
|
||||
|
||||
Test static_route_takes_precedence_over_parameter_route()
|
||||
{
|
||||
Router router;
|
||||
Void::Testing::require(
|
||||
router
|
||||
.add(Method::Get, "/rooms/:room",
|
||||
[](const ServerRequest&) {
|
||||
return Void::Result::ok(
|
||||
ServerResponse{.body = "parameter"});
|
||||
})
|
||||
.has_value());
|
||||
Void::Testing::require(
|
||||
router
|
||||
.add(Method::Get, "/rooms/new",
|
||||
[](const ServerRequest&) {
|
||||
return Void::Result::ok(
|
||||
ServerResponse{.body = "static"});
|
||||
})
|
||||
.has_value());
|
||||
|
||||
auto response = router.dispatch(
|
||||
ServerRequest{.method = Method::Get, .path = "/rooms/new"});
|
||||
Void::Testing::require(response.has_value());
|
||||
Void::Testing::require(response->has_value());
|
||||
Void::Testing::expect_eq((*response)->body,
|
||||
std::string{"static"});
|
||||
}
|
||||
|
||||
Test rejects_invalid_and_duplicate_patterns()
|
||||
{
|
||||
Router router;
|
||||
auto handler = [](const ServerRequest&) {
|
||||
return Void::Result::ok(ServerResponse{});
|
||||
};
|
||||
Void::Testing::expect(
|
||||
!router.add(Method::Get, "relative", handler));
|
||||
Void::Testing::expect(
|
||||
!router.add(Method::Get, "/bad/*path/tail", handler));
|
||||
Void::Testing::require(
|
||||
router.add(Method::Get, "/health", handler).has_value());
|
||||
Void::Testing::expect(
|
||||
!router.add(Method::Get, "/health", handler).has_value());
|
||||
Void::Testing::require(
|
||||
router.add(Method::Get, "/users/:id", handler).has_value());
|
||||
Void::Testing::expect(
|
||||
!router.add(Method::Get, "/users/:name", handler)
|
||||
.has_value());
|
||||
}
|
||||
|
||||
Test header_lookup_is_case_insensitive()
|
||||
{
|
||||
ServerRequest request{
|
||||
.headers = {{"Content-Type", "application/json"}}};
|
||||
Void::Testing::require(
|
||||
request.header("content-type").has_value());
|
||||
Void::Testing::expect_eq(*request.header("CONTENT-TYPE"),
|
||||
std::string_view{"application/json"});
|
||||
}
|
||||
};
|
||||
|
||||
struct StaticFileTests : Void::Testing::Suite<StaticFileTests> {
|
||||
Test resolves_index_assets_and_content_types()
|
||||
{
|
||||
Void::FileSystem::TempDir directory{"void_net_static_"};
|
||||
write_file(directory / "index.html", "index");
|
||||
write_file(directory / "assets/app.js", "script");
|
||||
auto resolver = StaticFileResolver::create(directory.path());
|
||||
Void::Testing::require(resolver.has_value());
|
||||
|
||||
auto index = resolver->resolve("/");
|
||||
auto script = resolver->resolve("/assets/app.js?v=1");
|
||||
Void::Testing::require(index.has_value() && index->has_value());
|
||||
Void::Testing::require(script.has_value() &&
|
||||
script->has_value());
|
||||
Void::Testing::expect_eq((*index)->path.filename(),
|
||||
std::filesystem::path{"index.html"});
|
||||
Void::Testing::expect_eq(
|
||||
(*script)->content_type,
|
||||
std::string_view{"text/javascript; charset=utf-8"});
|
||||
Void::Testing::expect_eq((*script)->size,
|
||||
static_cast<std::uintmax_t>(6));
|
||||
}
|
||||
|
||||
Test rejects_plain_encoded_and_backslash_traversal()
|
||||
{
|
||||
Void::FileSystem::TempDir directory{"void_net_static_"};
|
||||
auto resolver = StaticFileResolver::create(directory.path());
|
||||
Void::Testing::require(resolver.has_value());
|
||||
Void::Testing::expect(
|
||||
!resolver->resolve("/../secret").has_value());
|
||||
Void::Testing::expect(
|
||||
!resolver->resolve("/%2e%2e/secret").has_value());
|
||||
Void::Testing::expect(
|
||||
!resolver->resolve("/..%2fsecret").has_value());
|
||||
Void::Testing::expect(
|
||||
!resolver->resolve("/dir%5csecret").has_value());
|
||||
}
|
||||
|
||||
Test missing_file_is_a_successful_empty_result()
|
||||
{
|
||||
Void::FileSystem::TempDir directory{"void_net_static_"};
|
||||
auto resolver = StaticFileResolver::create(directory.path());
|
||||
Void::Testing::require(resolver.has_value());
|
||||
auto missing = resolver->resolve("/missing.txt");
|
||||
Void::Testing::require(missing.has_value());
|
||||
Void::Testing::expect(!missing->has_value());
|
||||
}
|
||||
|
||||
Test rejects_symlinks_that_escape_the_document_root()
|
||||
{
|
||||
Void::FileSystem::TempDir root{"void_net_root_"};
|
||||
Void::FileSystem::TempDir outside{"void_net_outside_"};
|
||||
write_file(outside / "secret.txt", "secret");
|
||||
std::error_code error;
|
||||
std::filesystem::create_directory_symlink(
|
||||
outside.path(), root / "escape", error);
|
||||
if (error) {
|
||||
Void::Testing::skip(
|
||||
std::string_view{"directory symlinks are unavailable"});
|
||||
}
|
||||
auto resolver = StaticFileResolver::create(root.path());
|
||||
Void::Testing::require(resolver.has_value());
|
||||
Void::Testing::expect(
|
||||
!resolver->resolve("/escape/secret.txt").has_value());
|
||||
}
|
||||
};
|
||||
|
||||
struct WebSocketTests : Void::Testing::Suite<WebSocketTests> {
|
||||
Test lifecycle_callbacks_run_outside_registry_lock()
|
||||
{
|
||||
WebSocketSessions sessions;
|
||||
std::size_t opened = 0;
|
||||
std::string received;
|
||||
std::uint16_t close_code = 0;
|
||||
sessions.set_events(WebSocketEvents{
|
||||
.opened =
|
||||
[&](const auto& connection) {
|
||||
++opened;
|
||||
Void::Testing::expect(
|
||||
sessions.contains(connection->id()));
|
||||
},
|
||||
.text =
|
||||
[&](const auto& connection, std::string_view message) {
|
||||
Void::Testing::expect(
|
||||
sessions.find(connection->id()) != nullptr);
|
||||
received = message;
|
||||
},
|
||||
.closed =
|
||||
[&](WebSocketConnectionId,
|
||||
const WebSocketClose& close) {
|
||||
close_code = close.code;
|
||||
}});
|
||||
|
||||
auto connection = std::make_shared<FakeConnection>(7);
|
||||
auto accepted = sessions.open(connection);
|
||||
Void::Testing::require(accepted.has_value() && *accepted);
|
||||
Void::Testing::expect_eq(opened, static_cast<std::size_t>(1));
|
||||
Void::Testing::expect(sessions.receive_text({7}, "hello"));
|
||||
Void::Testing::expect_eq(received, std::string{"hello"});
|
||||
Void::Testing::expect(
|
||||
sessions.close({7}, WebSocketClose{1008, "policy"}));
|
||||
Void::Testing::expect(connection->closed);
|
||||
Void::Testing::expect_eq(connection->close_code,
|
||||
static_cast<std::uint16_t>(1008));
|
||||
Void::Testing::expect_eq(close_code,
|
||||
static_cast<std::uint16_t>(1008));
|
||||
Void::Testing::expect_eq(sessions.size(),
|
||||
static_cast<std::size_t>(0));
|
||||
}
|
||||
|
||||
Test duplicate_and_unknown_connections_are_ignored()
|
||||
{
|
||||
WebSocketSessions sessions;
|
||||
auto first = std::make_shared<FakeConnection>(9);
|
||||
auto duplicate = std::make_shared<FakeConnection>(9);
|
||||
auto accepted = sessions.open(first);
|
||||
auto rejected = sessions.open(duplicate);
|
||||
Void::Testing::require(accepted.has_value() && *accepted);
|
||||
Void::Testing::require(rejected.has_value());
|
||||
Void::Testing::expect(!*rejected);
|
||||
Void::Testing::expect(!sessions.receive_text({99}, "ignored"));
|
||||
Void::Testing::expect(!sessions.disconnect({99}));
|
||||
}
|
||||
|
||||
Test transport_disconnect_does_not_close_transport_again()
|
||||
{
|
||||
WebSocketSessions sessions;
|
||||
auto connection = std::make_shared<FakeConnection>(11);
|
||||
Void::Testing::require(*sessions.open(connection));
|
||||
Void::Testing::expect(
|
||||
sessions.disconnect({11}, {1000, "done"}));
|
||||
Void::Testing::expect(!connection->closed);
|
||||
}
|
||||
|
||||
Test shutdown_closes_every_connection_once()
|
||||
{
|
||||
WebSocketSessions sessions;
|
||||
auto first = std::make_shared<FakeConnection>(1);
|
||||
auto second = std::make_shared<FakeConnection>(2);
|
||||
Void::Testing::require(*sessions.open(first));
|
||||
Void::Testing::require(*sessions.open(second));
|
||||
Void::Testing::expect_eq(sessions.shutdown(),
|
||||
static_cast<std::size_t>(2));
|
||||
Void::Testing::expect(first->closed && second->closed);
|
||||
Void::Testing::expect_eq(sessions.shutdown(),
|
||||
static_cast<std::size_t>(0));
|
||||
auto after_shutdown =
|
||||
sessions.open(std::make_shared<FakeConnection>(3));
|
||||
Void::Testing::require(after_shutdown.has_value());
|
||||
Void::Testing::expect(!*after_shutdown);
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
@@ -0,0 +1,95 @@
|
||||
#include <Void/Security/Security.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Suite.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace
|
||||
{
|
||||
using Void::Security::PasswordHashStrength;
|
||||
using Void::Testing::Test;
|
||||
|
||||
struct SecurityTests : Void::Testing::Suite<SecurityTests> {
|
||||
Test random_tokens_are_url_safe_and_unique()
|
||||
{
|
||||
auto first = Void::Security::random_token();
|
||||
auto second = Void::Security::random_token();
|
||||
Void::Testing::require(first.has_value());
|
||||
Void::Testing::require(second.has_value());
|
||||
Void::Testing::expect_eq(first->size(),
|
||||
static_cast<std::size_t>(43));
|
||||
Void::Testing::expect(first->find_first_of("+/=") ==
|
||||
std::string::npos);
|
||||
Void::Testing::expect_ne(*first, *second);
|
||||
}
|
||||
|
||||
Test random_token_rejects_zero_entropy()
|
||||
{
|
||||
Void::Testing::expect(
|
||||
!Void::Security::random_token(0).has_value());
|
||||
}
|
||||
|
||||
Test token_hash_is_deterministic_and_verifies_in_constant_time()
|
||||
{
|
||||
auto first = Void::Security::hash_token("session-secret");
|
||||
auto second = Void::Security::hash_token("session-secret");
|
||||
Void::Testing::require(first.has_value());
|
||||
Void::Testing::require(second.has_value());
|
||||
Void::Testing::expect_eq(*first, *second);
|
||||
Void::Testing::expect_eq(first->size(),
|
||||
static_cast<std::size_t>(64));
|
||||
|
||||
auto valid =
|
||||
Void::Security::verify_token("session-secret", *first);
|
||||
auto invalid =
|
||||
Void::Security::verify_token("other-secret", *first);
|
||||
Void::Testing::require(valid.has_value());
|
||||
Void::Testing::require(invalid.has_value());
|
||||
Void::Testing::expect(*valid);
|
||||
Void::Testing::expect(!*invalid);
|
||||
}
|
||||
|
||||
Test token_verification_rejects_malformed_digest()
|
||||
{
|
||||
auto malformed =
|
||||
Void::Security::verify_token("token", "not-hex");
|
||||
Void::Testing::require(malformed.has_value());
|
||||
Void::Testing::expect(!*malformed);
|
||||
}
|
||||
|
||||
Test password_hash_uses_argon2id_and_random_salts()
|
||||
{
|
||||
auto first = Void::Security::hash_password(
|
||||
"correct horse battery staple",
|
||||
PasswordHashStrength::Interactive);
|
||||
auto second = Void::Security::hash_password(
|
||||
"correct horse battery staple",
|
||||
PasswordHashStrength::Interactive);
|
||||
Void::Testing::require(first.has_value());
|
||||
Void::Testing::require(second.has_value());
|
||||
Void::Testing::expect(first->starts_with("$argon2id$"));
|
||||
Void::Testing::expect_ne(*first, *second);
|
||||
}
|
||||
|
||||
Test password_verification_accepts_only_the_original_password()
|
||||
{
|
||||
auto encoded = Void::Security::hash_password(
|
||||
"correct horse battery staple",
|
||||
PasswordHashStrength::Interactive);
|
||||
Void::Testing::require(encoded.has_value());
|
||||
|
||||
auto valid = Void::Security::verify_password(
|
||||
"correct horse battery staple", *encoded);
|
||||
auto invalid =
|
||||
Void::Security::verify_password("incorrect", *encoded);
|
||||
auto malformed = Void::Security::verify_password(
|
||||
"correct horse battery staple", "$argon2id$broken");
|
||||
Void::Testing::require(valid.has_value());
|
||||
Void::Testing::require(invalid.has_value());
|
||||
Void::Testing::require(malformed.has_value());
|
||||
Void::Testing::expect(*valid);
|
||||
Void::Testing::expect(!*invalid);
|
||||
Void::Testing::expect(!*malformed);
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
Reference in new issue
Block a user