summaryrefslogtreecommitdiffhomepage
path: root/tests/helper.cpp
blob: 66045fb08e5cd8a7eed91a16d77082f804a5b844 (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
#include "helper.h"

#include <libreichwein/stringhelper.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"};

// 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()};
}