Electroneum
Loading...
Searching...
No Matches
epee_levin_protocol_handler_async.cpp
Go to the documentation of this file.
1// Copyrights(c) 2017-2021, The Electroneum Project
2// Copyrights(c) 2014-2019, The Monero Project
3//
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without modification, are
7// permitted provided that the following conditions are met:
8//
9// 1. Redistributions of source code must retain the above copyright notice, this list of
10// conditions and the following disclaimer.
11//
12// 2. Redistributions in binary form must reproduce the above copyright notice, this list
13// of conditions and the following disclaimer in the documentation and/or other
14// materials provided with the distribution.
15//
16// 3. Neither the name of the copyright holder nor the names of its contributors may be
17// used to endorse or promote products derived from this software without specific
18// prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
31
32#include <boost/thread/mutex.hpp>
33#include <boost/thread/thread.hpp>
34
35#include "gtest/gtest.h"
36
37#include "include_base_utils.h"
38#include "string_tools.h"
40#include "net/net_utils_base.h"
41#include "unit_tests_utils.h"
42
43namespace
44{
45 struct test_levin_connection_context : public epee::net_utils::connection_context_base
46 {
47 };
48
51
52 struct test_levin_commands_handler : public epee::levin::levin_commands_handler<test_levin_connection_context>
53 {
54 test_levin_commands_handler()
55 : m_return_code(LEVIN_OK)
56 , m_last_command(-1)
57 {
58 }
59
60 virtual int invoke(int command, const epee::span<const uint8_t> in_buff, std::string& buff_out, test_levin_connection_context& context)
61 {
62 m_invoke_counter.inc();
63 boost::unique_lock<boost::mutex> lock(m_mutex);
64 m_last_command = command;
65 m_last_in_buf = std::string((const char*)in_buff.data(), in_buff.size());
66 buff_out = m_invoke_out_buf;
67 return m_return_code;
68 }
69
70 virtual int notify(int command, const epee::span<const uint8_t> in_buff, test_levin_connection_context& context)
71 {
72 m_notify_counter.inc();
73 boost::unique_lock<boost::mutex> lock(m_mutex);
74 m_last_command = command;
75 m_last_in_buf = std::string((const char*)in_buff.data(), in_buff.size());
76 return m_return_code;
77 }
78
79 virtual void callback(test_levin_connection_context& context)
80 {
81 m_callback_counter.inc();
82 //std::cout << "test_levin_commands_handler::callback()" << std::endl;
83 }
84
85 virtual void on_connection_new(test_levin_connection_context& context)
86 {
87 m_new_connection_counter.inc();
88 //std::cout << "test_levin_commands_handler::on_connection_new()" << std::endl;
89 }
90
91 virtual void on_connection_close(test_levin_connection_context& context)
92 {
93 m_close_connection_counter.inc();
94 //std::cout << "test_levin_commands_handler::on_connection_close()" << std::endl;
95 }
96
97 size_t invoke_counter() const { return m_invoke_counter.get(); }
98 size_t notify_counter() const { return m_notify_counter.get(); }
99 size_t callback_counter() const { return m_callback_counter.get(); }
100 size_t new_connection_counter() const { return m_new_connection_counter.get(); }
101 size_t close_connection_counter() const { return m_close_connection_counter.get(); }
102
103 int return_code() const { return m_return_code; }
104 void return_code(int v) { m_return_code = v; }
105
106 const std::string& invoke_out_buf() const { return m_invoke_out_buf; }
107 void invoke_out_buf(const std::string& v) { m_invoke_out_buf = v; }
108
109 int last_command() const { return m_last_command; }
110 const std::string& last_in_buf() const { return m_last_in_buf; }
111
112 private:
113 unit_test::call_counter m_invoke_counter;
114 unit_test::call_counter m_notify_counter;
115 unit_test::call_counter m_callback_counter;
116 unit_test::call_counter m_new_connection_counter;
117 unit_test::call_counter m_close_connection_counter;
118
119 boost::mutex m_mutex;
120
121 int m_return_code;
122 std::string m_invoke_out_buf;
123
124 int m_last_command;
125 std::string m_last_in_buf;
126 };
127
128 class test_connection : public epee::net_utils::i_service_endpoint
129 {
130 public:
131 test_connection(boost::asio::io_service& io_service, test_levin_protocol_handler_config& protocol_config)
132 : m_io_service(io_service)
133 , m_protocol_handler(this, protocol_config, m_context)
134 , m_send_return(true)
135 {
136 }
137
138 void start()
139 {
140 ASSERT_TRUE(m_protocol_handler.after_init_connection());
141 }
142
143 // Implement epee::net_utils::i_service_endpoint interface
144 virtual bool do_send(const void* ptr, size_t cb)
145 {
146 //std::cout << "test_connection::do_send()" << std::endl;
147 m_send_counter.inc();
148 boost::unique_lock<boost::mutex> lock(m_mutex);
149 m_last_send_data.append(reinterpret_cast<const char*>(ptr), cb);
150 return m_send_return;
151 }
152
153 virtual bool close() { /*std::cout << "test_connection::close()" << std::endl; */return true; }
154 virtual bool send_done() { /*std::cout << "test_connection::send_done()" << std::endl; */return true; }
155 virtual bool call_run_once_service_io() { std::cout << "test_connection::call_run_once_service_io()" << std::endl; return true; }
156 virtual bool request_callback() { std::cout << "test_connection::request_callback()" << std::endl; return true; }
157 virtual boost::asio::io_service& get_io_service() { std::cout << "test_connection::get_io_service()" << std::endl; return m_io_service; }
158 virtual bool add_ref() { std::cout << "test_connection::add_ref()" << std::endl; return true; }
159 virtual bool release() { std::cout << "test_connection::release()" << std::endl; return true; }
160
161 size_t send_counter() const { return m_send_counter.get(); }
162
163 const std::string& last_send_data() const { return m_last_send_data; }
164 void reset_last_send_data() { boost::unique_lock<boost::mutex> lock(m_mutex); m_last_send_data.clear(); }
165
166 bool send_return() const { return m_send_return; }
167 void send_return(bool v) { m_send_return = v; }
168
169 public:
170 test_levin_protocol_handler m_protocol_handler;
171
172 private:
173 boost::asio::io_service& m_io_service;
174 test_levin_connection_context m_context;
175
176 unit_test::call_counter m_send_counter;
177 boost::mutex m_mutex;
178
179 std::string m_last_send_data;
180
181 bool m_send_return;
182 };
183
184 class async_protocol_handler_test : public ::testing::Test
185 {
186 public:
187 const static uint64_t invoke_timeout = 5 * 1000;
188 const static size_t max_packet_size = 10 * 1024 * 1024;
189
190 typedef std::unique_ptr<test_connection> test_connection_ptr;
191
192 async_protocol_handler_test():
193 m_pcommands_handler(new test_levin_commands_handler()),
194 m_commands_handler(*m_pcommands_handler)
195 {
196 m_handler_config.set_handler(m_pcommands_handler, [](epee::levin::levin_commands_handler<test_levin_connection_context> *handler) { delete handler; });
197 m_handler_config.m_invoke_timeout = invoke_timeout;
198 m_handler_config.m_max_packet_size = max_packet_size;
199 }
200
201 virtual void SetUp()
202 {
203 }
204
205 protected:
206 test_connection_ptr create_connection(bool start = true)
207 {
208 test_connection_ptr conn(new test_connection(m_io_service, m_handler_config));
209 if (start)
210 {
211 conn->start();
212 }
213 return conn;
214 }
215
216 protected:
217 boost::asio::io_service m_io_service;
218 test_levin_protocol_handler_config m_handler_config;
219 test_levin_commands_handler *m_pcommands_handler, &m_commands_handler;
220 };
221
222 class positive_test_connection_to_levin_protocol_handler_calls : public async_protocol_handler_test
223 {
224 };
225
226 class test_levin_protocol_handler__hanle_recv_with_invalid_data : public async_protocol_handler_test
227 {
228 public:
229 static const int expected_command = 5615871;
230 static const int expected_return_code = 782546;
231
232 test_levin_protocol_handler__hanle_recv_with_invalid_data()
233 : m_expected_invoke_out_buf(512, 'y')
234 {
235 }
236
237 virtual void SetUp()
238 {
239 async_protocol_handler_test::SetUp();
240
241 m_conn = create_connection();
242
243 m_in_data.assign(256, 't');
244
245 m_req_head.m_signature = LEVIN_SIGNATURE;
246 m_req_head.m_cb = m_in_data.size();
247 m_req_head.m_have_to_return_data = true;
248 m_req_head.m_command = expected_command;
249 m_req_head.m_return_code = LEVIN_OK;
250 m_req_head.m_flags = LEVIN_PACKET_REQUEST;
251 m_req_head.m_protocol_version = LEVIN_PROTOCOL_VER_1;
252
253 m_commands_handler.return_code(expected_return_code);
254 m_commands_handler.invoke_out_buf(m_expected_invoke_out_buf);
255 }
256
257 protected:
258 void prepare_buf()
259 {
260 m_buf.assign(reinterpret_cast<const char*>(&m_req_head), sizeof(m_req_head));
261 m_buf += m_in_data;
262 }
263
264 protected:
265 test_connection_ptr m_conn;
266 epee::levin::bucket_head2 m_req_head;
267 std::string m_in_data;
268 std::string m_buf;
269 std::string m_expected_invoke_out_buf;
270 };
271}
272
273TEST_F(positive_test_connection_to_levin_protocol_handler_calls, new_handler_is_not_initialized)
274{
275 test_connection_ptr conn = create_connection(false);
276 ASSERT_FALSE(conn->m_protocol_handler.m_connection_initialized);
277 ASSERT_EQ(0, m_handler_config.get_connections_count());
278 ASSERT_EQ(0, m_commands_handler.new_connection_counter());
279 conn.reset();
280 ASSERT_EQ(0, m_handler_config.get_connections_count());
281 ASSERT_EQ(0, m_commands_handler.close_connection_counter());
282}
283
284TEST_F(positive_test_connection_to_levin_protocol_handler_calls, handler_initialization_and_destruction_is_correct)
285{
286 test_connection_ptr conn = create_connection();
287 ASSERT_TRUE(conn->m_protocol_handler.m_connection_initialized);
288 ASSERT_EQ(1, m_handler_config.get_connections_count());
289 ASSERT_EQ(1, m_commands_handler.new_connection_counter());
290 conn.reset();
291 ASSERT_EQ(0, m_handler_config.get_connections_count());
292 ASSERT_EQ(1, m_commands_handler.close_connection_counter());
293}
294
295TEST_F(positive_test_connection_to_levin_protocol_handler_calls, concurent_handler_initialization_and_destruction_is_correct)
296{
297 const size_t connection_count = 10000;
298 auto create_and_destroy_connections = [this]()
299 {
300 std::vector<test_connection_ptr> connections(connection_count);
301 for (size_t i = 0; i < connection_count; ++i)
302 {
303 connections[i] = create_connection();
304 }
305
306 for (size_t i = 0; i < connection_count; ++i)
307 {
308 connections[i].reset();
309 }
310 };
311
312 const size_t thread_count = boost::thread::hardware_concurrency();
313 std::vector<boost::thread> threads(thread_count);
314 for (boost::thread& th : threads)
315 {
316 th = boost::thread(create_and_destroy_connections);
317 }
318
319 for (boost::thread& th : threads)
320 {
321 th.join();
322 }
323
324 ASSERT_EQ(0, m_handler_config.get_connections_count());
325 ASSERT_EQ(connection_count * thread_count, m_commands_handler.new_connection_counter());
326 ASSERT_EQ(connection_count * thread_count, m_commands_handler.close_connection_counter());
327}
328
329TEST_F(positive_test_connection_to_levin_protocol_handler_calls, handler_processes_handle_read_as_invoke)
330{
331 // Setup
332 const int expected_command = 2634981;
333 const int expected_return_code = 6732;
334 const std::string expected_out_data(128, 'w');
335
336 test_connection_ptr conn = create_connection();
337
338 std::string in_data(256, 'q');
339
341 req_head.m_signature = LEVIN_SIGNATURE;
342 req_head.m_cb = in_data.size();
343 req_head.m_have_to_return_data = true;
344 req_head.m_command = expected_command;
345 req_head.m_flags = LEVIN_PACKET_REQUEST;
347
348 std::string buf(reinterpret_cast<const char*>(&req_head), sizeof(req_head));
349 buf += in_data;
350
351 m_commands_handler.invoke_out_buf(expected_out_data);
352 m_commands_handler.return_code(expected_return_code);
353
354 // Test
355 ASSERT_TRUE(conn->m_protocol_handler.handle_recv(buf.data(), buf.size()));
356
357 //
358 // Check
359 //
360
361 // Check connection and levin_commands_handler states
362 ASSERT_EQ(1, m_commands_handler.invoke_counter());
363 ASSERT_EQ(0, m_commands_handler.notify_counter());
364 ASSERT_EQ(expected_command, m_commands_handler.last_command());
365 ASSERT_EQ(in_data, m_commands_handler.last_in_buf());
366 ASSERT_LE(1, conn->send_counter());
367
368 // Parse send data
369 std::string send_data = conn->last_send_data();
371 resp_head = *reinterpret_cast<const epee::levin::bucket_head2*>(send_data.data());
372 ASSERT_LT(sizeof(resp_head), send_data.size());
373 std::string out_data = send_data.substr(sizeof(resp_head));
374
375 // Check sent response
376 ASSERT_EQ(expected_out_data, out_data);
378 ASSERT_EQ(expected_command, resp_head.m_command);
379 ASSERT_EQ(expected_return_code, resp_head.m_return_code);
380 ASSERT_EQ(expected_out_data.size(), resp_head.m_cb);
383 ASSERT_TRUE(0 != (resp_head.m_flags & LEVIN_PACKET_RESPONSE));
384}
385
386TEST_F(positive_test_connection_to_levin_protocol_handler_calls, handler_processes_handle_read_as_notify)
387{
388 // Setup
389 const int expected_command = 4673261;
390
391 test_connection_ptr conn = create_connection();
392
393 std::string in_data(256, 'e');
394
396 req_head.m_signature = LEVIN_SIGNATURE;
397 req_head.m_cb = in_data.size();
398 req_head.m_have_to_return_data = false;
399 req_head.m_command = expected_command;
400 req_head.m_flags = LEVIN_PACKET_REQUEST;
402
403 std::string buf(reinterpret_cast<const char*>(&req_head), sizeof(req_head));
404 buf += in_data;
405
406 // Test
407 ASSERT_TRUE(conn->m_protocol_handler.handle_recv(buf.data(), buf.size()));
408
409 // Check connection and levin_commands_handler states
410 ASSERT_EQ(1, m_commands_handler.notify_counter());
411 ASSERT_EQ(0, m_commands_handler.invoke_counter());
412 ASSERT_EQ(expected_command, m_commands_handler.last_command());
413 ASSERT_EQ(in_data, m_commands_handler.last_in_buf());
414 ASSERT_LE(0, conn->send_counter());
415 ASSERT_TRUE(conn->last_send_data().empty());
416}
417
418TEST_F(positive_test_connection_to_levin_protocol_handler_calls, handler_processes_qued_callback)
419{
420 test_connection_ptr conn = create_connection();
421
422 conn->m_protocol_handler.handle_qued_callback();
423 conn->m_protocol_handler.handle_qued_callback();
424 conn->m_protocol_handler.handle_qued_callback();
425
426 ASSERT_EQ(3, m_commands_handler.callback_counter());
427}
428
429TEST_F(test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_big_packet_1)
430{
431 std::string buf("yyyyyy");
432 ASSERT_FALSE(m_conn->m_protocol_handler.handle_recv(buf.data(), max_packet_size + 1));
433}
434
435TEST_F(test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_big_packet_2)
436{
437 prepare_buf();
438 const size_t first_packet_size = sizeof(m_req_head) - 1;
439
440 m_buf.resize(first_packet_size);
441 ASSERT_TRUE(m_conn->m_protocol_handler.handle_recv(m_buf.data(), m_buf.size()));
442
443 ASSERT_FALSE(m_conn->m_protocol_handler.handle_recv(m_buf.data(), max_packet_size - m_buf.size() + 1));
444}
445
446TEST_F(test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_invalid_signature_for_full_header)
447{
448 m_req_head.m_signature = LEVIN_SIGNATURE ^ 1;
449 prepare_buf();
450
451 ASSERT_FALSE(m_conn->m_protocol_handler.handle_recv(m_buf.data(), m_buf.size()));
452}
453
454TEST_F(test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_invalid_signature_for_partial_header)
455{
456 m_req_head.m_signature = LEVIN_SIGNATURE ^ 1;
457 prepare_buf();
458 m_buf.resize(sizeof(m_req_head.m_signature));
459
460 ASSERT_FALSE(m_conn->m_protocol_handler.handle_recv(m_buf.data(), m_buf.size()));
461}
462
463TEST_F(test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_big_cb)
464{
465 m_req_head.m_cb = max_packet_size + 1;
466 prepare_buf();
467
468 ASSERT_FALSE(m_conn->m_protocol_handler.handle_recv(m_buf.data(), m_buf.size()));
469}
470
471TEST_F(test_levin_protocol_handler__hanle_recv_with_invalid_data, does_not_handle_data_after_close)
472{
473 prepare_buf();
474
475 ASSERT_TRUE(m_conn->m_protocol_handler.close());
476 ASSERT_FALSE(m_conn->m_protocol_handler.handle_recv(m_buf.data(), m_buf.size()));
477}
478
479TEST_F(test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_network_error)
480{
481 prepare_buf();
482
483 m_conn->send_return(false);
484 ASSERT_FALSE(m_conn->m_protocol_handler.handle_recv(m_buf.data(), m_buf.size()));
485}
486
487TEST_F(test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_chunked_header)
488{
489 prepare_buf();
490
491 size_t buf1_size = sizeof(m_req_head) / 2;
492
493 std::string buf1 = m_buf.substr(0, buf1_size);
494 std::string buf2 = m_buf.substr(buf1_size);
495 ASSERT_EQ(m_buf, buf1 + buf2);
496
497 ASSERT_TRUE(m_conn->m_protocol_handler.handle_recv(buf1.data(), buf1.size()));
498 ASSERT_EQ(0, m_commands_handler.invoke_counter());
499
500 ASSERT_TRUE(m_conn->m_protocol_handler.handle_recv(buf2.data(), buf2.size()));
501 ASSERT_EQ(1, m_commands_handler.invoke_counter());
502}
503
504
505TEST_F(test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_chunked_body)
506{
507 prepare_buf();
508
509 size_t buf1_size = sizeof(m_req_head) + (m_buf.size() - sizeof(m_req_head)) / 2;
510
511 std::string buf1 = m_buf.substr(0, buf1_size);
512 std::string buf2 = m_buf.substr(buf1_size);
513 ASSERT_EQ(m_buf, buf1 + buf2);
514
515 ASSERT_TRUE(m_conn->m_protocol_handler.handle_recv(buf1.data(), buf1.size()));
516 ASSERT_EQ(0, m_commands_handler.invoke_counter());
517
518 ASSERT_TRUE(m_conn->m_protocol_handler.handle_recv(buf2.data(), buf2.size()));
519 ASSERT_EQ(1, m_commands_handler.invoke_counter());
520}
521
522TEST_F(test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_two_requests_at_once)
523{
524 prepare_buf();
525 m_buf.append(m_buf);
526
527 ASSERT_TRUE(m_conn->m_protocol_handler.handle_recv(m_buf.data(), m_buf.size()));
528 ASSERT_EQ(2, m_commands_handler.invoke_counter());
529}
530
531TEST_F(test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_unexpected_response)
532{
533 m_req_head.m_flags = LEVIN_PACKET_RESPONSE;
534 prepare_buf();
535
536 ASSERT_FALSE(m_conn->m_protocol_handler.handle_recv(m_buf.data(), m_buf.size()));
537}
constexpr std::size_t size() const noexcept
Definition span.h:111
constexpr pointer data() const noexcept
Definition span.h:110
#define TEST_F(test_fixture, test_name)
Definition gtest.h:2216
#define ASSERT_EQ(val1, val2)
Definition gtest.h:1956
#define ASSERT_LE(val1, val2)
Definition gtest.h:1964
#define ASSERT_FALSE(condition)
Definition gtest.h:1868
#define ASSERT_TRUE(condition)
Definition gtest.h:1865
#define ASSERT_LT(val1, val2)
Definition gtest.h:1968
#define LEVIN_PACKET_RESPONSE
Definition levin_base.h:74
#define LEVIN_PROTOCOL_VER_1
Definition levin_base.h:78
#define LEVIN_PACKET_REQUEST
Definition levin_base.h:73
#define LEVIN_OK
Definition levin_base.h:93
#define LEVIN_SIGNATURE
Definition levin_base.h:34
epee::levin::async_protocol_handler_config< test_connection_context > test_levin_protocol_handler_config
epee::net_utils::connection< test_levin_protocol_handler > test_connection
epee::levin::async_protocol_handler< test_connection_context > test_levin_protocol_handler
const char * buf
#define true
unsigned __int64 uint64_t
Definition stdint.h:136