| 1 | // Copyright (c) 2014-2024, The Monero Project |
| 2 | // |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | // |
| 29 | |
| 30 | #include <atomic> |
| 31 | #include <boost/asio/connect.hpp> |
| 32 | #include <boost/asio/ip/tcp.hpp> |
| 33 | #include <boost/beast/core.hpp> |
| 34 | #include <boost/beast/http.hpp> |
| 35 | #include <boost/beast/version.hpp> |
| 36 | #include "gtest/gtest.h" |
| 37 | #include "net/http_server_handlers_map2.h" |
| 38 | #include "net/http_server_impl_base.h" |
| 39 | #include "storages/portable_storage_template_helper.h" |
| 40 | |
| 41 | namespace |
| 42 | { |
| 43 | constexpr const std::size_t payload_size = 26 * 1024 * 1024; |
| 44 | constexpr const std::size_t max_private_ips = 25; |
| 45 | struct dummy |
| 46 | { |
| 47 | struct request |
| 48 | { |
| 49 | BEGIN_KV_SERIALIZE_MAP() |
| 50 | END_KV_SERIALIZE_MAP() |
| 51 | }; |
| 52 | |
| 53 | struct response |
| 54 | { |
| 55 | BEGIN_KV_SERIALIZE_MAP() |
| 56 | KV_SERIALIZE(payload) |
| 57 | END_KV_SERIALIZE_MAP() |
| 58 | |
| 59 | std::string payload; |
| 60 | }; |
| 61 | }; |
| 62 | |
| 63 | std::string make_payload() |
| 64 | { |
| 65 | dummy::request body{}; |
| 66 | const auto body_serialized = epee::serialization::store_t_to_binary(body); |
| 67 | return std::string{ |
| 68 | reinterpret_cast<const char*>(body_serialized.data()), |
| 69 | body_serialized.size() |
| 70 | }; |
| 71 | } |
| 72 | |
| 73 | struct http_server : epee::http_server_impl_base<http_server> |
| 74 | { |
| 75 | using connection_context = epee::net_utils::connection_context_base; |
| 76 | |
| 77 | http_server() |
| 78 | : epee::http_server_impl_base<http_server>(), |
| 79 | dummy_size(payload_size) |
| 80 | {} |
| 81 | |
| 82 | CHAIN_HTTP_TO_MAP2(connection_context); //forward http requests to uri map |
| 83 | |
| 84 | BEGIN_URI_MAP2() |
| 85 | MAP_URI_AUTO_BIN2("/dummy", on_dummy, dummy) |
| 86 | END_URI_MAP2() |
| 87 | |
| 88 | bool on_dummy(const dummy::request&, dummy::response& res, const connection_context *ctx = NULL) |
| 89 | { |
| 90 | res.payload.resize(dummy_size.load(), 'f'); |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | std::atomic<std::size_t> dummy_size; |
| 95 | }; |
| 96 | } // anonymous |
| 97 | |
| 98 | TEST(http_server, response_soft_limit) |
| 99 | { |
| 100 | namespace http = boost::beast::http; |
| 101 | |
| 102 | http_server server{}; |
| 103 | server.init(nullptr, "8080"); |
| 104 | server.run(1, false); |
| 105 | |
| 106 | boost::system::error_code error{}; |
| 107 | boost::asio::io_context context{}; |
| 108 | boost::asio::ip::tcp::socket stream{context}; |
| 109 | stream.connect( |
| 110 | boost::asio::ip::tcp::endpoint{ |
| 111 | boost::asio::ip::make_address("127.0.0.1"), 8080 |
| 112 | }, |
| 113 | error |
| 114 | ); |
| 115 | EXPECT_FALSE(bool(error)); |
| 116 | |
| 117 | http::request<http::string_body> req{http::verb::get, "/dummy", 11}; |
| 118 | req.set(http::field::host, "127.0.0.1"); |
| 119 | req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING); |
| 120 | req.body() = make_payload(); |
| 121 | req.prepare_payload(); |
| 122 | http::write(stream, req, error); |
| 123 | EXPECT_FALSE(bool(error)); |
| 124 | |
| 125 | { |
| 126 | dummy::response payload{}; |
| 127 | boost::beast::flat_buffer buffer; |
| 128 | http::response<http::basic_string_body<char>> res; |
| 129 | http::read(stream, buffer, res, error); |
| 130 | EXPECT_FALSE(bool(error)); |
| 131 | EXPECT_EQ(200u, res.result_int()); |
| 132 | EXPECT_TRUE(epee::serialization::load_t_from_binary(payload, res.body())); |
| 133 | EXPECT_EQ(payload_size, std::count(payload.payload.begin(), payload.payload.end(), 'f')); |
| 134 | } |
| 135 | |
| 136 | while (!error) |
| 137 | http::write(stream, req, error); |
| 138 | server.send_stop_signal(); |
| 139 | } |
| 140 | |
| 141 | TEST(http_server, private_ip_limit) |
| 142 | { |
| 143 | namespace http = boost::beast::http; |
| 144 | |
| 145 | http_server server{}; |
| 146 | server.dummy_size = 1; |
| 147 | server.init(nullptr, "8080"); |
| 148 | server.run(1, false); |
| 149 | |
| 150 | boost::system::error_code error{}; |
| 151 | boost::asio::io_context context{}; |
| 152 | |
| 153 | http::request<http::string_body> req{http::verb::get, "/dummy", 11}; |
| 154 | req.set(http::field::host, "127.0.0.1"); |
| 155 | req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING); |
| 156 | req.body() = make_payload(); |
| 157 | req.prepare_payload(); |
| 158 | |
| 159 | std::vector<boost::asio::ip::tcp::socket> streams{}; |
| 160 | for (std::size_t i = 0; i < max_private_ips; ++i) |
| 161 | { |
| 162 | streams.emplace_back(context); |
| 163 | streams.back().connect( |
| 164 | boost::asio::ip::tcp::endpoint{ |
| 165 | boost::asio::ip::make_address("127.0.0.1"), 8080 |
| 166 | }, |
| 167 | error |
| 168 | ); |
| 169 | http::write(streams.back(), req, error); |
| 170 | EXPECT_FALSE(bool(error)); |
| 171 | |
| 172 | dummy::response payload{}; |
| 173 | boost::beast::flat_buffer buffer; |
| 174 | http::response<http::basic_string_body<char>> res; |
| 175 | |
| 176 | http::read(streams.back(), buffer, res, error); |
| 177 | EXPECT_FALSE(bool(error)); |
| 178 | } |
| 179 | |
| 180 | boost::asio::ip::tcp::socket stream{context}; |
| 181 | stream.connect( |
| 182 | boost::asio::ip::tcp::endpoint{ |
| 183 | boost::asio::ip::make_address("127.0.0.1"), 8080 |
| 184 | }, |
| 185 | error |
| 186 | ); |
| 187 | bool failed = bool(error); |
| 188 | http::write(stream, req, error); |
| 189 | failed |= bool(error); |
| 190 | { |
| 191 | dummy::response payload{}; |
| 192 | boost::beast::flat_buffer buffer; |
| 193 | http::response<http::basic_string_body<char>> res; |
| 194 | |
| 195 | // make sure server ran async_accept code |
| 196 | http::read(stream, buffer, res, error); |
| 197 | } |
| 198 | failed |= bool(error); |
| 199 | EXPECT_TRUE(failed); |
| 200 | } |
| 201 | |
| 202 | TEST(http_server, read_then_close) |
| 203 | { |
| 204 | namespace http = boost::beast::http; |
| 205 | |
| 206 | http_server server{}; |
| 207 | server.dummy_size = 200000; |
| 208 | server.init(nullptr, "8080"); |
| 209 | server.run(2, false); // need at least 2 threads to trigger issues |
| 210 | |
| 211 | bool failed_read = false; |
| 212 | bool closed_all_connections = true; |
| 213 | for (std::size_t j = 0; j < 1000; ++j) |
| 214 | { |
| 215 | boost::system::error_code error{}; |
| 216 | boost::asio::io_context context{}; |
| 217 | boost::asio::ip::tcp::socket stream{context}; |
| 218 | stream.connect( |
| 219 | boost::asio::ip::tcp::endpoint{ |
| 220 | boost::asio::ip::make_address("127.0.0.1"), 8080 |
| 221 | }, |
| 222 | error |
| 223 | ); |
| 224 | EXPECT_FALSE(bool(error)); |
| 225 | |
| 226 | http::request<http::string_body> req{http::verb::get, "/dummy", 11}; |
| 227 | req.set(http::field::host, "127.0.0.1"); |
| 228 | req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING); |
| 229 | req.set(http::field::connection, "close"); // tell server to close connection after sending all data to the client |
| 230 | req.body() = make_payload(); |
| 231 | req.prepare_payload(); |
| 232 | |
| 233 | dummy::response payload{}; |
| 234 | boost::beast::flat_buffer buffer; |
| 235 | http::response_parser<http::basic_string_body<char>> parser; |
| 236 | parser.body_limit(server.dummy_size + 1024); |
| 237 | |
| 238 | http::write(stream, req, error); |
| 239 | EXPECT_FALSE(bool(error)); |
| 240 | |
| 241 | http::read(stream, buffer, parser, error); |
| 242 | |
| 243 | // If the read fails, continue the loop still just to make sure the server can handle it |
| 244 | failed_read |= bool(error); |
| 245 | if (failed_read) |
| 246 | continue; |
| 247 | failed_read |= !(parser.is_done()); |
| 248 | if (failed_read) |
| 249 | continue; |
| 250 | const auto res = parser.release(); |
| 251 | failed_read |= res.result_int() != 200u |
| 252 | || !(epee::serialization::load_t_from_binary(payload, res.body())) |
| 253 | || (server.dummy_size != std::count(payload.payload.begin(), payload.payload.end(), 'f')); |
| 254 | |
| 255 | // See if the server closes the connection after handling the resp |
| 256 | char buf[1]; |
| 257 | stream.read_some(boost::asio::buffer(buf), error); |
| 258 | closed_all_connections &= error == boost::asio::error::eof; |
| 259 | } |
| 260 | |
| 261 | // The client should have been able to read all data sent by the server across all requests |
| 262 | EXPECT_FALSE(failed_read); |
| 263 | |
| 264 | // The server should have closed all connections |
| 265 | EXPECT_TRUE(closed_all_connections); |
| 266 | |
| 267 | server.send_stop_signal(); |
| 268 | } |