From 3642434a0b1b3dd223e7c2666eb00231858ee1b7 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Tue, 3 Jan 2023 15:48:29 +0100 Subject: Download test (WIP) --- tests/Makefile | 2 +- tests/test-webserver.cpp | 103 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) diff --git a/tests/Makefile b/tests/Makefile index 338800a..d4d047c 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -13,7 +13,7 @@ CXXFLAGS+=--coverage LDFLAGS+=--coverage endif -CXXFLAGS+= -I. -fPIE +CXXFLAGS+= -I. -I.. -fPIE CXXTESTFLAGS= diff --git a/tests/test-webserver.cpp b/tests/test-webserver.cpp index 1668c60..473e67d 100644 --- a/tests/test-webserver.cpp +++ b/tests/test-webserver.cpp @@ -10,12 +10,113 @@ #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; +class WebserverProcess +{ +public: + const fs::path testConfigFilename{"./webserver.conf"}; + WebserverProcess(): m_pid{} + { + File::setFile(testConfigFilename, R"CONFIG( + + www-data + www-data + 10 + ../plugins + + + localhost + ip6-localhost + localhost + 127.0.0.1 + [::1] + + static-files + . + + + + + +
127.0.0.1
+ 8080 + http + localhost +
+ +
::1
+ 8080 + http + localhost +
+
+
+ +)CONFIG"); + + start(); + } + + ~WebserverProcess() + { + stop(); + fs::remove(testConfigFilename); + } + + void start() + { + if (m_pid != 0) + throw std::runtime_error("Process already running, so it can't be started"); + + 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; + } + +private: + pid_t m_pid; +}; + class Fixture { public: @@ -25,4 +126,6 @@ public: BOOST_FIXTURE_TEST_CASE(http_download, Fixture) { + WebserverProcess serverProcess; } + -- cgit v1.2.3