Debian Codegraph @

monero/tests/unit_tests/epee_boosted_tcp_server.cpp

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