summaryrefslogtreecommitdiffhomepage
path: root/tests/helper.cpp
blob: 644b9cadbe1980ff2499215f164f339989a68f4c (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "helper.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"};

// tcp: tcp or tcp6
bool tcp_is_pid_listening_on(const std::string& tcp, pid_t pid, int port)
{
 std::string filename{fmt::format("/proc/{}/net/{}", pid, tcp)};
 std::ifstream f{filename, std::ios::in};
 // e.g.:
 //   sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode
 //    0: 00000000:C799 00000000:0000 0A 00000000:00000000 00:00000000 00000000   107        0 21869 1 00000000335416a4 100 0 0 10 0
 std::string s;
 std::getline(f, s); // skip head line
 while (std::getline(f, s)) {
  boost::algorithm::trim_left(s);

  size_t pos_space1{s.find(' ')};
  if (pos_space1 == std::string::npos)
   throw std::runtime_error("Expected first space in " + filename);

  size_t pos_colon1{s.find(':', pos_space1 + 1)};
  if (pos_colon1 == std::string::npos)
   throw std::runtime_error("Expected first colon in " + filename);
  
  size_t pos_space2{s.find(' ', pos_colon1 + 1)};
  if (pos_space2 == std::string::npos)
   throw std::runtime_error("Expected second space in " + filename);

  std::string port_s{s.substr(pos_colon1 + 1, pos_space2 - (pos_colon1 + 1))};
  auto current_port{std::stoul(port_s, nullptr, 16)};
  if (current_port != port)
   continue;

  // now, we are in a line related to matching local port
  
  size_t pos_space3{s.find(' ', pos_space2 + 1)};
  if (pos_space3 == std::string::npos)
   throw std::runtime_error("Expected third space in " + filename);

  size_t pos_space4{s.find(' ', pos_space3 + 1)};
  if (pos_space4 == std::string::npos)
   throw std::runtime_error("Expected fourth space in " + filename);

  std::string state_s{s.substr(pos_space3 + 1, pos_space4 - (pos_space3 + 1))};
  if (state_s == "0A") // listening state TCP_LISTEN, from net/tcp_states.h
   return true;
 }

 return false; // not found
}

bool is_pid_listening_on(pid_t pid, int port)
{
 return tcp_is_pid_listening_on("tcp", pid, port) || tcp_is_pid_listening_on("tcp6", pid, port);
}

void wait_for_pid_listening_on(pid_t pid, int port)
{
 while (!is_pid_listening_on(pid, port)) {
  std::this_thread::sleep_for(std::chrono::milliseconds(10));
 }
}

// returns -1 if no port found in config
int port_from_config(const std::string& config)
{
 pt::ptree tree;
 std::istringstream stream{config};
 pt::read_xml(stream, tree);
 try {
  return tree.get<int>("webserver.sockets.socket.port");
 } catch(...) {
  return -1;
 }
}

std::pair<std::string,std::string> HTTP(const std::string& target, bool ipv6, bool HTTP11, boost::beast::http::verb method)
{
 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<boost::beast::http::string_body> 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<boost::beast::http::dynamic_body> 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<std::string,std::string> HTTPS(const std::string& target, bool ipv6, bool HTTP11, boost::beast::http::verb method)
{
 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<boost::beast::tcp_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<int>(::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<boost::beast::http::string_body> 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<boost::beast::http::dynamic_body> 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()};
}