//------------------------------------------------------------------------------ /* This file is part of clio: https://github.com/XRPLF/clio Copyright (c) 2024, the clio developers. Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ //============================================================================== #include "util/TestWsServer.hpp" #include "util/Expected.hpp" #include "util/requests/Types.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace asio = boost::asio; namespace websocket = boost::beast::websocket; TestWsConnection::TestWsConnection(websocket::stream wsStream) : ws_(std::move(wsStream)) { } std::optional TestWsConnection::send(std::string const& message, boost::asio::yield_context yield) { boost::beast::error_code errorCode; ws_.async_write(asio::buffer(message), yield[errorCode]); if (errorCode) return errorCode.message(); return std::nullopt; } std::optional TestWsConnection::receive(boost::asio::yield_context yield) { boost::beast::error_code errorCode; boost::beast::flat_buffer buffer; ws_.async_read(buffer, yield[errorCode]); if (errorCode == websocket::error::closed) return std::nullopt; [&]() { ASSERT_FALSE(errorCode) << errorCode.message(); }(); return boost::beast::buffers_to_string(buffer.data()); } std::optional TestWsConnection::close(boost::asio::yield_context yield) { boost::beast::error_code errorCode; ws_.async_close(websocket::close_code::normal, yield[errorCode]); if (errorCode) return errorCode.message(); return std::nullopt; } TestWsServer::TestWsServer(asio::io_context& context, std::string const& host, int port) : acceptor_(context) { auto endpoint = asio::ip::tcp::endpoint(boost::asio::ip::make_address(host), port); acceptor_.open(endpoint.protocol()); acceptor_.set_option(asio::socket_base::reuse_address(true)); acceptor_.bind(endpoint); } util::Expected TestWsServer::acceptConnection(asio::yield_context yield) { acceptor_.listen(asio::socket_base::max_listen_connections); boost::beast::error_code errorCode; asio::ip::tcp::socket socket(acceptor_.get_executor()); acceptor_.async_accept(socket, yield[errorCode]); if (errorCode) return util::Unexpected{util::requests::RequestError{"Accept error", errorCode}}; boost::beast::websocket::stream ws(std::move(socket)); ws.set_option(websocket::stream_base::timeout::suggested(boost::beast::role_type::server)); ws.async_accept(yield[errorCode]); if (errorCode) return util::Unexpected{util::requests::RequestError{"Handshake error", errorCode}}; return TestWsConnection(std::move(ws)); } void TestWsServer::acceptConnectionAndDropIt(asio::yield_context yield) { acceptConnectionWithoutHandshake(yield); } boost::asio::ip::tcp::socket TestWsServer::acceptConnectionWithoutHandshake(boost::asio::yield_context yield) { acceptor_.listen(asio::socket_base::max_listen_connections); boost::beast::error_code errorCode; asio::ip::tcp::socket socket(acceptor_.get_executor()); acceptor_.async_accept(socket, yield[errorCode]); [&]() { ASSERT_FALSE(errorCode) << errorCode.message(); }(); return socket; }