| 1 | // Copyright (c) 2014-2022, 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 | // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers |
| 30 | |
| 31 | #include <boost/asio/post.hpp> |
| 32 | #include <boost/chrono/chrono.hpp> |
| 33 | #include <boost/thread/condition_variable.hpp> |
| 34 | #include <boost/thread/mutex.hpp> |
| 35 | #include <condition_variable> |
| 36 | #include <mutex> |
| 37 | |
| 38 | #include "gtest/gtest.h" |
| 39 | |
| 40 | #include "cryptonote_protocol/cryptonote_protocol_defs.h" |
| 41 | #include "include_base_utils.h" |
| 42 | #include "string_tools.h" |
| 43 | #include "net/abstract_tcp_server2.h" |
| 44 | #include "net/levin_protocol_handler_async.h" |
| 45 | #include "p2p/net_node.h" |
| 46 | |
| 47 | namespace |
| 48 | { |
| 49 | const uint32_t test_server_port = 5626; |
| 50 | const std::string test_server_host("127.0.0.1"); |
| 51 | |
| 52 | struct test_connection_context : public epee::net_utils::connection_context_base |
| 53 | { |
| 54 | }; |
| 55 | |
| 56 | struct test_protocol_handler_config |
| 57 | { |
| 58 | }; |
| 59 | |
| 60 | struct test_protocol_handler |
| 61 | { |
| 62 | typedef test_connection_context connection_context; |
| 63 | typedef test_protocol_handler_config config_type; |
| 64 | |
| 65 | test_protocol_handler(epee::net_utils::i_service_endpoint* /*psnd_hndlr*/, config_type& /*config*/, connection_context& /*conn_context*/) |
| 66 | { |
| 67 | } |
| 68 | |
| 69 | void after_init_connection() |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | void handle_qued_callback() |
| 74 | { |
| 75 | } |
| 76 | |
| 77 | bool release_protocol() |
| 78 | { |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | bool handle_recv(const void* /*data*/, size_t /*size*/) |
| 83 | { |
| 84 | return false; |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | typedef epee::net_utils::boosted_tcp_server<test_protocol_handler> test_tcp_server; |
| 89 | } |
| 90 | |
| 91 | TEST(boosted_tcp_server, worker_threads_are_exception_resistant) |
| 92 | { |
| 93 | test_tcp_server srv(epee::net_utils::e_connection_type_RPC); // RPC disables network limit for unit tests |
| 94 | ASSERT_TRUE(srv.init_server(test_server_port, test_server_host)); |
| 95 | |
| 96 | boost::mutex mtx; |
| 97 | boost::condition_variable cond; |
| 98 | int counter = 0; |
| 99 | |
| 100 | auto counter_incrementer = [&counter, &cond, &mtx]() |
| 101 | { |
| 102 | boost::unique_lock<boost::mutex> lock(mtx); |
| 103 | ++counter; |
| 104 | if (4 <= counter) |
| 105 | { |
| 106 | cond.notify_one(); |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | // 2 theads, but 4 exceptions |
| 111 | ASSERT_TRUE(srv.run_server(2, false)); |
| 112 | ASSERT_TRUE(srv.async_call([&counter_incrementer]() { counter_incrementer(); throw std::runtime_error("test 1"); })); |
| 113 | ASSERT_TRUE(srv.async_call([&counter_incrementer]() { counter_incrementer(); throw std::string("test 2"); })); |
| 114 | ASSERT_TRUE(srv.async_call([&counter_incrementer]() { counter_incrementer(); throw "test 3"; })); |
| 115 | ASSERT_TRUE(srv.async_call([&counter_incrementer]() { counter_incrementer(); throw 4; })); |
| 116 | |
| 117 | { |
| 118 | boost::unique_lock<boost::mutex> lock(mtx); |
| 119 | ASSERT_TRUE(cond.wait_for(lock, boost::chrono::seconds(5), [&counter]{ return counter == 4; })); |
| 120 | } |
| 121 | |
| 122 | // Check if threads are alive |
| 123 | counter = 0; |
| 124 | //auto counter_incrementer = [&counter]() { counter.fetch_add(1); epee::misc_utils::sleep_no_w(counter.load() * 10); }; |
| 125 | ASSERT_TRUE(srv.async_call(counter_incrementer)); |
| 126 | ASSERT_TRUE(srv.async_call(counter_incrementer)); |
| 127 | ASSERT_TRUE(srv.async_call(counter_incrementer)); |
| 128 | ASSERT_TRUE(srv.async_call(counter_incrementer)); |
| 129 | |
| 130 | { |
| 131 | boost::unique_lock<boost::mutex> lock(mtx); |
| 132 | ASSERT_TRUE(cond.wait_for(lock, boost::chrono::seconds(5), [&counter]{ return counter == 4; })); |
| 133 | } |
| 134 | |
| 135 | srv.send_stop_signal(); |
| 136 | ASSERT_TRUE(srv.timed_wait_server_stop(5 * 1000)); |
| 137 | ASSERT_TRUE(srv.deinit_server()); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | TEST(test_epee_connection, test_lifetime) |
| 142 | { |
| 143 | struct context_t: epee::net_utils::connection_context_base { |
| 144 | static constexpr size_t get_max_bytes(int) noexcept { return -1; } |
| 145 | static constexpr int handshake_command() noexcept { return 1001; } |
| 146 | static constexpr bool handshake_complete() noexcept { return true; } |
| 147 | }; |
| 148 | |
| 149 | using functional_obj_t = std::function<void ()>; |
| 150 | struct command_handler_t: epee::levin::levin_commands_handler<context_t> { |
| 151 | size_t delay; |
| 152 | functional_obj_t on_connection_close_f; |
| 153 | command_handler_t(size_t delay = 0, |
| 154 | functional_obj_t on_connection_close_f = nullptr |
| 155 | ): |
| 156 | delay(delay), |
| 157 | on_connection_close_f(on_connection_close_f) |
| 158 | {} |
| 159 | virtual int invoke(int, const epee::span<const uint8_t>, epee::byte_stream&, context_t&) override { epee::misc_utils::sleep_no_w(delay); return {}; } |
| 160 | virtual int notify(int, const epee::span<const uint8_t>, context_t&) override { return {}; } |
| 161 | virtual void callback(context_t&) override {} |
| 162 | virtual void on_connection_new(context_t&) override {} |
| 163 | virtual void on_connection_close(context_t&) override { |
| 164 | if (on_connection_close_f) |
| 165 | on_connection_close_f(); |
| 166 | } |
| 167 | virtual ~command_handler_t() override {} |
| 168 | static void destroy(epee::levin::levin_commands_handler<context_t>* ptr) { delete ptr; } |
| 169 | }; |
| 170 | |
| 171 | using handler_t = epee::levin::async_protocol_handler<context_t>; |
| 172 | using connection_t = epee::net_utils::connection<handler_t>; |
| 173 | using connection_ptr = boost::shared_ptr<connection_t>; |
| 174 | using shared_state_t = typename connection_t::shared_state; |
| 175 | using shared_state_ptr = std::shared_ptr<shared_state_t>; |
| 176 | using shared_states_t = std::vector<shared_state_ptr>; |
| 177 | using tag_t = boost::uuids::uuid; |
| 178 | using tags_t = std::vector<tag_t>; |
| 179 | using io_context_t = boost::asio::io_context; |
| 180 | using endpoint_t = boost::asio::ip::tcp::endpoint; |
| 181 | using work_t = boost::asio::executor_work_guard<boost::asio::io_context::executor_type>; |
| 182 | using work_ptr = std::shared_ptr<work_t>; |
| 183 | using workers_t = std::vector<std::thread>; |
| 184 | using server_t = epee::net_utils::boosted_tcp_server<handler_t>; |
| 185 | using lock_t = std::mutex; |
| 186 | using lock_guard_t = std::lock_guard<lock_t>; |
| 187 | using connection_weak_ptr = boost::weak_ptr<connection_t>; |
| 188 | struct shared_conn_t { |
| 189 | lock_t lock; |
| 190 | connection_weak_ptr conn; |
| 191 | }; |
| 192 | using shared_conn_ptr = std::shared_ptr<shared_conn_t>; |
| 193 | |
| 194 | io_context_t io_context; |
| 195 | work_ptr work(std::make_shared<work_t>(io_context.get_executor())); |
| 196 | |
| 197 | workers_t workers; |
| 198 | while (workers.size() < 4) { |
| 199 | workers.emplace_back([&io_context]{ |
| 200 | io_context.run(); |
| 201 | }); |
| 202 | } |
| 203 | |
| 204 | endpoint_t endpoint(boost::asio::ip::make_address("127.0.0.1"), 5262); |
| 205 | server_t server(epee::net_utils::e_connection_type_P2P); |
| 206 | server.init_server(endpoint.port(), |
| 207 | endpoint.address().to_string(), |
| 208 | 0, |
| 209 | "", |
| 210 | false, |
| 211 | true, |
| 212 | epee::net_utils::ssl_support_t::e_ssl_support_disabled |
| 213 | ); |
| 214 | server.run_server(2, false); |
| 215 | server.get_config_shared()->set_handler(new command_handler_t, &command_handler_t::destroy); |
| 216 | |
| 217 | boost::asio::post(io_context, [&io_context, &work, &endpoint, &server]{ |
| 218 | auto scope_exit_handler = epee::misc_utils::create_scope_leave_handler([&work]{ |
| 219 | work.reset(); |
| 220 | }); |
| 221 | |
| 222 | shared_state_ptr shared_state(std::make_shared<shared_state_t>()); |
| 223 | shared_state->set_handler(new command_handler_t, &command_handler_t::destroy); |
| 224 | |
| 225 | auto create_connection = [&io_context, &endpoint, &shared_state] { |
| 226 | connection_ptr conn(new connection_t(io_context, shared_state, {}, {})); |
| 227 | conn->socket().connect(endpoint); |
| 228 | conn->start({}, {}); |
| 229 | context_t context; |
| 230 | conn->get_context(context); |
| 231 | auto tag = context.m_connection_id; |
| 232 | return tag; |
| 233 | }; |
| 234 | |
| 235 | ASSERT_TRUE(shared_state->get_connections_count() == 0); |
| 236 | auto tag = create_connection(); |
| 237 | ASSERT_TRUE(shared_state->get_connections_count() == 1); |
| 238 | bool success = shared_state->for_connection(tag, [shared_state](context_t& context){ |
| 239 | shared_state->close(context.m_connection_id, true); |
| 240 | context.m_remote_address.get_zone(); |
| 241 | return true; |
| 242 | }); |
| 243 | ASSERT_TRUE(success); |
| 244 | |
| 245 | ASSERT_TRUE(shared_state->get_connections_count() == 0); |
| 246 | constexpr auto N = 8; |
| 247 | tags_t tags(N); |
| 248 | for(auto &t: tags) |
| 249 | t = create_connection(); |
| 250 | ASSERT_TRUE(shared_state->get_connections_count() == N); |
| 251 | size_t index = 0; |
| 252 | success = shared_state->foreach_connection([&index, shared_state, &tags, &create_connection](context_t& context){ |
| 253 | if (!index) |
| 254 | for (const auto &t: tags) |
| 255 | shared_state->close(t, true); |
| 256 | |
| 257 | shared_state->close(context.m_connection_id, true); |
| 258 | context.m_remote_address.get_zone(); |
| 259 | ++index; |
| 260 | |
| 261 | for(auto i = 0; i < N; ++i) |
| 262 | create_connection(); |
| 263 | return true; |
| 264 | }); |
| 265 | ASSERT_TRUE(success); |
| 266 | ASSERT_TRUE(index == N); |
| 267 | ASSERT_TRUE(shared_state->get_connections_count() == N * N); |
| 268 | |
| 269 | index = 0; |
| 270 | success = shared_state->foreach_connection([&index, shared_state](context_t& context){ |
| 271 | shared_state->close(context.m_connection_id, true); |
| 272 | context.m_remote_address.get_zone(); |
| 273 | ++index; |
| 274 | return true; |
| 275 | }); |
| 276 | ASSERT_TRUE(success); |
| 277 | ASSERT_TRUE(index == N * N); |
| 278 | ASSERT_TRUE(shared_state->get_connections_count() == 0); |
| 279 | |
| 280 | while (shared_state->sock_count); |
| 281 | ASSERT_TRUE(shared_state->get_connections_count() == 0); |
| 282 | constexpr auto DELAY = 30; |
| 283 | constexpr auto TIMEOUT = 1; |
| 284 | while (server.get_connections_count()) { |
| 285 | server.get_config_shared()->del_in_connections( |
| 286 | server.get_config_shared()->get_in_connections_count() |
| 287 | ); |
| 288 | } |
| 289 | server.get_config_shared()->set_handler(new command_handler_t(DELAY), &command_handler_t::destroy); |
| 290 | for (auto i = 0; i < N; ++i) { |
| 291 | tag = create_connection(); |
| 292 | ASSERT_TRUE(shared_state->get_connections_count() == 1); |
| 293 | success = shared_state->invoke_async(1, epee::levin::message_writer{}, tag, [](int, const epee::span<const uint8_t>, context_t&){}, TIMEOUT); |
| 294 | ASSERT_TRUE(success); |
| 295 | while (shared_state->sock_count == 1) { |
| 296 | success = shared_state->foreach_connection([&shared_state, &tag](context_t&){ |
| 297 | return shared_state->request_callback(tag); |
| 298 | }); |
| 299 | ASSERT_TRUE(success); |
| 300 | } |
| 301 | shared_state->close(tag, true); |
| 302 | ASSERT_TRUE(shared_state->get_connections_count() == 0); |
| 303 | } |
| 304 | |
| 305 | while (shared_state->sock_count); |
| 306 | constexpr auto ZERO_DELAY = 0; |
| 307 | size_t counter = 0; |
| 308 | shared_state->set_handler(new command_handler_t(ZERO_DELAY, |
| 309 | [&counter]{ |
| 310 | ASSERT_TRUE(counter++ == 0); |
| 311 | } |
| 312 | ), |
| 313 | &command_handler_t::destroy |
| 314 | ); |
| 315 | connection_ptr conn(new connection_t(io_context, shared_state, {}, {})); |
| 316 | conn->socket().connect(endpoint); |
| 317 | conn->start({}, {}); |
| 318 | ASSERT_TRUE(shared_state->get_connections_count() == 1); |
| 319 | shared_state->del_out_connections(1); |
| 320 | ASSERT_TRUE(shared_state->get_connections_count() == 0); |
| 321 | conn.reset(); |
| 322 | |
| 323 | while (shared_state->sock_count); |
| 324 | shared_conn_ptr shared_conn(std::make_shared<shared_conn_t>()); |
| 325 | shared_state->set_handler(new command_handler_t(ZERO_DELAY, |
| 326 | [shared_state, shared_conn]{ |
| 327 | { |
| 328 | connection_ptr conn; |
| 329 | { |
| 330 | lock_guard_t guard(shared_conn->lock); |
| 331 | conn = shared_conn->conn.lock(); |
| 332 | shared_conn->conn.reset(); |
| 333 | } |
| 334 | if (conn) |
| 335 | conn->cancel(); |
| 336 | } |
| 337 | const auto success = shared_state->foreach_connection([](context_t&){ |
| 338 | return true; |
| 339 | }); |
| 340 | ASSERT_TRUE(success); |
| 341 | } |
| 342 | ), |
| 343 | &command_handler_t::destroy |
| 344 | ); |
| 345 | for (auto i = 0; i < N * N * N; ++i) { |
| 346 | { |
| 347 | connection_ptr conn(new connection_t(io_context, shared_state, {}, {})); |
| 348 | conn->socket().connect(endpoint); |
| 349 | conn->start({}, {}); |
| 350 | lock_guard_t guard(shared_conn->lock); |
| 351 | shared_conn->conn = conn; |
| 352 | } |
| 353 | ASSERT_TRUE(shared_state->get_connections_count() == 1); |
| 354 | shared_state->del_out_connections(1); |
| 355 | while (shared_state->sock_count); |
| 356 | ASSERT_TRUE(shared_state->get_connections_count() == 0); |
| 357 | } |
| 358 | |
| 359 | shared_states_t shared_states; |
| 360 | while (shared_states.size() < 2) { |
| 361 | shared_states.emplace_back(std::make_shared<shared_state_t>()); |
| 362 | shared_states.back()->set_handler(new command_handler_t(ZERO_DELAY, |
| 363 | [&shared_states]{ |
| 364 | for (auto &s: shared_states) { |
| 365 | auto success = s->foreach_connection([](context_t&){ |
| 366 | return true; |
| 367 | }); |
| 368 | ASSERT_TRUE(success); |
| 369 | } |
| 370 | } |
| 371 | ), |
| 372 | &command_handler_t::destroy |
| 373 | ); |
| 374 | } |
| 375 | workers_t workers; |
| 376 | |
| 377 | for (auto &s: shared_states) { |
| 378 | workers.emplace_back([&io_context, &s, &endpoint]{ |
| 379 | for (auto i = 0; i < N * N; ++i) { |
| 380 | connection_ptr conn(new connection_t(io_context, s, {}, {})); |
| 381 | conn->socket().connect(endpoint); |
| 382 | conn->start({}, {}); |
| 383 | boost::asio::post(io_context, [conn] { conn->cancel(); }); |
| 384 | conn.reset(); |
| 385 | s->del_out_connections(1); |
| 386 | while (s->sock_count); |
| 387 | } |
| 388 | }); |
| 389 | } |
| 390 | for (;workers.size(); workers.pop_back()) |
| 391 | workers.back().join(); |
| 392 | |
| 393 | for (auto &s: shared_states) { |
| 394 | workers.emplace_back([&io_context, &s, &endpoint]{ |
| 395 | for (auto i = 0; i < N * N; ++i) { |
| 396 | connection_ptr conn(new connection_t(io_context, s, {}, {})); |
| 397 | conn->socket().connect(endpoint); |
| 398 | conn->start({}, {}); |
| 399 | conn->cancel(); |
| 400 | while (conn.use_count() > 1); |
| 401 | s->foreach_connection([&io_context, &s, &endpoint, &conn](context_t& context){ |
| 402 | conn.reset(new connection_t(io_context, s, {}, {})); |
| 403 | conn->socket().connect(endpoint); |
| 404 | conn->start({}, {}); |
| 405 | conn->cancel(); |
| 406 | while (conn.use_count() > 1); |
| 407 | conn.reset(); |
| 408 | return true; |
| 409 | }); |
| 410 | while (s->sock_count); |
| 411 | } |
| 412 | }); |
| 413 | } |
| 414 | for (;workers.size(); workers.pop_back()) |
| 415 | workers.back().join(); |
| 416 | |
| 417 | for (auto &s: shared_states) { |
| 418 | workers.emplace_back([&io_context, &s, &endpoint]{ |
| 419 | for (auto i = 0; i < N; ++i) { |
| 420 | connection_ptr conn(new connection_t(io_context, s, {}, {})); |
| 421 | conn->socket().connect(endpoint); |
| 422 | conn->start({}, {}); |
| 423 | context_t context; |
| 424 | conn->get_context(context); |
| 425 | auto tag = context.m_connection_id; |
| 426 | conn->cancel(); |
| 427 | while (conn.use_count() > 1); |
| 428 | s->for_connection(tag, [&io_context, &s, &endpoint, &conn](context_t& context){ |
| 429 | conn.reset(new connection_t(io_context, s, {}, {})); |
| 430 | conn->socket().connect(endpoint); |
| 431 | conn->start({}, {}); |
| 432 | conn->cancel(); |
| 433 | while (conn.use_count() > 1); |
| 434 | conn.reset(); |
| 435 | return true; |
| 436 | }); |
| 437 | while (s->sock_count); |
| 438 | } |
| 439 | }); |
| 440 | } |
| 441 | for (;workers.size(); workers.pop_back()) |
| 442 | workers.back().join(); |
| 443 | |
| 444 | for (auto &s: shared_states) { |
| 445 | workers.emplace_back([&io_context, &s, &endpoint]{ |
| 446 | for (auto i = 0; i < N; ++i) { |
| 447 | connection_ptr conn(new connection_t(io_context, s, {}, {})); |
| 448 | conn->socket().connect(endpoint); |
| 449 | conn->start({}, {}); |
| 450 | context_t context; |
| 451 | conn->get_context(context); |
| 452 | auto tag = context.m_connection_id; |
| 453 | boost::asio::post(io_context, [conn] { conn->cancel(); }); |
| 454 | conn.reset(); |
| 455 | s->close(tag, true); |
| 456 | while (s->sock_count); |
| 457 | } |
| 458 | }); |
| 459 | } |
| 460 | for (;workers.size(); workers.pop_back()) |
| 461 | workers.back().join(); |
| 462 | while (server.get_connections_count()) { |
| 463 | server.get_config_shared()->del_in_connections( |
| 464 | server.get_config_shared()->get_in_connections_count() |
| 465 | ); |
| 466 | } |
| 467 | }); |
| 468 | |
| 469 | for (auto& w: workers) { |
| 470 | w.join(); |
| 471 | } |
| 472 | server.send_stop_signal(); |
| 473 | server.timed_wait_server_stop(5 * 1000); |
| 474 | server.deinit_server(); |
| 475 | } |
| 476 | |
| 477 | TEST(test_epee_connection, ssl_shutdown) |
| 478 | { |
| 479 | struct context_t: epee::net_utils::connection_context_base { |
| 480 | static constexpr size_t get_max_bytes(int) noexcept { return -1; } |
| 481 | static constexpr int handshake_command() noexcept { return 1001; } |
| 482 | static constexpr bool handshake_complete() noexcept { return true; } |
| 483 | }; |
| 484 | |
| 485 | struct command_handler_t: epee::levin::levin_commands_handler<context_t> { |
| 486 | virtual int invoke(int, const epee::span<const uint8_t>, epee::byte_stream&, context_t&) override { return {}; } |
| 487 | virtual int notify(int, const epee::span<const uint8_t>, context_t&) override { return {}; } |
| 488 | virtual void callback(context_t&) override {} |
| 489 | virtual void on_connection_new(context_t&) override {} |
| 490 | virtual void on_connection_close(context_t&) override { } |
| 491 | virtual ~command_handler_t() override {} |
| 492 | static void destroy(epee::levin::levin_commands_handler<context_t>* ptr) { delete ptr; } |
| 493 | }; |
| 494 | |
| 495 | using handler_t = epee::levin::async_protocol_handler<context_t>; |
| 496 | using io_context_t = boost::asio::io_context; |
| 497 | using endpoint_t = boost::asio::ip::tcp::endpoint; |
| 498 | using server_t = epee::net_utils::boosted_tcp_server<handler_t>; |
| 499 | using socket_t = boost::asio::ip::tcp::socket; |
| 500 | using ssl_socket_t = boost::asio::ssl::stream<socket_t>; |
| 501 | using ssl_context_t = boost::asio::ssl::context; |
| 502 | using ec_t = boost::system::error_code; |
| 503 | |
| 504 | endpoint_t endpoint(boost::asio::ip::make_address("127.0.0.1"), 5263); |
| 505 | server_t server(epee::net_utils::e_connection_type_P2P); |
| 506 | server.init_server(endpoint.port(), |
| 507 | endpoint.address().to_string(), |
| 508 | 0, |
| 509 | "", |
| 510 | false, |
| 511 | true, |
| 512 | epee::net_utils::ssl_support_t::e_ssl_support_enabled |
| 513 | ); |
| 514 | server.get_config_shared()->set_handler(new command_handler_t, &command_handler_t::destroy); |
| 515 | server.run_server(2, false); |
| 516 | |
| 517 | ssl_context_t ssl_context{boost::asio::ssl::context::sslv23}; |
| 518 | io_context_t io_context; |
| 519 | ssl_socket_t socket(io_context, ssl_context); |
| 520 | ec_t ec; |
| 521 | socket.next_layer().connect(endpoint, ec); |
| 522 | EXPECT_EQ(ec.value(), 0); |
| 523 | socket.handshake(boost::asio::ssl::stream_base::client, ec); |
| 524 | EXPECT_EQ(ec.value(), 0); |
| 525 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 526 | while (server.get_config_shared()->get_connections_count() < 1); |
| 527 | server.get_config_shared()->del_in_connections(1); |
| 528 | while (server.get_config_shared()->get_connections_count() > 0); |
| 529 | server.send_stop_signal(); |
| 530 | EXPECT_TRUE(server.timed_wait_server_stop(5 * 1000)); |
| 531 | server.deinit_server(); |
| 532 | socket.next_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec); |
| 533 | socket.next_layer().close(ec); |
| 534 | socket.shutdown(ec); |
| 535 | } |
| 536 | |
| 537 | TEST(test_epee_connection, ssl_handshake) |
| 538 | { |
| 539 | using io_context_t = boost::asio::io_context; |
| 540 | using work_t = boost::asio::executor_work_guard<boost::asio::io_context::executor_type>; |
| 541 | using work_ptr = std::shared_ptr<work_t>; |
| 542 | using workers_t = std::vector<std::thread>; |
| 543 | using socket_t = boost::asio::ip::tcp::socket; |
| 544 | using ssl_socket_t = boost::asio::ssl::stream<socket_t>; |
| 545 | using ssl_socket_ptr = std::unique_ptr<ssl_socket_t>; |
| 546 | using ssl_options_t = epee::net_utils::ssl_options_t; |
| 547 | io_context_t io_context; |
| 548 | work_ptr work(std::make_shared<work_t>(io_context.get_executor())); |
| 549 | workers_t workers; |
| 550 | auto constexpr N = 2; |
| 551 | while (workers.size() < N) { |
| 552 | workers.emplace_back([&io_context]{ |
| 553 | io_context.run(); |
| 554 | }); |
| 555 | } |
| 556 | ssl_options_t ssl_options{{}}; |
| 557 | auto ssl_context = ssl_options.create_context(); |
| 558 | for (size_t i = 0; i < N * N * N; ++i) { |
| 559 | ssl_socket_ptr ssl_socket(new ssl_socket_t(io_context, ssl_context)); |
| 560 | ssl_socket->next_layer().open(boost::asio::ip::tcp::v4()); |
| 561 | for (size_t i = 0; i < N; ++i) { |
| 562 | boost::asio::post( |
| 563 | io_context, |
| 564 | [] { std::this_thread::sleep_for(std::chrono::milliseconds(50)); } |
| 565 | ); |
| 566 | } |
| 567 | EXPECT_EQ( |
| 568 | ssl_options.handshake( |
| 569 | io_context, |
| 570 | *ssl_socket, |
| 571 | ssl_socket_t::server, |
| 572 | {}, |
| 573 | {}, |
| 574 | std::chrono::milliseconds(0) |
| 575 | ), |
| 576 | false |
| 577 | ); |
| 578 | ssl_socket->next_layer().close(); |
| 579 | ssl_socket.reset(); |
| 580 | } |
| 581 | work.reset(); |
| 582 | for (;workers.size(); workers.pop_back()) |
| 583 | workers.back().join(); |
| 584 | } |
| 585 | |
| 586 | |
| 587 | TEST(boosted_tcp_server, strand_deadlock) |
| 588 | { |
| 589 | using context_t = epee::net_utils::connection_context_base; |
| 590 | using lock_t = std::mutex; |
| 591 | using unique_lock_t = std::unique_lock<lock_t>; |
| 592 | |
| 593 | struct config_t { |
| 594 | using condition_t = std::condition_variable_any; |
| 595 | using lock_guard_t = std::lock_guard<lock_t>; |
| 596 | void notify_success() |
| 597 | { |
| 598 | lock_guard_t guard(lock); |
| 599 | success = true; |
| 600 | condition.notify_all(); |
| 601 | } |
| 602 | lock_t lock; |
| 603 | condition_t condition; |
| 604 | bool success; |
| 605 | }; |
| 606 | |
| 607 | struct handler_t { |
| 608 | using config_type = config_t; |
| 609 | using connection_context = context_t; |
| 610 | using byte_slice_t = epee::byte_slice; |
| 611 | using socket_t = epee::net_utils::i_service_endpoint; |
| 612 | |
| 613 | handler_t(socket_t *socket, config_t &config, context_t &context): |
| 614 | socket(socket), |
| 615 | config(config), |
| 616 | context(context) |
| 617 | {} |
| 618 | void after_init_connection() |
| 619 | { |
| 620 | unique_lock_t guard(lock); |
| 621 | if (!context.m_is_income) { |
| 622 | guard.unlock(); |
| 623 | socket->do_send(byte_slice_t{"."}); |
| 624 | } |
| 625 | } |
| 626 | void handle_qued_callback() |
| 627 | { |
| 628 | } |
| 629 | bool handle_recv(const char *data, size_t bytes_transferred) |
| 630 | { |
| 631 | unique_lock_t guard(lock); |
| 632 | if (!context.m_is_income) { |
| 633 | if (context.m_recv_cnt == 1024) { |
| 634 | guard.unlock(); |
| 635 | socket->do_send(byte_slice_t{"."}); |
| 636 | } |
| 637 | } |
| 638 | else { |
| 639 | if (context.m_recv_cnt == 1) { |
| 640 | for(size_t i = 0; i < 1024; ++i) { |
| 641 | guard.unlock(); |
| 642 | socket->do_send(byte_slice_t{"."}); |
| 643 | guard.lock(); |
| 644 | } |
| 645 | } |
| 646 | else if(context.m_recv_cnt == 2) { |
| 647 | guard.unlock(); |
| 648 | socket->close(false); |
| 649 | } |
| 650 | } |
| 651 | return true; |
| 652 | } |
| 653 | void release_protocol() |
| 654 | { |
| 655 | unique_lock_t guard(lock); |
| 656 | if(!context.m_is_income |
| 657 | && context.m_recv_cnt == 1024 |
| 658 | && context.m_send_cnt == 2 |
| 659 | ) { |
| 660 | guard.unlock(); |
| 661 | config.notify_success(); |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | lock_t lock; |
| 666 | socket_t *socket; |
| 667 | config_t &config; |
| 668 | context_t &context; |
| 669 | }; |
| 670 | |
| 671 | using server_t = epee::net_utils::boosted_tcp_server<handler_t>; |
| 672 | using endpoint_t = boost::asio::ip::tcp::endpoint; |
| 673 | |
| 674 | endpoint_t endpoint(boost::asio::ip::make_address("127.0.0.1"), 5262); |
| 675 | server_t server(epee::net_utils::e_connection_type_P2P); |
| 676 | server.init_server( |
| 677 | endpoint.port(), |
| 678 | endpoint.address().to_string(), |
| 679 | {}, |
| 680 | {}, |
| 681 | {}, |
| 682 | true, |
| 683 | epee::net_utils::ssl_support_t::e_ssl_support_disabled |
| 684 | ); |
| 685 | server.run_server(2, {}); |
| 686 | server.async_call( |
| 687 | [&]{ |
| 688 | context_t context; |
| 689 | ASSERT_TRUE( |
| 690 | server.connect( |
| 691 | endpoint.address().to_string(), |
| 692 | std::to_string(endpoint.port()), |
| 693 | 5, |
| 694 | context, |
| 695 | "0.0.0.0", |
| 696 | epee::net_utils::ssl_support_t::e_ssl_support_disabled |
| 697 | ) |
| 698 | ); |
| 699 | } |
| 700 | ); |
| 701 | { |
| 702 | unique_lock_t guard(server.get_config_object().lock); |
| 703 | EXPECT_TRUE( |
| 704 | server.get_config_object().condition.wait_for( |
| 705 | guard, |
| 706 | std::chrono::seconds(5), |
| 707 | [&] { return server.get_config_object().success; } |
| 708 | ) |
| 709 | ); |
| 710 | } |
| 711 | |
| 712 | server.send_stop_signal(); |
| 713 | server.timed_wait_server_stop(5 * 1000); |
| 714 | server.deinit_server(); |
| 715 | } |
| 716 | |
| 717 | TEST(boosted_tcp_server, shutdown) |
| 718 | { |
| 719 | struct context_t: epee::net_utils::connection_context_base { |
| 720 | static constexpr size_t get_max_bytes(int) noexcept { return -1; } |
| 721 | static constexpr int handshake_command() noexcept { return 1001; } |
| 722 | static constexpr bool handshake_complete() noexcept { return true; } |
| 723 | }; |
| 724 | |
| 725 | struct config_t : epee::levin::async_protocol_handler_config<context_t> { |
| 726 | void received_handshake() { handshake_received.raise(); } |
| 727 | epee::simple_event handshake_received; |
| 728 | }; |
| 729 | |
| 730 | struct handler_t : epee::levin::async_protocol_handler<context_t> { |
| 731 | using config_type = config_t; |
| 732 | using connection_context = context_t; |
| 733 | using epee::levin::async_protocol_handler<context_t>::async_protocol_handler; |
| 734 | |
| 735 | bool handle_recv(const void *data, size_t bytes_transferred) |
| 736 | { |
| 737 | // We don't respond to the handshake (the async_invoke_remote_command2 is waiting for a response) |
| 738 | MINFO("handle_recv just came in"); |
| 739 | config_t* config = dynamic_cast<config_t*>(&m_config); |
| 740 | if (config == nullptr) |
| 741 | throw std::runtime_error("m_config must be of type config_t"); |
| 742 | config->received_handshake(); |
| 743 | return true; |
| 744 | } |
| 745 | }; |
| 746 | |
| 747 | boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::make_address("127.0.0.1"), 5262); |
| 748 | epee::net_utils::boosted_tcp_server<handler_t> server(epee::net_utils::e_connection_type_P2P); |
| 749 | server.init_server( |
| 750 | endpoint.port(), |
| 751 | endpoint.address().to_string(), |
| 752 | {}, |
| 753 | {}, |
| 754 | {}, |
| 755 | true, |
| 756 | epee::net_utils::ssl_support_t::e_ssl_support_disabled |
| 757 | ); |
| 758 | |
| 759 | // Run the server in a thread and wait for it to start |
| 760 | MINFO("Starting the server"); |
| 761 | std::thread running_server([&]{server.run_server(2, true/*wait*/);} ); |
| 762 | |
| 763 | // Have the server connect to itself |
| 764 | MINFO("Connecting the server to itself"); |
| 765 | context_t context; |
| 766 | { |
| 767 | epee::simple_event connected; |
| 768 | server.async_call( |
| 769 | [&]{ |
| 770 | ASSERT_TRUE( |
| 771 | server.connect( |
| 772 | endpoint.address().to_string(), |
| 773 | std::to_string(endpoint.port()), |
| 774 | 5, |
| 775 | context, |
| 776 | "0.0.0.0", |
| 777 | epee::net_utils::ssl_support_t::e_ssl_support_disabled |
| 778 | ) |
| 779 | ); |
| 780 | connected.raise(); |
| 781 | } |
| 782 | ); |
| 783 | connected.wait(); |
| 784 | } |
| 785 | |
| 786 | // Invoke handshake to the connection, and wait for cb cancel in a separate thread |
| 787 | MINFO("Invoking handshake"); |
| 788 | epee::simple_event ev; |
| 789 | { |
| 790 | using COMMAND_HANDSHAKE = nodetool::COMMAND_HANDSHAKE_T<cryptonote::CORE_SYNC_DATA>; |
| 791 | COMMAND_HANDSHAKE::request arg; |
| 792 | bool r = epee::net_utils::async_invoke_remote_command2<COMMAND_HANDSHAKE::response>(context, COMMAND_HANDSHAKE::ID, arg, server.get_config_object(), |
| 793 | [&ev](int code, const COMMAND_HANDSHAKE::response&, context_t&) |
| 794 | { |
| 795 | ASSERT_EQ(code, LEVIN_ERROR_CONNECTION_DESTROYED); |
| 796 | ev.raise(); |
| 797 | }, P2P_DEFAULT_HANDSHAKE_INVOKE_TIMEOUT); |
| 798 | ASSERT_TRUE(r); |
| 799 | |
| 800 | MINFO("Waiting for handshake invocation to be received"); |
| 801 | server.get_config_object().handshake_received.wait(); |
| 802 | } |
| 803 | |
| 804 | // Now stop the server, providing the callback necessary to wait for all connections to shutdown |
| 805 | const auto close_all_connections = [&]() |
| 806 | { |
| 807 | server.get_config_object().close(context.m_connection_id, true/*wait_for_shutdown*/); |
| 808 | }; |
| 809 | |
| 810 | MINFO("Stopping the server"); |
| 811 | server.send_stop_signal(close_all_connections); |
| 812 | running_server.join(); |
| 813 | |
| 814 | MINFO("Waiting for handshake to cancel"); |
| 815 | ev.wait(); |
| 816 | } |