// Main unit test compilation unit // boost mandates that exactly one compilation unit contains the following two lines: #define BOOST_TEST_MODULE webserver_test #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "webserver.h" using namespace std::string_literals; namespace fs = std::filesystem; namespace pt = boost::property_tree; using namespace boost::unit_test; using namespace Reichwein; const fs::path testConfigFilename{"./webserver.conf"}; const fs::path testCertFilename{"./testchain.pem"}; const fs::path testKeyFilename{"./testkey.pem"}; class WebserverProcess { public: WebserverProcess(): m_pid{} { File::setFile(testConfigFilename, R"CONFIG( www-data www-data 10 stats.db ../plugins localhost ip6-localhost localhost 127.0.0.1 [::1] static-files . testchain.pem testkey.pem
127.0.0.1
8080 http localhost
::1
8080 http localhost
127.0.0.1
8081 https localhost
::1
8081 https localhost
)CONFIG"); // test self signed certificate File::setFile(testCertFilename, R"(-----BEGIN CERTIFICATE----- MIIC4zCCAcugAwIBAgIUeS9y+EsFWxf+foEx6SJ/R56rmX8wDQYJKoZIhvcNAQEL BQAwADAgFw0yMzAxMDYxNzIwNTFaGA8yMDUwMDUyNDE3MjA1MVowADCCASIwDQYJ KoZIhvcNAQEBBQADggEPADCCAQoCggEBALiZSICAcXng9j7zAb873U4TpuzvRVfh xS3gEhxqNPs6+ZQ43nAxDSdafzfGxpTkElTt/REj4oEOLw+QWI/jfbe4gDRDzf6V ij0fVuzp02JtJSS+dNrLv17NufBydOyD8oDrPehVrPlrZQhhkYMvLHAim+wikT2O s0es2R+avixxAZvx5EYgHba9T7R/pC/lA4BI3lEbVKjDA83hZvjPH1YdK+RYQS2g Jygdhe8qOSswXIwFAF3MMBpwRD3mz+vAJZP3lpBGsn+asO6Xd/5cjC8msgomS8Ji c9DMMNlrE1WU73wVG9n0OJcke2XEtzARVKJLlBPsug4oxDev6O4GakkCAwEAAaNT MFEwHQYDVR0OBBYEFE4i7Gtyn30qpIkH6f0/wuFA45pjMB8GA1UdIwQYMBaAFE4i 7Gtyn30q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RaZwSq788i/d8oTSeCk93TDF+GHhUq6ymkORACj76ws 8ohfoNQIG1VhdcTK2GOQjqctEFUm54t9N1nxD6VavMu5DlSSVsPTDbYuE5U4cy3T ThDtoYJgwz5KRflklwl3xoDJVx3B5wMUaqviRp6l0QKBgQC6MPG+EV2drsHwG0Wg gnP4uCSuFAfWBlHAQyZv5PMQBNfM8YjyMyL+O7cggGNJSOZr/X30EqoBe+LXrL3X Gtix8F1Ed0fbAarAgxIwq8MktzmastDq4XS+zwYPZ7UTbmbqvT3VYPga4Sh90fyY nPJpqZvhvGzQX22yeHS7vTSQOQKBgQD9z36EIYMuLl0HJK6gfjGHsy/Rx7bw1TmP aHmuF8Ra7rpDSOym0ImKWTOLEoLlQUsMz/FuVLCGP/ACjMFKsqh3Zy/0hVJOMDMR Z+ODT28Hcz4AMcTYDvcTYd70HhhZL+/eFCVk8Nk164saMuhifAkOgvwfaYs0m3ue S9jxlZKKkQKBgHgBzf6k8MMOfaAF4/XVv2wDPFkbPgW74vtaDK84UVX02ScWUx9Q yHA3Cwye09/LZgEazREA6qS0NfyvMVkwy5S9CVB01VKam3UjxhiqzMegdTd5o+CQ WpAVnaFWRcb1dM4+FVmv+5pPn6qhKv8uwaxLDtcLfNM9ftX2f77176g5AoGADWtQ DBpdfi6TWpJU7UVexwbxS00c3gTYAz4J2OuGxSwECxSq9nLmIrtunza+VvKpziac ZDH0F1UAEpJwkct6Xr3E6k+2N04TFSOCAupLO4CbUZVQDABWjd7J0+xXaze+neZA x+J4CYLHmv4ADVzzeaHxRJPm+UQTOB5YfQVkdxECgYBv3QuUMiBGKWgeheP4nAFU SVgqGBQwAtqb5DR1YVJ4LFPt+jyrQMby6mqSlzENYcidSP3Ogn22CvST+bAjbf6D D/ae1zeOHBls00ILHANv1Z/hXcEkiKnZdeP6O43xBfCS+Lps5daXgUbC0kw2R09S VZTqPHmb+db0rFA3XlAg2A== -----END PRIVATE KEY----- )"); start(); } ~WebserverProcess() { stop(); fs::remove(testConfigFilename); fs::remove(testCertFilename); fs::remove(testKeyFilename); } void start() { if (m_pid != 0) throw std::runtime_error("Process already running, so it can't be started"); // connect stdout of new child process to stream of parent, via pipe m_pid = fork(); if (m_pid < 0) throw std::runtime_error("Fork unsuccessful."); if (m_pid == 0) { // child process branch char* argv[] = {(char*)"webserver", (char*)"-c", (char*)"./webserver.conf"}; webserver(sizeof(argv) / sizeof(char*), argv); exit(0); } // wait for server to start up std::this_thread::sleep_for(std::chrono::milliseconds(100)); } void stop() { if (m_pid == 0) throw std::runtime_error("Process not running, so it can't be stopped"); if (kill(m_pid, SIGTERM) != 0) throw std::runtime_error("Unable to kill process"); if (int result = waitpid(m_pid, NULL, 0); result != m_pid) throw std::runtime_error("waitpid returned "s + std::to_string(result)); m_pid = 0; m_is = 0; m_filebuf = 0; } bool is_running() { if (m_pid == 0) return false; return Reichwein::Process::is_running(m_pid); } std::string output() { if (!is_running()) throw std::runtime_error("No output/stdout available from webserver since it is not running"); if (!m_is) throw std::runtime_error("Webserver stdout stream not initialized."); std::stringstream result; std::string buffer(static_cast(1024), '\0'); int size{}; while ((size = m_is->readsome(buffer.data(), buffer.size())) > 0) result << buffer.substr(0, size); return result.str(); } private: pid_t m_pid; // child stdout std::shared_ptr<__gnu_cxx::stdio_filebuf> m_filebuf; std::shared_ptr m_is; }; // class WebserverProcess std::pair HTTP(const std::string& target, bool ipv6 = true, bool HTTP11 = true, boost::beast::http::verb method = boost::beast::http::verb::get) { auto const host = ipv6 ? "::1" : "127.0.0.1"; auto const port = "8080"; int version = HTTP11 ? 11 : 10; // The io_context is required for all I/O boost::asio::io_context ioc; // These objects perform our I/O boost::asio::ip::tcp::resolver resolver(ioc); boost::beast::tcp_stream stream(ioc); // Look up the domain name auto const results = resolver.resolve(host, port); // Make the connection on the IP address we get from a lookup stream.connect(results); // Set up an HTTP GET request message boost::beast::http::request req; req.method(method); req.target(target); req.version(version); req.set(boost::beast::http::field::host, host == "::1"s ? "["s + host + "]"s : host); req.set(boost::beast::http::field::user_agent, "Webserver Testsuite"); // Send the HTTP request to the remote host boost::beast::http::write(stream, req); // This buffer is used for reading and must be persisted boost::beast::flat_buffer buffer; // Declare a container to hold the response boost::beast::http::response res; // Receive the HTTP response boost::beast::http::read(stream, buffer, res); // Return value std::ostringstream header_stream; header_stream << res.base(); std::ostringstream body_stream; body_stream << boost::beast::buffers_to_string(res.body().data()); // Gracefully close the socket boost::beast::error_code ec; stream.socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec); // not_connected happens sometimes // so don't bother reporting it. // if (ec && ec != boost::beast::errc::not_connected) throw boost::beast::system_error{ec}; return {header_stream.str(), body_stream.str()}; } void load_root_certificates(boost::asio::ssl::context& ctx) { std::string cert_chain{File::getFile(testCertFilename)}; ctx.add_certificate_authority(boost::asio::buffer(cert_chain.data(), cert_chain.size())); } std::pair HTTPS(const std::string& target, bool ipv6 = true, bool HTTP11 = true, boost::beast::http::verb method = boost::beast::http::verb::get) { auto const host = ipv6 ? "::1" : "127.0.0.1"; auto const port = "8081"; int version = HTTP11 ? 11 : 10; // The io_context is required for all I/O boost::asio::io_context ioc; // The SSL context is required, and holds certificates boost::asio::ssl::context ctx(boost::asio::ssl::context::tlsv13_client); // This holds the root certificate used for verification load_root_certificates(ctx); // Verify the remote server's certificate ctx.set_verify_mode(boost::asio::ssl::verify_peer); // These objects perform our I/O boost::asio::ip::tcp::resolver resolver(ioc); boost::beast::ssl_stream stream(ioc, ctx); // Set SNI Hostname (many hosts need this to handshake successfully) if (!SSL_set_tlsext_host_name(stream.native_handle(), host)) { boost::beast::error_code ec{static_cast(::ERR_get_error()), boost::asio::error::get_ssl_category()}; throw boost::beast::system_error{ec}; } // Look up the domain name auto const results = resolver.resolve(host, port); // Make the connection on the IP address we get from a lookup boost::beast::get_lowest_layer(stream).connect(results); // Perform the SSL handshake stream.handshake(boost::asio::ssl::stream_base::client); // Set up an HTTP GET request message boost::beast::http::request req; req.method(method); req.target(target); req.version(version); req.set(boost::beast::http::field::host, host == "::1"s ? "["s + host + "]"s : host); req.set(boost::beast::http::field::user_agent, "Webserver Testsuite"); // Send the HTTP request to the remote host boost::beast::http::write(stream, req); // This buffer is used for reading and must be persisted boost::beast::flat_buffer buffer; // Declare a container to hold the response boost::beast::http::response res; // Receive the HTTP response boost::beast::http::read(stream, buffer, res); // Return value std::ostringstream header_stream; header_stream << res.base(); std::ostringstream body_stream; body_stream << boost::beast::buffers_to_string(res.body().data()); // Gracefully close the stream boost::beast::error_code ec; stream.shutdown(ec); if (ec == boost::asio::error::eof) { // Rationale: // http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error ec = {}; } if (ec) throw boost::beast::system_error{ec}; return {header_stream.str(), body_stream.str()}; } class Fixture { public: Fixture(){} ~Fixture() { fs::remove("stats.db"); } }; BOOST_DATA_TEST_CASE_F(Fixture, http_get, data::make({false, true}) * data::make({false, true}) * data::make({false, true}) * data::make({boost::beast::http::verb::head, boost::beast::http::verb::get}), ipv6, http11, https, method) { WebserverProcess serverProcess; BOOST_REQUIRE(serverProcess.is_running()); std::pair response{https ? HTTPS("/webserver.conf", ipv6, http11, method) : HTTP("/webserver.conf", ipv6, http11, method)}; BOOST_REQUIRE(serverProcess.is_running()); std::string::size_type size{File::getFile(testConfigFilename).size()}; BOOST_CHECK_GT(size, 0); BOOST_REQUIRE_EQUAL(response.first, fmt::format("HTTP/{} 200 OK\r\nServer: Reichwein.IT Webserver " VERSION "\r\nContent-Type: application/text\r\nContent-Length: {}\r\n\r\n", http11 ? "1.1" : "1.0", method == boost::beast::http::verb::head ? 0 : size)); BOOST_REQUIRE_EQUAL(response.second, method == boost::beast::http::verb::head ? ""s : File::getFile(testConfigFilename)); for (int i = 0; i < 10; i++) { std::pair response{https ? HTTPS("/webserver.conf", ipv6, http11, method) : HTTP("/webserver.conf", ipv6, http11, method)}; BOOST_REQUIRE(serverProcess.is_running()); BOOST_REQUIRE_EQUAL(response.first, fmt::format("HTTP/{} 200 OK\r\nServer: Reichwein.IT Webserver " VERSION "\r\nContent-Type: application/text\r\nContent-Length: {}\r\n\r\n", http11 ? "1.1" : "1.0", method == boost::beast::http::verb::head ? 0 : size)); BOOST_REQUIRE_EQUAL(response.second, method == boost::beast::http::verb::head ? ""s : File::getFile(testConfigFilename)); } } BOOST_DATA_TEST_CASE_F(Fixture, http_get_file_not_found, data::make({false, true}) * data::make({false, true}) * data::make({false, true}) * data::make({boost::beast::http::verb::head, boost::beast::http::verb::get}), ipv6, http11, https, method) { WebserverProcess serverProcess; BOOST_REQUIRE(serverProcess.is_running()); BOOST_REQUIRE(!fs::exists("./webserver.confSUFFIX")); auto response{(https ? HTTPS("/webserver.confSUFFIX", ipv6, http11, method) : HTTP("/webserver.confSUFFIX", ipv6, http11, method))}; BOOST_REQUIRE(serverProcess.is_running()); BOOST_REQUIRE_EQUAL(response.first, fmt::format("HTTP/{} 404 Not Found\r\nServer: Reichwein.IT Webserver " VERSION "\r\nContent-Type: text/html\r\nContent-Length: {}\r\n\r\n", http11 ? "1.1" : "1.0", method == boost::beast::http::verb::head ? 0 : 36)); BOOST_REQUIRE_EQUAL(response.second, method == boost::beast::http::verb::head ? "" : "404 Not found: /webserver.confSUFFIX"); } // Test server class WebsocketServerProcess { public: WebsocketServerProcess() { start(); } ~WebsocketServerProcess() { stop(); } // Echoes back all received WebSocket messages void do_session(boost::asio::ip::tcp::socket socket) { try { // Construct the stream by moving in the socket boost::beast::websocket::stream ws{std::move(socket)}; // Set a decorator to change the Server of the handshake ws.set_option(boost::beast::websocket::stream_base::decorator( [](boost::beast::websocket::response_type& res) { res.set(boost::beast::http::field::server, std::string("Reichwein.IT Test Websocket Server")); })); // Accept the websocket handshake ws.accept(); for(;;) { // This buffer will hold the incoming message boost::beast::flat_buffer buffer; // Read a message ws.read(buffer); // Echo the message back ws.text(ws.got_text()); std::string data(boost::asio::buffers_begin(buffer.data()), boost::asio::buffers_end(buffer.data())); data += ": " + std::to_string(m_count++); buffer.consume(buffer.size()); boost::beast::ostream(buffer) << data; ws.write(buffer.data()); } } catch(boost::beast::system_error const& se) { // This indicates that the session was closed if(se.code() != boost::beast::websocket::error::closed) std::cerr << "Error: " << se.code().message() << std::endl; } catch(std::exception const& e) { std::cerr << "Error: " << e.what() << std::endl; } } bool is_running() { if (m_pid == 0) return false; return Reichwein::Process::is_running(m_pid); } void start() { if (m_pid != 0) throw std::runtime_error("Process already running, so it can't be started"); // connect stdout of new child process to stream of parent, via pipe m_pid = fork(); if (m_pid < 0) throw std::runtime_error("Fork unsuccessful."); if (m_pid == 0) { // child process branch try { auto const address = boost::asio::ip::make_address("::1"); auto const port = static_cast(9876); // The io_context is required for all I/O boost::asio::io_context ioc{1}; // The acceptor receives incoming connections boost::asio::ip::tcp::acceptor acceptor{ioc, {address, port}}; for(;;) { // This will receive the new connection boost::asio::ip::tcp::socket socket{ioc}; // Block until we get a connection acceptor.accept(socket); // Launch the session, transferring ownership of the socket std::thread( &WebsocketServerProcess::do_session, this, std::move(socket)).detach(); } } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; } exit(0); } std::this_thread::sleep_for(std::chrono::milliseconds(100)); } void stop() { if (!is_running()) throw std::runtime_error("Process not running, so it can't be stopped"); if (kill(m_pid, SIGTERM) != 0) throw std::runtime_error("Unable to kill process"); if (int result = waitpid(m_pid, NULL, 0); result != m_pid) throw std::runtime_error("waitpid returned "s + std::to_string(result)); m_pid = 0; } private: int m_pid{}; int m_count{}; }; // class WebsocketServerProcess BOOST_FIXTURE_TEST_CASE(websocket, Fixture) { WebserverProcess serverProcess; BOOST_REQUIRE(serverProcess.is_running()); WebsocketServerProcess websocketProcess; BOOST_REQUIRE(websocketProcess.is_running()); std::string host = "::1"; auto const port = "8081" ; auto const text = "request1"; // The io_context is required for all I/O boost::asio::io_context ioc; // The SSL context is required, and holds certificates boost::asio::ssl::context ctx{boost::asio::ssl::context::tlsv13_client}; // This holds the root certificate used for verification load_root_certificates(ctx); // These objects perform our I/O boost::asio::ip::tcp::resolver resolver{ioc}; boost::beast::websocket::stream> ws{ioc, ctx}; // Look up the domain name auto const results = resolver.resolve(host, port); // Make the connection on the IP address we get from a lookup auto ep = boost::asio::connect(get_lowest_layer(ws), results); // Set SNI Hostname (many hosts need this to handshake successfully) if(! SSL_set_tlsext_host_name(ws.next_layer().native_handle(), host.c_str())) throw boost::beast::system_error( boost::beast::error_code( static_cast(::ERR_get_error()), boost::asio::error::get_ssl_category()), "Failed to set SNI Hostname"); // Update the host_ string. This will provide the value of the // Host HTTP header during the WebSocket handshake. // See https://tools.ietf.org/html/rfc7230#section-5.4 host += ':' + std::to_string(ep.port()); // Perform the SSL handshake ws.next_layer().handshake(boost::asio::ssl::stream_base::client); // Set a decorator to change the User-Agent of the handshake ws.set_option(boost::beast::websocket::stream_base::decorator( [](boost::beast::websocket::request_type& req) { req.set(boost::beast::http::field::user_agent, std::string("Reichwein.IT Test Websocket Client")); })); // Perform the websocket handshake ws.handshake(host, "/"); // Send the message ws.write(boost::asio::buffer(std::string(text))); // This buffer will hold the incoming message boost::beast::flat_buffer buffer; // Read a message into our buffer ws.read(buffer); std::string data(boost::asio::buffers_begin(buffer.data()), boost::asio::buffers_end(buffer.data())); BOOST_CHECK_EQUAL(data, "request1: 0"); buffer.consume(buffer.size()); ws.write(boost::asio::buffer(std::string(text))); ws.read(buffer); data = std::string(boost::asio::buffers_begin(buffer.data()), boost::asio::buffers_end(buffer.data())); BOOST_CHECK_EQUAL(data, "request1: 1"); buffer.consume(buffer.size()); ws.write(boost::asio::buffer(std::string(text))); ws.read(buffer); data = std::string(boost::asio::buffers_begin(buffer.data()), boost::asio::buffers_end(buffer.data())); BOOST_CHECK_EQUAL(data, "request1: 2"); // Close the WebSocket connection ws.close(boost::beast::websocket::close_code::normal); BOOST_REQUIRE(serverProcess.is_running()); BOOST_REQUIRE(websocketProcess.is_running()); }