diff options
author | Roland Reichwein <mail@reichwein.it> | 2023-01-03 15:48:29 +0100 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2023-01-03 15:48:29 +0100 |
commit | 3642434a0b1b3dd223e7c2666eb00231858ee1b7 (patch) | |
tree | b0acf7973e4c16a2ed8d8ca7f0082de2dda71701 /tests/test-webserver.cpp | |
parent | f30eba63cb3f5e3aa5d81d6b31d1ba2fdee1e5c4 (diff) |
Download test (WIP)
Diffstat (limited to 'tests/test-webserver.cpp')
-rw-r--r-- | tests/test-webserver.cpp | 103 |
1 files changed, 103 insertions, 0 deletions
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 <boost/property_tree/ptree.hpp> #include <boost/property_tree/xml_parser.hpp> +#include <chrono> +#include <filesystem> #include <sstream> #include <string> +#include <thread> + +#include <unistd.h> +#include <signal.h> +#include <sys/wait.h> + +#include <libreichwein/file.h> + +#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( +<webserver> + <user>www-data</user> + <group>www-data</group> + <threads>10</threads> + <plugin-directory>../plugins</plugin-directory> + <sites> + <site> + <name>localhost</name> + <host>ip6-localhost</host> + <host>localhost</host> + <host>127.0.0.1</host> + <host>[::1]</host> + <path requested="/"> + <plugin>static-files</plugin> + <target>.</target> + </path> + </site> + </sites> + <sockets> + <socket> + <address>127.0.0.1</address> + <port>8080</port> + <protocol>http</protocol> + <site>localhost</site> + </socket> + <socket> + <address>::1</address> + <port>8080</port> + <protocol>http</protocol> + <site>localhost</site> + </socket> + </sockets> +</webserver> + +)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; } + |