Files
Library/tests/standalone/Net/Server.cpp
T
faumaray 8583a479dd
CI / GCC 16 / C++26 / default (push) Has been cancelled
CI / GCC 16 / C++26 / sanitizers (push) Has been cancelled
CI / GCC 16 / C++26 / werror (push) Has been cancelled
Update
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-15 19:49:06 +03:00

331 lines
11 KiB
C++

#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