// SPDX-License-Identifier: Apache-2.0 OR CECILL-B // Author: Ambroise Leclerc // Created: 2026-10-02 // // WebFront — tests/unit // Role: Socket mock driving a WebLink from the test thread, with injectable write failures. // Thread: bridge-io | Alloc: unrestricted // LLM: test helper; senders complete when the test says so #pragma once #include "InlineTaskScope.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace webfront::bdd; namespace webfront::testing { /// A sender completed by the test rather than by the network: `start` receives the means to complete or /// stop the operation, and decides when to use them. class ManualSender { public: struct Completion { std::function complete; std::function stop; }; using sender_concept = networking::ex::sender_t; using completion_signatures = networking::ex:: completion_signatures; explicit ManualSender(std::function start) : onStart(std::move(start)) { } // Completed by the test, or stopped by the stop request of the awaiting task, whichever comes first. template struct Operation { using StopToken = networking::ex::stop_token_of_t>; struct OnStop { Operation* operation; void operator()() noexcept { operation->finish([](Receiver& receiver) { networking::ex::set_stopped(std::move(receiver)); }); } }; Receiver receiver; std::function onStart; std::shared_ptr finished = std::make_shared(false); // outlives the operation std::optional> stopCallback; Operation(Receiver r, std::function start) : receiver(std::move(r)), onStart(std::move(start)) { } void start() noexcept { auto token = networking::ex::get_stop_token(networking::ex::get_env(receiver)); if (token.stop_requested()) { finish([](Receiver& r) { networking::ex::set_stopped(std::move(r)); }); return; // completing may destroy this operation: touch no member afterwards } stopCallback.emplace(std::move(token), OnStop{this}); onStart({[this, done = finished](std::error_code error, std::size_t size) { if (*done) return; finish([&](Receiver& r) { if (error) networking::ex::set_error(std::move(r), error); else networking::ex::set_value(std::move(r), size); }); }, [this, done = finished] { if (!*done) finish([](Receiver& r) { networking::ex::set_stopped(std::move(r)); }); }}); } void finish(auto signal) { if (*finished) return; *finished = true; stopCallback.reset(); signal(receiver); } }; template Operation connect(Receiver receiver) const { return Operation(std::move(receiver), onStart); } private: std::function onStart; }; /// Reads are injected with receive()/fail(). Writes complete synchronously, inside the calling /// write, unless deferWrites() queues their completions for completeDeferredWrites(). class InjectableSocket : public networking::SocketBaseMock { public: enum shutdown_type { shutdown_receive, shutdown_send, shutdown_both }; std::size_t write_some(auto input, std::error_code& error) { std::lock_guard lock(mutex); if (writeError) { error = writeError; return 0; } if (discarding) { discarded += input.size(); return input.size(); } const auto* first = static_cast(input.data()); written.insert(written.end(), first, first + input.size()); return input.size(); } void close() { } void shutdown(shutdown_type) { ++shutdowns; } /// The peer address of the next accepted connection, as the server reads it for trustLoopbackClients. static inline std::atomic peerIsLoopback{false}; struct PeerAddress { bool loopback; bool is_loopback() const { return loopback; } }; struct PeerEndpoint { bool loopback; PeerAddress address() const { return {loopback}; } }; PeerEndpoint remote_endpoint(std::error_code&) const { return {peerIsLoopback.load()}; } /// The read the WebSocket is waiting for: the test completes it, or the task is stopped. static ManualSender read(networking::buffers::MutableBuffer buffer) { return ManualSender([buffer](ManualSender::Completion completion) { ++readCalls; readBuffer = buffer; readCompletion = std::move(completion); }); } static void receive(std::span bytes) { require(readCompletion.complete); require(bytes.size() <= readBuffer.size()); std::copy(bytes.begin(), bytes.end(), static_cast(readBuffer.data())); std::exchange(readCompletion, {}).complete({}, bytes.size()); } /// An error ends the read; no error is a read of zero bytes, the end of the stream. static void fail(std::error_code error) { require(readCompletion.complete); std::exchange(readCompletion, {}).complete(error, 0); } /// Stops a pending read, as the WebSocket does when it is stopped. static void reset() { if (readCompletion.stop) std::exchange(readCompletion, {}).stop(); std::lock_guard lock(mutex); written.clear(); writeError = {}; deferring = false; deferred.clear(); readCalls = 0; shutdowns = 0; discarding = false; discarded = 0; } /// How many times the server shut a socket down: the end of a connection it answered. static int shutdownCount() { return shutdowns; } /// Everything the server wrote. static std::string writtenText() { std::lock_guard lock(mutex); return {reinterpret_cast(written.data()), written.size()}; } /// Counts the written bytes instead of keeping them, to send more than the test may hold. static void discardWrites() { std::lock_guard lock(mutex); discarding = true; } static std::uint64_t discardedBytes() { return discarded; } static int readCallCount() { return readCalls; } static bool hasWrittenData() { std::lock_guard lock(mutex); return !written.empty(); } /// True when the server sent these bytes, e.g. the text of a debugLog command. static bool wrote(std::string_view text) { std::lock_guard lock(mutex); const auto* first = reinterpret_cast(text.data()); return std::search(written.begin(), written.end(), first, first + text.size()) != written.end(); } /// Where the server first sent these bytes, or npos when it did not. static std::size_t writtenAt(std::string_view text) { std::lock_guard lock(mutex); const auto* first = reinterpret_cast(text.data()); const auto found = std::search(written.begin(), written.end(), first, first + text.size()); return found == written.end() ? std::string_view::npos : static_cast(found - written.begin()); } static void failWrites(std::error_code error) { std::lock_guard lock(mutex); writeError = error; } static void deferWrites(bool defer = true) { std::lock_guard lock(mutex); deferring = defer; } /// A deferred send has registered cancellation and handed its completion to the test. static bool hasDeferredWrites() { std::lock_guard lock(mutex); return !deferred.empty(); } /// Run queued completions on the calling thread, outside the write that queued them. static std::size_t completeDeferredWrites() { std::vector> pending; { std::lock_guard lock(mutex); pending.swap(deferred); } for (auto& completion : pending) completion(); return pending.size(); } /// The write of these buffers: complete at once, or when completeDeferredWrites() runs. static ManualSender write(const auto& buffers) { std::error_code error; std::size_t transferred = 0; InjectableSocket socket; for (const auto& buffer : buffers) { transferred += socket.write_some(buffer, error); if (error) break; } return ManualSender([error, transferred](ManualSender::Completion completion) { { std::lock_guard lock(mutex); if (deferring) { deferred.push_back([completion, error, transferred] { completion.complete(error, transferred); }); return; } } completion.complete(error, transferred); }); } private: inline static networking::buffers::MutableBuffer readBuffer; inline static ManualSender::Completion readCompletion; inline static std::mutex mutex; inline static std::vector written; inline static std::error_code writeError; inline static bool deferring = false; inline static std::vector> deferred; inline static int readCalls = 0; inline static std::atomic shutdowns{0}; inline static bool discarding = false; inline static std::atomic discarded{0}; }; class InjectableNetworking : public networking::NetworkingMock { public: using Socket = InjectableSocket; static ManualSender Receive(Socket&, MutableBuffer buffer) { return InjectableSocket::read(buffer); } static ManualSender Send(Socket&, const auto& buffers) { return InjectableSocket::write(buffers); } }; /// Mask a browser-to-server frame: by default a binary message in one frame (FIN set, opcode 2). inline std::vector clientFrame(std::span payload, std::byte finAndOpcode = std::byte{0x82}) { require(payload.size() < 126); constexpr std::array mask{std::byte{0x12}, std::byte{0x34}, std::byte{0x56}, std::byte{0x78}}; std::vector frame{finAndOpcode, std::byte{static_cast(0x80u | payload.size())}}; frame.insert(frame.end(), mask.begin(), mask.end()); for (std::size_t index = 0; index < payload.size(); ++index) frame.push_back(payload[index] ^ mask[index % mask.size()]); return frame; } template std::vector messagePayload(const websocket::Frame& frame) { std::vector payload; const auto buffers = frame.toBuffers(); for (auto buffer = std::next(buffers.begin()); buffer != buffers.end(); ++buffer) { const auto* first = static_cast(buffer->data()); payload.insert(payload.end(), first, first + buffer->size()); } return payload; } } // namespace webfront::testing