Debian Codegraph @

tests/unit_tests/epee_levin_protocol_handler_async.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/thread/mutex.hpp>
32 #include <boost/thread/thread.hpp>
33
34 #include "gtest/gtest.h"
35
36 #include "include_base_utils.h"
37 #include "string_tools.h"
38 #include "net/levin_protocol_handler_async.h"
39 #include "net/net_utils_base.h"
40 #include "unit_tests_utils.h"
41
42 namespace
43 {
44 struct test_levin_connection_context : public epee::net_utils::connection_context_base
45 {
46 static constexpr int handshake_command() noexcept { return 1001; }
47 static constexpr bool handshake_complete() noexcept { return true; }
48 size_t get_max_bytes(int command) const { return LEVIN_DEFAULT_MAX_PACKET_SIZE; }
49 };
50
51 typedef epee::levin::async_protocol_handler_config<test_levin_connection_context> test_levin_protocol_handler_config;
52 typedef epee::levin::async_protocol_handler<test_levin_connection_context> test_levin_protocol_handler;
53
54 struct test_levin_commands_handler : public epee::levin::levin_commands_handler<test_levin_connection_context>
55 {
56 test_levin_commands_handler()
57 : m_return_code(LEVIN_OK)
58 , m_last_command(-1)
59 {
60 }
61
62 virtual int invoke(int command, const epee::span<const uint8_t> in_buff, epee::byte_stream& buff_out, test_levin_connection_context& context)
63 {
64 m_invoke_counter.inc();
65 boost::unique_lock<boost::mutex> lock(m_mutex);
66 m_last_command = command;
67 m_last_in_buf = std::string((const char*)in_buff.data(), in_buff.size());
68 buff_out.write(epee::to_span(m_invoke_out_buf));
69 return m_return_code;
70 }
71
72 virtual int notify(int command, const epee::span<const uint8_t> in_buff, test_levin_connection_context& context)
73 {
74 m_notify_counter.inc();
75 boost::unique_lock<boost::mutex> lock(m_mutex);
76 m_last_command = command;
77 m_last_in_buf = std::string((const char*)in_buff.data(), in_buff.size());
78 return m_return_code;
79 }
80
81 virtual void callback(test_levin_connection_context& context)
82 {
83 m_callback_counter.inc();
84 //std::cout << "test_levin_commands_handler::callback()" << std::endl;
85 }
86
87 virtual void on_connection_new(test_levin_connection_context& context)
88 {
89 m_new_connection_counter.inc();
90 //std::cout << "test_levin_commands_handler::on_connection_new()" << std::endl;
91 }
92
93 virtual void on_connection_close(test_levin_connection_context& context)
94 {
95 m_close_connection_counter.inc();
96 //std::cout << "test_levin_commands_handler::on_connection_close()" << std::endl;
97 }
98
99 size_t invoke_counter() const { return m_invoke_counter.get(); }
100 size_t notify_counter() const { return m_notify_counter.get(); }
101 size_t callback_counter() const { return m_callback_counter.get(); }
102 size_t new_connection_counter() const { return m_new_connection_counter.get(); }
103 size_t close_connection_counter() const { return m_close_connection_counter.get(); }
104
105 int return_code() const { return m_return_code; }
106 void return_code(int v) { m_return_code = v; }
107
108 void invoke_out_buf(std::string v) { m_invoke_out_buf = epee::byte_slice{std::move(v)}; }
109
110 int last_command() const { return m_last_command; }
111 const std::string& last_in_buf() const { return m_last_in_buf; }
112
113 private:
114 unit_test::call_counter m_invoke_counter;
115 unit_test::call_counter m_notify_counter;
116 unit_test::call_counter m_callback_counter;
117 unit_test::call_counter m_new_connection_counter;
118 unit_test::call_counter m_close_connection_counter;
119
120 boost::mutex m_mutex;
121
122 int m_return_code;
123 epee::byte_slice m_invoke_out_buf;
124
125 int m_last_command;
126 std::string m_last_in_buf;
127 };
128
129 class test_connection : public epee::net_utils::i_service_endpoint
130 {
131 public:
132 test_connection(boost::asio::io_context& io_service, test_levin_protocol_handler_config& protocol_config)
133 : m_io_service(io_service)
134 , m_protocol_handler(this, protocol_config, m_context)
135 , m_send_return(true)
136 {
137 }
138
139 void start()
140 {
141 ASSERT_TRUE(m_protocol_handler.after_init_connection());
142 }
143
144 // Implement epee::net_utils::i_service_endpoint interface
145 virtual bool do_send(epee::byte_slice message)
146 {
147 //std::cout << "test_connection::do_send()" << std::endl;
148 m_send_counter.inc();
149 boost::unique_lock<boost::mutex> lock(m_mutex);
150 m_last_send_data.append(reinterpret_cast<const char*>(message.data()), message.size());
151 return m_send_return;
152 }
153
154 virtual bool close(const bool wait_for_shutdown) { /*std::cout << "test_connection::close()" << std::endl; */return true; }
155 virtual bool send_done() { /*std::cout << "test_connection::send_done()" << std::endl; */return true; }
156 virtual bool call_run_once_service_io() { std::cout << "test_connection::call_run_once_service_io()" << std::endl; return true; }
157 virtual bool request_callback() { std::cout << "test_connection::request_callback()" << std::endl; return true; }
158 virtual boost::asio::io_context& get_io_context() { std::cout << "test_connection::get_io_context()" << std::endl; return m_io_service; }
159 virtual bool add_ref() { std::cout << "test_connection::add_ref()" << std::endl; return true; }
160 virtual bool release() { std::cout << "test_connection::release()" << std::endl; return true; }
161
162 size_t send_counter() const { return m_send_counter.get(); }
163
164 const std::string& last_send_data() const { return m_last_send_data; }
165 void reset_last_send_data() { boost::unique_lock<boost::mutex> lock(m_mutex); m_last_send_data.clear(); }
166
167 bool send_return() const { return m_send_return; }
168 void send_return(bool v) { m_send_return = v; }
169
170 public:
171 test_levin_protocol_handler m_protocol_handler;
172
173 private:
174 boost::asio::io_context& m_io_service;
175 test_levin_connection_context m_context;
176
177 unit_test::call_counter m_send_counter;
178 boost::mutex m_mutex;
179
180 std::string m_last_send_data;
181
182 bool m_send_return;
183 };
184
185 class async_protocol_handler_test : public ::testing::Test
186 {
187 public:
188 const static uint64_t invoke_timeout = 5 * 1000;
189 const static size_t max_packet_size = 10 * 1024 * 1024;
190
191 typedef std::unique_ptr<test_connection> test_connection_ptr;
192
193 async_protocol_handler_test():
194 m_pcommands_handler(new test_levin_commands_handler()),
195 m_commands_handler(*m_pcommands_handler)
196 {
197 m_handler_config.set_handler(m_pcommands_handler, [](epee::levin::levin_commands_handler<test_levin_connection_context> *handler) { delete handler; });
198 m_handler_config.m_invoke_timeout = invoke_timeout;
199 m_handler_config.m_initial_max_packet_size = max_packet_size;
200 m_handler_config.m_max_packet_size = max_packet_size;
201 }
202
203 virtual void SetUp()
204 {
205 }
206
207 protected:
208 test_connection_ptr create_connection(bool start = true)
209 {
210 test_connection_ptr conn(new test_connection(m_io_service, m_handler_config));
211 if (start)
212 {
213 conn->start();
214 }
215 return conn;
216 }
217
218 protected:
219 boost::asio::io_context m_io_service;
220 test_levin_protocol_handler_config m_handler_config;
221 test_levin_commands_handler *m_pcommands_handler, &m_commands_handler;
222 };
223
224 class positive_test_connection_to_levin_protocol_handler_calls : public async_protocol_handler_test
225 {
226 };
227
228 class test_levin_protocol_handler__hanle_recv_with_invalid_data : public async_protocol_handler_test
229 {
230 public:
231 static const int expected_command = 5615871;
232 static const int expected_return_code = 782546;
233
234 test_levin_protocol_handler__hanle_recv_with_invalid_data()
235 : m_expected_invoke_out_buf(512, 'y')
236 {
237 }
238
239 virtual void SetUp()
240 {
241 async_protocol_handler_test::SetUp();
242
243 m_conn = create_connection();
244
245 m_in_data.assign(256, 't');
246
247 m_req_head.m_signature = SWAP64LE(LEVIN_SIGNATURE);
248 m_req_head.m_cb = SWAP64LE(m_in_data.size());
249 m_req_head.m_have_to_return_data = true;
250 m_req_head.m_command = SWAP32LE(expected_command);
251 m_req_head.m_return_code = SWAP32LE(LEVIN_OK);
252 m_req_head.m_flags = SWAP32LE(LEVIN_PACKET_REQUEST);
253 m_req_head.m_protocol_version = SWAP32LE(LEVIN_PROTOCOL_VER_1);
254
255 m_commands_handler.return_code(expected_return_code);
256 m_commands_handler.invoke_out_buf(m_expected_invoke_out_buf);
257 }
258
259 protected:
260 void prepare_buf()
261 {
262 m_buf.assign(reinterpret_cast<const char*>(&m_req_head), sizeof(m_req_head));
263 m_buf += m_in_data;
264 }
265
266 protected:
267 test_connection_ptr m_conn;
268 epee::levin::bucket_head2 m_req_head;
269 std::string m_in_data;
270 std::string m_buf;
271 std::string m_expected_invoke_out_buf;
272 };
273 }
274
275 TEST_F(positive_test_connection_to_levin_protocol_handler_calls, new_handler_is_not_initialized)
276 {
277 test_connection_ptr conn = create_connection(false);
278 ASSERT_FALSE(conn->m_protocol_handler.m_connection_initialized);
279 ASSERT_EQ(0, m_handler_config.get_connections_count());
280 ASSERT_EQ(0, m_commands_handler.new_connection_counter());
281 conn.reset();
282 ASSERT_EQ(0, m_handler_config.get_connections_count());
283 ASSERT_EQ(0, m_commands_handler.close_connection_counter());
284 }
285
286 TEST_F(positive_test_connection_to_levin_protocol_handler_calls, handler_initialization_and_destruction_is_correct)
287 {
288 test_connection_ptr conn = create_connection();
289 ASSERT_TRUE(conn->m_protocol_handler.m_connection_initialized);
290 ASSERT_EQ(1, m_handler_config.get_connections_count());
291 ASSERT_EQ(1, m_commands_handler.new_connection_counter());
292 conn.reset();
293 ASSERT_EQ(0, m_handler_config.get_connections_count());
294 ASSERT_EQ(1, m_commands_handler.close_connection_counter());
295 }
296
297 TEST_F(positive_test_connection_to_levin_protocol_handler_calls, concurent_handler_initialization_and_destruction_is_correct)
298 {
299 const size_t connection_count = 10000;
300 auto create_and_destroy_connections = [this]()
301 {
302 std::vector<test_connection_ptr> connections(connection_count);
303 for (size_t i = 0; i < connection_count; ++i)
304 {
305 connections[i] = create_connection();
306 }
307
308 for (size_t i = 0; i < connection_count; ++i)
309 {
310 connections[i].reset();
311 }
312 };
313
314 const size_t thread_count = boost::thread::hardware_concurrency();
315 std::vector<boost::thread> threads(thread_count);
316 for (boost::thread& th : threads)
317 {
318 th = boost::thread(create_and_destroy_connections);
319 }
320
321 for (boost::thread& th : threads)
322 {
323 th.join();
324 }
325
326 ASSERT_EQ(0, m_handler_config.get_connections_count());
327 ASSERT_EQ(connection_count * thread_count, m_commands_handler.new_connection_counter());
328 ASSERT_EQ(connection_count * thread_count, m_commands_handler.close_connection_counter());
329 }
330
331 TEST_F(positive_test_connection_to_levin_protocol_handler_calls, handler_processes_handle_read_as_invoke)
332 {
333 // Setup
334 const int expected_command = 2634981;
335 const int expected_return_code = 6732;
336 const std::string expected_out_data(128, 'w');
337
338 test_connection_ptr conn = create_connection();
339
340 std::string in_data(256, 'q');
341
342 epee::levin::bucket_head2 req_head;
343 req_head.m_signature = SWAP64LE(LEVIN_SIGNATURE);
344 req_head.m_cb = SWAP64LE(in_data.size());
345 req_head.m_have_to_return_data = true;
346 req_head.m_command = SWAP32LE(expected_command);
347 req_head.m_flags = SWAP32LE(LEVIN_PACKET_REQUEST);
348 req_head.m_protocol_version = SWAP32LE(LEVIN_PROTOCOL_VER_1);
349
350 std::string buf(reinterpret_cast<const char*>(&req_head), sizeof(req_head));
351 buf += in_data;
352
353 m_commands_handler.invoke_out_buf(expected_out_data);
354 m_commands_handler.return_code(expected_return_code);
355
356 // Test
357 ASSERT_TRUE(conn->m_protocol_handler.handle_recv(buf.data(), buf.size()));
358
359 //
360 // Check
361 //
362
363 // Check connection and levin_commands_handler states
364 ASSERT_EQ(1, m_commands_handler.invoke_counter());
365 ASSERT_EQ(0, m_commands_handler.notify_counter());
366 ASSERT_EQ(expected_command, m_commands_handler.last_command());
367 ASSERT_EQ(in_data, m_commands_handler.last_in_buf());
368 ASSERT_LE(1, conn->send_counter());
369
370 // Parse send data
371 std::string send_data = conn->last_send_data();
372 epee::levin::bucket_head2 resp_head;
373 ASSERT_LT(sizeof(resp_head), send_data.size());
374 std::memcpy(std::addressof(resp_head), send_data.data(), sizeof(resp_head));
375 std::string out_data = send_data.substr(sizeof(resp_head));
376
377 // Check sent response
378 ASSERT_EQ(expected_out_data, out_data);
379 ASSERT_EQ(LEVIN_SIGNATURE, SWAP64LE(resp_head.m_signature));
380 ASSERT_EQ(expected_command, SWAP32LE(resp_head.m_command));
381 ASSERT_EQ(expected_return_code, SWAP32LE(resp_head.m_return_code));
382 ASSERT_EQ(expected_out_data.size(), SWAP64LE(resp_head.m_cb));
383 ASSERT_FALSE(resp_head.m_have_to_return_data);
384 ASSERT_EQ(SWAP32LE(LEVIN_PROTOCOL_VER_1), resp_head.m_protocol_version);
385 ASSERT_TRUE(0 != (SWAP32LE(resp_head.m_flags) & LEVIN_PACKET_RESPONSE));
386 }
387
388 TEST_F(positive_test_connection_to_levin_protocol_handler_calls, handler_processes_handle_read_as_notify)
389 {
390 // Setup
391 const int expected_command = 4673261;
392
393 test_connection_ptr conn = create_connection();
394
395 std::string in_data(256, 'e');
396
397 epee::levin::bucket_head2 req_head;
398 req_head.m_signature = SWAP64LE(LEVIN_SIGNATURE);
399 req_head.m_cb = SWAP64LE(in_data.size());
400 req_head.m_have_to_return_data = false;
401 req_head.m_command = SWAP32LE(expected_command);
402 req_head.m_flags = SWAP32LE(LEVIN_PACKET_REQUEST);
403 req_head.m_protocol_version = SWAP32LE(LEVIN_PROTOCOL_VER_1);
404
405 std::string buf(reinterpret_cast<const char*>(&req_head), sizeof(req_head));
406 buf += in_data;
407
408 // Test
409 ASSERT_TRUE(conn->m_protocol_handler.handle_recv(buf.data(), buf.size()));
410
411 // Check connection and levin_commands_handler states
412 ASSERT_EQ(1, m_commands_handler.notify_counter());
413 ASSERT_EQ(0, m_commands_handler.invoke_counter());
414 ASSERT_EQ(expected_command, m_commands_handler.last_command());
415 ASSERT_EQ(in_data, m_commands_handler.last_in_buf());
416 ASSERT_LE(0, conn->send_counter());
417 ASSERT_TRUE(conn->last_send_data().empty());
418 }
419
420 TEST_F(positive_test_connection_to_levin_protocol_handler_calls, handler_processes_qued_callback)
421 {
422 test_connection_ptr conn = create_connection();
423
424 conn->m_protocol_handler.handle_qued_callback();
425 conn->m_protocol_handler.handle_qued_callback();
426 conn->m_protocol_handler.handle_qued_callback();
427
428 ASSERT_EQ(3, m_commands_handler.callback_counter());
429 }
430
431 TEST_F(positive_test_connection_to_levin_protocol_handler_calls, handler_processes_handle_read_as_dummy)
432 {
433 // Setup
434 const int expected_command = 4673261;
435 const std::string in_data(256, 'e');
436
437 epee::levin::message_writer message{};
438 message.buffer.write(epee::to_span(in_data));
439
440 const epee::byte_slice noise = epee::levin::make_noise_notify(1024);
441 const epee::byte_slice notify = message.finalize_notify(expected_command);
442
443 test_connection_ptr conn = create_connection();
444
445 // Test
446 ASSERT_TRUE(conn->m_protocol_handler.handle_recv(noise.data(), noise.size()));
447
448 // Check connection and levin_commands_handler states
449 ASSERT_EQ(0u, m_commands_handler.notify_counter());
450 ASSERT_EQ(0u, m_commands_handler.invoke_counter());
451 ASSERT_EQ(-1, m_commands_handler.last_command());
452 ASSERT_TRUE(m_commands_handler.last_in_buf().empty());
453 ASSERT_EQ(0u, conn->send_counter());
454 ASSERT_TRUE(conn->last_send_data().empty());
455
456
457 ASSERT_TRUE(conn->m_protocol_handler.handle_recv(notify.data(), notify.size()));
458
459 // Check connection and levin_commands_handler states
460 ASSERT_EQ(1u, m_commands_handler.notify_counter());
461 ASSERT_EQ(0u, m_commands_handler.invoke_counter());
462 ASSERT_EQ(expected_command, m_commands_handler.last_command());
463 ASSERT_EQ(in_data, m_commands_handler.last_in_buf());
464 ASSERT_EQ(0u, conn->send_counter());
465 ASSERT_TRUE(conn->last_send_data().empty());
466 }
467
468 TEST_F(positive_test_connection_to_levin_protocol_handler_calls, handler_processes_handle_read_as_fragment)
469 {
470 // Setup
471 const int expected_command = 4673261;
472 const int expected_fragmented_command = 46732;
473 const std::string in_data(256, 'e');
474
475 epee::levin::message_writer message{};
476 message.buffer.write(epee::to_span(in_data));
477
478 epee::levin::message_writer in_fragmented_data;
479 in_fragmented_data.buffer.put_n('c', 1024 * 4);
480
481 const epee::byte_slice noise = epee::levin::make_noise_notify(1024);
482 const epee::byte_slice notify = message.finalize_notify(expected_command);
483 epee::byte_slice fragmented = epee::levin::make_fragmented_notify(noise.size(), expected_fragmented_command, std::move(in_fragmented_data));
484
485 EXPECT_EQ(5u, fragmented.size() / 1024);
486 EXPECT_EQ(0u, fragmented.size() % 1024);
487
488 test_connection_ptr conn = create_connection();
489
490 while (!fragmented.empty())
491 {
492 if ((fragmented.size() / 1024) % 2 == 1)
493 {
494 ASSERT_TRUE(conn->m_protocol_handler.handle_recv(notify.data(), notify.size()));
495 }
496
497 ASSERT_EQ(3u - (fragmented.size() / 2048), m_commands_handler.notify_counter());
498 ASSERT_EQ(0u, m_commands_handler.invoke_counter());
499 ASSERT_EQ(expected_command, m_commands_handler.last_command());
500 ASSERT_EQ(in_data, m_commands_handler.last_in_buf());
501 ASSERT_EQ(0u, conn->send_counter());
502 ASSERT_TRUE(conn->last_send_data().empty());
503
504 epee::byte_slice next = fragmented.take_slice(1024);
505 ASSERT_TRUE(conn->m_protocol_handler.handle_recv(next.data(), next.size()));
506 }
507
508 std::string compare_buffer(1024 * 4, 'c');
509
510 ASSERT_EQ(4u, m_commands_handler.notify_counter());
511 ASSERT_EQ(0u, m_commands_handler.invoke_counter());
512 ASSERT_EQ(expected_fragmented_command, m_commands_handler.last_command());
513 ASSERT_EQ(compare_buffer, m_commands_handler.last_in_buf());
514 ASSERT_EQ(0u, conn->send_counter());
515 ASSERT_TRUE(conn->last_send_data().empty());
516
517
518 ASSERT_TRUE(conn->m_protocol_handler.handle_recv(notify.data(), notify.size()));
519
520 ASSERT_EQ(5u, m_commands_handler.notify_counter());
521 ASSERT_EQ(0u, m_commands_handler.invoke_counter());
522 ASSERT_EQ(expected_command, m_commands_handler.last_command());
523 ASSERT_EQ(in_data, m_commands_handler.last_in_buf());
524 ASSERT_EQ(0u, conn->send_counter());
525 ASSERT_TRUE(conn->last_send_data().empty());
526 }
527
528
529 TEST_F(test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_big_packet_1)
530 {
531 std::string buf("yyyyyy");
532 ASSERT_FALSE(m_conn->m_protocol_handler.handle_recv(buf.data(), max_packet_size + 1));
533 }
534
535 TEST_F(test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_big_packet_2)
536 {
537 prepare_buf();
538 const size_t first_packet_size = sizeof(m_req_head) - 1;
539
540 m_buf.resize(first_packet_size);
541 ASSERT_TRUE(m_conn->m_protocol_handler.handle_recv(m_buf.data(), m_buf.size()));
542
543 ASSERT_FALSE(m_conn->m_protocol_handler.handle_recv(m_buf.data(), max_packet_size - m_buf.size() + 1));
544 }
545
546 TEST_F(test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_invalid_signature_for_full_header)
547 {
548 m_req_head.m_signature = LEVIN_SIGNATURE ^ 1;
549 prepare_buf();
550
551 ASSERT_FALSE(m_conn->m_protocol_handler.handle_recv(m_buf.data(), m_buf.size()));
552 }
553
554 TEST_F(test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_invalid_signature_for_partial_header)
555 {
556 m_req_head.m_signature = LEVIN_SIGNATURE ^ 1;
557 prepare_buf();
558 m_buf.resize(sizeof(m_req_head.m_signature));
559
560 ASSERT_FALSE(m_conn->m_protocol_handler.handle_recv(m_buf.data(), m_buf.size()));
561 }
562
563 TEST_F(test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_big_cb)
564 {
565 m_req_head.m_cb = max_packet_size + 1;
566 prepare_buf();
567
568 ASSERT_FALSE(m_conn->m_protocol_handler.handle_recv(m_buf.data(), m_buf.size()));
569 }
570
571 TEST_F(test_levin_protocol_handler__hanle_recv_with_invalid_data, does_not_handle_data_after_close)
572 {
573 prepare_buf();
574
575 ASSERT_TRUE(m_conn->m_protocol_handler.close(true));
576 ASSERT_FALSE(m_conn->m_protocol_handler.handle_recv(m_buf.data(), m_buf.size()));
577 }
578
579 TEST_F(test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_network_error)
580 {
581 prepare_buf();
582
583 m_conn->send_return(false);
584 ASSERT_FALSE(m_conn->m_protocol_handler.handle_recv(m_buf.data(), m_buf.size()));
585 }
586
587 TEST_F(test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_chunked_header)
588 {
589 prepare_buf();
590
591 size_t buf1_size = sizeof(m_req_head) / 2;
592
593 std::string buf1 = m_buf.substr(0, buf1_size);
594 std::string buf2 = m_buf.substr(buf1_size);
595 ASSERT_EQ(m_buf, buf1 + buf2);
596
597 ASSERT_TRUE(m_conn->m_protocol_handler.handle_recv(buf1.data(), buf1.size()));
598 ASSERT_EQ(0, m_commands_handler.invoke_counter());
599
600 ASSERT_TRUE(m_conn->m_protocol_handler.handle_recv(buf2.data(), buf2.size()));
601 ASSERT_EQ(1, m_commands_handler.invoke_counter());
602 }
603
604
605 TEST_F(test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_chunked_body)
606 {
607 prepare_buf();
608
609 size_t buf1_size = sizeof(m_req_head) + (m_buf.size() - sizeof(m_req_head)) / 2;
610
611 std::string buf1 = m_buf.substr(0, buf1_size);
612 std::string buf2 = m_buf.substr(buf1_size);
613 ASSERT_EQ(m_buf, buf1 + buf2);
614
615 ASSERT_TRUE(m_conn->m_protocol_handler.handle_recv(buf1.data(), buf1.size()));
616 ASSERT_EQ(0, m_commands_handler.invoke_counter());
617
618 ASSERT_TRUE(m_conn->m_protocol_handler.handle_recv(buf2.data(), buf2.size()));
619 ASSERT_EQ(1, m_commands_handler.invoke_counter());
620 }
621
622 TEST_F(test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_two_requests_at_once)
623 {
624 prepare_buf();
625 m_buf.append(m_buf);
626
627 ASSERT_TRUE(m_conn->m_protocol_handler.handle_recv(m_buf.data(), m_buf.size()));
628 ASSERT_EQ(2, m_commands_handler.invoke_counter());
629 }
630
631 TEST_F(test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_unexpected_response)
632 {
633 m_req_head.m_flags = SWAP32LE(LEVIN_PACKET_RESPONSE);
634 prepare_buf();
635
636 ASSERT_FALSE(m_conn->m_protocol_handler.handle_recv(m_buf.data(), m_buf.size()));
637 }
638
639 TEST_F(test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_short_fragment)
640 {
641 m_req_head.m_cb = 1;
642 m_req_head.m_flags = LEVIN_PACKET_BEGIN;
643 m_req_head.m_command = 0;
644 m_in_data.resize(1);
645 prepare_buf();
646
647 ASSERT_TRUE(m_conn->m_protocol_handler.handle_recv(m_buf.data(), m_buf.size()));
648
649 m_req_head.m_flags = LEVIN_PACKET_END;
650 prepare_buf();
651
652 ASSERT_FALSE(m_conn->m_protocol_handler.handle_recv(m_buf.data(), m_buf.size()));
653 }
654
655 TEST_F(test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_bad_cb)
656 {
657 m_req_head.m_cb = sizeof(epee::levin::bucket_head2);
658 m_req_head.m_flags = LEVIN_PACKET_BEGIN;
659 m_req_head.m_command = 0;
660
661 epee::levin::bucket_head2 inner{};
662 inner.m_cb = 2;
663 m_in_data.resize(sizeof(epee::levin::bucket_head2));
664 prepare_buf();
665
666 ASSERT_TRUE(m_conn->m_protocol_handler.handle_recv(m_buf.data(), m_buf.size()));
667
668 m_req_head.m_cb = 1;
669 m_req_head.m_flags = LEVIN_PACKET_END;
670 m_in_data.resize(1);
671 prepare_buf();
672
673 ASSERT_FALSE(m_conn->m_protocol_handler.handle_recv(m_buf.data(), m_buf.size()));
674 }