summaryrefslogtreecommitdiffhomepage
path: root/tests/test-webserver.cpp
blob: 473e67d1673b5b10888197738e3e3be82bcaba93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Main unit test compilation unit
// boost mandates that exactly one compilation unit contains the following two lines:
#define BOOST_TEST_MODULE webserver_test
#include <boost/test/included/unit_test.hpp>

#include <boost/test/data/dataset.hpp>
#include <boost/test/data/monomorphic.hpp>
#include <boost/test/data/test_case.hpp>

#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:
 Fixture(){}
 ~Fixture(){}
};

BOOST_FIXTURE_TEST_CASE(http_download, Fixture)
{
 WebserverProcess serverProcess;
}