Electroneum
Loading...
Searching...
No Matches
epee_levin_protocol_handler_async.cpp File Reference
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>
#include "gtest/gtest.h"
#include "include_base_utils.h"
#include "string_tools.h"
#include "net/levin_protocol_handler_async.h"
#include "net/net_utils_base.h"
#include "unit_tests_utils.h"
Include dependency graph for epee_levin_protocol_handler_async.cpp:

Go to the source code of this file.

Functions

 TEST_F (positive_test_connection_to_levin_protocol_handler_calls, new_handler_is_not_initialized)
 TEST_F (positive_test_connection_to_levin_protocol_handler_calls, handler_initialization_and_destruction_is_correct)
 TEST_F (positive_test_connection_to_levin_protocol_handler_calls, concurent_handler_initialization_and_destruction_is_correct)
 TEST_F (positive_test_connection_to_levin_protocol_handler_calls, handler_processes_handle_read_as_invoke)
 TEST_F (positive_test_connection_to_levin_protocol_handler_calls, handler_processes_handle_read_as_notify)
 TEST_F (positive_test_connection_to_levin_protocol_handler_calls, handler_processes_qued_callback)
 TEST_F (test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_big_packet_1)
 TEST_F (test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_big_packet_2)
 TEST_F (test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_invalid_signature_for_full_header)
 TEST_F (test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_invalid_signature_for_partial_header)
 TEST_F (test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_big_cb)
 TEST_F (test_levin_protocol_handler__hanle_recv_with_invalid_data, does_not_handle_data_after_close)
 TEST_F (test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_network_error)
 TEST_F (test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_chunked_header)
 TEST_F (test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_chunked_body)
 TEST_F (test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_two_requests_at_once)
 TEST_F (test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_unexpected_response)

Function Documentation

◆ TEST_F() [1/17]

TEST_F ( positive_test_connection_to_levin_protocol_handler_calls ,
concurent_handler_initialization_and_destruction_is_correct  )

Definition at line 295 of file epee_levin_protocol_handler_async.cpp.

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}
#define ASSERT_EQ(val1, val2)
Definition gtest.h:1956

◆ TEST_F() [2/17]

TEST_F ( positive_test_connection_to_levin_protocol_handler_calls ,
handler_initialization_and_destruction_is_correct  )

Definition at line 284 of file epee_levin_protocol_handler_async.cpp.

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}
#define ASSERT_TRUE(condition)
Definition gtest.h:1865

◆ TEST_F() [3/17]

TEST_F ( positive_test_connection_to_levin_protocol_handler_calls ,
handler_processes_handle_read_as_invoke  )

Definition at line 329 of file epee_levin_protocol_handler_async.cpp.

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}
#define ASSERT_LE(val1, val2)
Definition gtest.h:1964
#define ASSERT_FALSE(condition)
Definition gtest.h:1868
#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_SIGNATURE
Definition levin_base.h:34
const char * buf

◆ TEST_F() [4/17]

TEST_F ( positive_test_connection_to_levin_protocol_handler_calls ,
handler_processes_handle_read_as_notify  )

Definition at line 386 of file epee_levin_protocol_handler_async.cpp.

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}

◆ TEST_F() [5/17]

TEST_F ( positive_test_connection_to_levin_protocol_handler_calls ,
handler_processes_qued_callback  )

Definition at line 418 of file epee_levin_protocol_handler_async.cpp.

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}

◆ TEST_F() [6/17]

TEST_F ( positive_test_connection_to_levin_protocol_handler_calls ,
new_handler_is_not_initialized  )

Definition at line 273 of file epee_levin_protocol_handler_async.cpp.

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}

◆ TEST_F() [7/17]

TEST_F ( test_levin_protocol_handler__hanle_recv_with_invalid_data ,
does_not_handle_data_after_close  )

Definition at line 471 of file epee_levin_protocol_handler_async.cpp.

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}

◆ TEST_F() [8/17]

TEST_F ( test_levin_protocol_handler__hanle_recv_with_invalid_data ,
handles_big_cb  )

Definition at line 463 of file epee_levin_protocol_handler_async.cpp.

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}

◆ TEST_F() [9/17]

TEST_F ( test_levin_protocol_handler__hanle_recv_with_invalid_data ,
handles_big_packet_1  )

Definition at line 429 of file epee_levin_protocol_handler_async.cpp.

430{
431 std::string buf("yyyyyy");
432 ASSERT_FALSE(m_conn->m_protocol_handler.handle_recv(buf.data(), max_packet_size + 1));
433}

◆ TEST_F() [10/17]

TEST_F ( test_levin_protocol_handler__hanle_recv_with_invalid_data ,
handles_big_packet_2  )

Definition at line 435 of file epee_levin_protocol_handler_async.cpp.

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}

◆ TEST_F() [11/17]

TEST_F ( test_levin_protocol_handler__hanle_recv_with_invalid_data ,
handles_chunked_body  )

Definition at line 505 of file epee_levin_protocol_handler_async.cpp.

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}

◆ TEST_F() [12/17]

TEST_F ( test_levin_protocol_handler__hanle_recv_with_invalid_data ,
handles_chunked_header  )

Definition at line 487 of file epee_levin_protocol_handler_async.cpp.

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}

◆ TEST_F() [13/17]

TEST_F ( test_levin_protocol_handler__hanle_recv_with_invalid_data ,
handles_invalid_signature_for_full_header  )

Definition at line 446 of file epee_levin_protocol_handler_async.cpp.

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}

◆ TEST_F() [14/17]

TEST_F ( test_levin_protocol_handler__hanle_recv_with_invalid_data ,
handles_invalid_signature_for_partial_header  )

Definition at line 454 of file epee_levin_protocol_handler_async.cpp.

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}

◆ TEST_F() [15/17]

TEST_F ( test_levin_protocol_handler__hanle_recv_with_invalid_data ,
handles_network_error  )

Definition at line 479 of file epee_levin_protocol_handler_async.cpp.

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}

◆ TEST_F() [16/17]

TEST_F ( test_levin_protocol_handler__hanle_recv_with_invalid_data ,
handles_two_requests_at_once  )

Definition at line 522 of file epee_levin_protocol_handler_async.cpp.

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}

◆ TEST_F() [17/17]

TEST_F ( test_levin_protocol_handler__hanle_recv_with_invalid_data ,
handles_unexpected_response  )

Definition at line 531 of file epee_levin_protocol_handler_async.cpp.

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}