diff options
author | Mykola Korniichuk (GitHub) <42380041+mkorniichuk@users.noreply.github.com> | 2020-11-12 18:41:49 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-12 11:41:49 -0500 |
commit | 43e539e57f7459ce7ab5c683af5580728f7eff49 (patch) | |
tree | 419ac6d65857ebce2cddaaf9b8df3c27d5dcab1a | |
parent | 7af66962b29992166de8b2162a82db6aa3ebce80 (diff) | |
download | sdl_core-43e539e57f7459ce7ab5c683af5580728f7eff49.tar.gz |
Cover connection handler and policy handler with unit tests (#2682)
* Cover connection handler and policy handler with unit tests
Few tests added to connection_handler_impl_test.cc and to
connection_test.cc
There were 2 mocks for telemetry observer. So, one is removed
(src/components/include/test/protocol_handler/mock_telemetry_observer.h)
Fixed mock_telemetry_observer.h (src/components/protocol_handler/
test/include/protocol_handler/mock_telemetry_observer.h)
Added tests to protocol_handler_tm_test.cc
set_hash_id and get_hash_id are covered with unit tests.
set_hash_id, get_hash_id, ConvertPacketDataToString,
get_protocol_observers_count declarations are added to
protocol_handler_impl.h under BUILD_TEST flag.
Resolves: #2449
* fix according to comments
* Unit test for get_hash_id and set_hash_id
Removed declarations for internal functions get_hash_id and set_hash_id
ConvertPacketDataToString moved to convert_utils
as ConvertBinaryDataToString
Fixed unit tests for get_hash_id and set_hash_id
* replace dcheck or return to if in ConvertBinaryDataToString
due to issue in smoke tests
* minor UT fixes
* Update doxygen for convert_utils
* Address comments and fix UT
Co-authored-by: Andrii Kalinich <AKalinich@luxoft.com>
-rw-r--r-- | src/components/connection_handler/test/connection_handler_impl_test.cc | 171 | ||||
-rw-r--r-- | src/components/connection_handler/test/connection_test.cc | 97 | ||||
-rw-r--r-- | src/components/protocol_handler/include/protocol_handler/protocol_handler_impl.h | 3 | ||||
-rw-r--r-- | src/components/protocol_handler/src/protocol_handler_impl.cc | 33 | ||||
-rw-r--r-- | src/components/protocol_handler/test/include/protocol_handler/mock_telemetry_observer.h | 8 | ||||
-rw-r--r-- | src/components/protocol_handler/test/protocol_handler_tm_test.cc | 1279 | ||||
-rw-r--r-- | src/components/utils/include/utils/convert_utils.h | 31 | ||||
-rw-r--r-- | src/components/utils/src/convert_utils.cc | 20 | ||||
-rw-r--r-- | src/components/utils/test/convert_utils_test.cc (renamed from src/components/include/test/protocol_handler/mock_telemetry_observer.h) | 64 |
9 files changed, 1553 insertions, 153 deletions
diff --git a/src/components/connection_handler/test/connection_handler_impl_test.cc b/src/components/connection_handler/test/connection_handler_impl_test.cc index 9305eee2ee..87dac9182e 100644 --- a/src/components/connection_handler/test/connection_handler_impl_test.cc +++ b/src/components/connection_handler/test/connection_handler_impl_test.cc @@ -399,6 +399,10 @@ TEST_F(ConnectionHandlerTest, AddConnection_StopConnection) { } TEST_F(ConnectionHandlerTest, GetConnectionSessionsCount) { + ASSERT_TRUE(connection_handler_->getConnectionList().empty()); + EXPECT_EQ(0u, + connection_handler_->GetConnectionSessionsCount(connection_key_)); + AddTestDeviceConnection(); EXPECT_EQ(0u, connection_handler_->GetConnectionSessionsCount(connection_key_)); @@ -415,13 +419,19 @@ TEST_F(ConnectionHandlerTest, GetAppIdOnSessionKey) { uint32_t app_id = 0; const uint32_t test_id = SessionHash(uid_, out_context_.new_session_id_); - connection_handler::DeviceHandle* device_id = NULL; EXPECT_EQ(0, connection_handler_->GetDataOnSessionKey( - connection_key_, &app_id, NULL, device_id)); + connection_key_, &app_id, nullptr, nullptr)); EXPECT_EQ(test_id, app_id); } +TEST_F(ConnectionHandlerTest, GetAppIdOnSessionKey_InvalidConnectionId) { + uint8_t invalid_id = 0; + EXPECT_EQ(-1, + connection_handler_->GetDataOnSessionKey( + invalid_id, nullptr, nullptr, nullptr)); +} + TEST_F(ConnectionHandlerTest, GetAppIdOnSessionKey_SessionNotStarted) { AddTestDeviceConnection(); @@ -443,10 +453,21 @@ TEST_F(ConnectionHandlerTest, GetDeviceID) { const Device& devres = pos->second; std::string test_mac_address = devres.mac_address(); + std::string invalid_mac_address; + EXPECT_FALSE( + connection_handler_->GetDeviceID(invalid_mac_address, &test_handle)); + EXPECT_TRUE(connection_handler_->GetDeviceID(test_mac_address, &test_handle)); EXPECT_EQ(device_handle_, test_handle); } +TEST_F(ConnectionHandlerTest, GetDataOnDeviceID_InvalidDeviceHandle) { + const DeviceHandle handle = 0; + EXPECT_EQ(-1, + connection_handler_->GetDataOnDeviceID( + handle, nullptr, nullptr, nullptr, nullptr)); +} + TEST_F(ConnectionHandlerTest, GetDeviceName) { AddTestDeviceConnection(); AddTestSession(); @@ -466,7 +487,7 @@ TEST_F(ConnectionHandlerTest, GetConnectionType) { std::string test_connection_type; EXPECT_EQ(0, connection_handler_->GetDataOnDeviceID( - handle, NULL, NULL, NULL, &test_connection_type)); + handle, nullptr, nullptr, nullptr, &test_connection_type)); EXPECT_EQ(connection_type_, test_connection_type); } @@ -487,7 +508,7 @@ TEST_F(ConnectionHandlerTest, GetApplicationsOnDevice) { EXPECT_EQ(test_id, applications_list.front()); } -TEST_F(ConnectionHandlerTest, GetDefaultProtocolVersion) { +TEST_F(ConnectionHandlerTest, ProtocolVersionUsed_Default_Success) { AddTestDeviceConnection(); AddTestSession(); @@ -498,7 +519,7 @@ TEST_F(ConnectionHandlerTest, GetDefaultProtocolVersion) { EXPECT_EQ(PROTOCOL_VERSION_2, protocol_version); } -TEST_F(ConnectionHandlerTest, GetProtocolVersion) { +TEST_F(ConnectionHandlerTest, ProtocolVersionUsed_Success) { AddTestDeviceConnection(); AddTestSession(); ChangeProtocol(uid_, out_context_.new_session_id_, PROTOCOL_VERSION_3); @@ -510,7 +531,7 @@ TEST_F(ConnectionHandlerTest, GetProtocolVersion) { EXPECT_EQ(PROTOCOL_VERSION_3, protocol_version); } -TEST_F(ConnectionHandlerTest, GetProtocolVersionAfterBinding) { +TEST_F(ConnectionHandlerTest, ProtocolVersionUsed_AfterBinding_Success) { AddTestDeviceConnection(); AddTestSession(); uint8_t protocol_version = 0; @@ -526,6 +547,15 @@ TEST_F(ConnectionHandlerTest, GetProtocolVersionAfterBinding) { EXPECT_EQ(PROTOCOL_VERSION_3, protocol_version); } +TEST_F(ConnectionHandlerTest, ProtocolVersionUsed_WrongConnection_Fail) { + AddTestDeviceConnection(); + AddTestSession(); + uint8_t connection_id = 0; + uint8_t protocol_version = 0; + EXPECT_FALSE(connection_handler_->ProtocolVersionUsed( + connection_id, out_context_.new_session_id_, protocol_version)); +} + TEST_F(ConnectionHandlerTest, GetPairFromKey) { AddTestDeviceConnection(); AddTestSession(); @@ -544,14 +574,25 @@ MATCHER_P(SameDevice, device, "") { arg.connection_type() == device.connection_type(); } -TEST_F(ConnectionHandlerTest, IsHeartBeatSupported) { +TEST_F(ConnectionHandlerTest, IsHeartBeatSupported_Success) { AddTestDeviceConnection(); AddTestSession(); ChangeProtocol(uid_, out_context_.new_session_id_, PROTOCOL_VERSION_3); + EXPECT_TRUE(connection_handler_->IsHeartBeatSupported( uid_, out_context_.new_session_id_)); } +TEST_F(ConnectionHandlerTest, IsHeartBeatSupported_Fail) { + AddTestDeviceConnection(); + AddTestSession(); + ChangeProtocol(uid_, out_context_.new_session_id_, PROTOCOL_VERSION_3); + + uint8_t invalid_id = 0u; + EXPECT_FALSE(connection_handler_->IsHeartBeatSupported( + invalid_id, out_context_.new_session_id_)); +} + TEST_F(ConnectionHandlerTest, SendEndServiceWithoutSetProtocolHandler) { AddTestDeviceConnection(); AddTestSession(); @@ -559,6 +600,12 @@ TEST_F(ConnectionHandlerTest, SendEndServiceWithoutSetProtocolHandler) { connection_handler_->SendEndService(connection_key_, kRpc); } +TEST_F(ConnectionHandlerTest, SendEndService_NoSession_Fail) { + AddTestDeviceConnection(); + EXPECT_CALL(mock_protocol_handler_, SendEndService(_, _, _, kRpc)).Times(0); + connection_handler_->SendEndService(connection_key_, kRpc); +} + TEST_F(ConnectionHandlerTest, SendEndService) { AddTestDeviceConnection(); AddTestSession(); @@ -838,7 +885,27 @@ TEST_F(ConnectionHandlerTest, OnDeviceRemoved_ServiceStarted) { connection_handler_->OnDeviceRemoved(device1); } -TEST_F(ConnectionHandlerTest, OnConnectionClosed) { +TEST_F(ConnectionHandlerTest, OnConnectionClosed_InvalidConnectionId_Fail) { + AddTestDeviceConnection(); + AddTestSession(); + + connection_handler_test::MockConnectionHandlerObserver + mock_connection_handler_observer; + connection_handler_->set_connection_handler_observer( + &mock_connection_handler_observer); + + EXPECT_CALL(mock_connection_handler_observer, + OnServiceEndedCallback(connection_key_, kBulk, kCommon)) + .Times(0); + EXPECT_CALL(mock_connection_handler_observer, + OnServiceEndedCallback(connection_key_, kRpc, kCommon)) + .Times(0); + + uint8_t invalid_id = 0; + connection_handler_->OnConnectionClosed(invalid_id); +} + +TEST_F(ConnectionHandlerTest, OnConnectionClosed_Success) { AddTestDeviceConnection(); AddTestSession(); @@ -1132,6 +1199,24 @@ TEST_F(ConnectionHandlerTest, CloseConnectionSessionsWithMalformedMessage) { EXPECT_TRUE(waiter.WaitFor(times, kAsyncExpectationsTimeout)); } +TEST_F(ConnectionHandlerTest, CloseConnectionSessionsInvalidConnectionId) { + connection_handler_test::MockConnectionHandlerObserver + mock_connection_handler_observer; + connection_handler_->set_connection_handler_observer( + &mock_connection_handler_observer); + + connection_handler_->set_protocol_handler(&mock_protocol_handler_); + + EXPECT_CALL(mock_protocol_handler_, SendEndSession(_, _)).Times(0); + EXPECT_CALL(mock_connection_handler_observer, OnServiceEndedCallback(_, _, _)) + .Times(0); + EXPECT_CALL(mock_connection_handler_observer, OnServiceEndedCallback(_, _, _)) + .Times(0); + + uint8_t invalid_id = 0u; + connection_handler_->CloseConnectionSessions(invalid_id, kCommon); +} + TEST_F(ConnectionHandlerTest, CloseConnectionSessionsWithCommonReason) { AddTestDeviceConnection(); AddTestSession(); @@ -1747,7 +1832,7 @@ TEST_F(ConnectionHandlerTest, #endif // ENABLE_SECURITY } -TEST_F(ConnectionHandlerTest, SessionStarted_DealyProtect) { +TEST_F(ConnectionHandlerTest, SessionStarted_DelayProtect) { AddTestDeviceConnection(); AddTestSession(); ChangeProtocol( @@ -1804,7 +1889,7 @@ TEST_F(ConnectionHandlerTest, SessionStarted_DealyProtect) { #endif // ENABLE_SECURITY } -TEST_F(ConnectionHandlerTest, SessionStarted_DealyProtectBulk) { +TEST_F(ConnectionHandlerTest, SessionStarted_DelayProtectBulk) { AddTestDeviceConnection(); AddTestSession(); @@ -2095,6 +2180,25 @@ TEST_F(ConnectionHandlerTest, OnDeviceConnectionSwitching) { OnDeviceSwitchingStart(SameDevice(d1), SameDevice(d2))); connection_handler_->OnDeviceSwitchingStart(mac_address_, second_mac_address); + + EXPECT_CALL(mock_connection_handler_observer, + OnDeviceSwitchingFinish(encryption::MakeHash(mac_address_))); + connection_handler_->OnDeviceSwitchingFinish(mac_address_); + + EXPECT_CALL( + mock_connection_handler_observer, + OnDeviceSwitchingFinish(encryption::MakeHash(second_mac_address))); + connection_handler_->OnDeviceSwitchingFinish(second_mac_address); +} + +TEST_F(ConnectionHandlerTest, OnConnectionEstablishedForUnknownDevice) { + const transport_manager::DeviceInfo device_info( + device_handle_, mac_address_, device_name_, std::string("WIFI")); + const transport_manager::ConnectionUID uid = 2u; + + connection_handler_->OnConnectionEstablished(device_info, uid); + ASSERT_TRUE(connection_handler_->getDeviceList().empty()); + ASSERT_TRUE(connection_handler_->getConnectionList().empty()); } TEST_F(ConnectionHandlerTest, StartStopSecondarySession) { @@ -2200,6 +2304,13 @@ TEST_F(ConnectionHandlerTest, StopSecondarySession_NoService) { EXPECT_EQ(st.secondary_transport, 0u); } +TEST_F(ConnectionHandlerTest, GetSessionTransports_InvalidSessionId) { + uint8_t invalid_id = 0; + SessionTransports st = connection_handler_->GetSessionTransports(invalid_id); + EXPECT_EQ(st.primary_transport, 0u); + EXPECT_EQ(st.secondary_transport, 0u); +} + TEST_F(ConnectionHandlerTest, ConnectionType_valid) { AddTestDeviceConnection(); AddTestSession(); @@ -2289,6 +2400,46 @@ TEST_F(ConnectionHandlerTest, SetSecondaryTransportID_Failure) { EXPECT_EQ(0u, st.secondary_transport); } +TEST_F(ConnectionHandlerTest, CloseSession_TwoSessions_ConnectionOpened) { + // Add virtual device and connection + AddTestDeviceConnection(); + AddTestSession(); + + connection_handler::ConnectionList& connection_list = + connection_handler_->getConnectionList(); + ASSERT_EQ(1u, connection_list.size()); + + connection_handler::Connection* connection = connection_list.begin()->second; + EXPECT_EQ(1u, connection->session_map().size()); + + AddTestSession(); + EXPECT_EQ(2u, connection->session_map().size()); + + connection_handler_->CloseSession( + uid_, out_context_.new_session_id_, kCommon); + + EXPECT_EQ(1u, connection->session_map().size()); + EXPECT_EQ(1u, connection_list.size()); +} + +TEST_F(ConnectionHandlerTest, CloseSession_LastSession_ConnectionOpened) { + // Add virtual device and connection + AddTestDeviceConnection(); + AddTestSession(); + + connection_handler::ConnectionList& connection_list = + connection_handler_->getConnectionList(); + ASSERT_EQ(1u, connection_list.size()); + + connection_handler::Connection* connection = connection_list.begin()->second; + EXPECT_EQ(1u, connection->session_map().size()); + + connection_handler_->CloseSession( + uid_, out_context_.new_session_id_, kCommon); + + EXPECT_EQ(0u, connection->session_map().size()); + EXPECT_EQ(1u, connection_list.size()); +} } // namespace connection_handler_test } // namespace components } // namespace test diff --git a/src/components/connection_handler/test/connection_test.cc b/src/components/connection_handler/test/connection_test.cc index dd86831225..9896142cef 100644 --- a/src/components/connection_handler/test/connection_test.cc +++ b/src/components/connection_handler/test/connection_test.cc @@ -62,24 +62,27 @@ using ::testing::Return; class ConnectionTest : public ::testing::Test { protected: void SetUp() OVERRIDE { - connection_handler_ = new ConnectionHandlerImpl( - mock_connection_handler_settings, transport_manager_mock); + connection_handler_.reset(new ConnectionHandlerImpl( + mock_connection_handler_settings, transport_manager_mock)); const ConnectionHandle connectionHandle = 0; const DeviceHandle device_handle = 0u; const uint32_t heart_beat = 10000u; - connection_ = new Connection( - connectionHandle, device_handle, connection_handler_, heart_beat); + connection_.reset(new Connection(connectionHandle, + device_handle, + connection_handler_.get(), + heart_beat)); } void TearDown() OVERRIDE { - delete connection_; - delete connection_handler_; + connection_.reset(); + connection_handler_.reset(); } void StartSession() { StartDefaultSession(); connection_->UpdateProtocolVersionSession( session_id, protocol_handler::PROTOCOL_VERSION_3); } + void StartDefaultSession() { session_id = connection_->AddNewSession(kDefaultConnectionHandle); EXPECT_NE(session_id, 0u); @@ -93,6 +96,7 @@ class ConnectionTest : public ::testing::Test { EXPECT_TRUE(found_result); EXPECT_EQ(connection_->primary_connection_handle(), 0); } + void AddNewService(const ServiceType service_type, const bool protection, const bool expect_add_new_service_call_result, @@ -124,6 +128,7 @@ class ConnectionTest : public ::testing::Test { #endif // ENABLE_SECURITY } } + void AddNewSecondaryService(const ServiceType service_type) { const bool result = connection_->AddNewService( session_id, service_type, false, kSecondaryConnectionHandle); @@ -162,11 +167,11 @@ class ConnectionTest : public ::testing::Test { EXPECT_EQ(expect_exist_service, found_result); } - Connection* connection_; + std::shared_ptr<Connection> connection_; MockConnectionHandlerSettings mock_connection_handler_settings; testing::StrictMock<transport_manager_test::MockTransportManager> transport_manager_mock; - ConnectionHandlerImpl* connection_handler_; + std::shared_ptr<ConnectionHandlerImpl> connection_handler_; uint32_t session_id; static const transport_manager::ConnectionUID kDefaultConnectionHandle = 1; static const transport_manager::ConnectionUID kSecondaryConnectionHandle = 2; @@ -191,19 +196,24 @@ TEST_F(ConnectionTest, Session_UpdateProtocolVersion) { EXPECT_EQ(static_cast<uint8_t>(PROTOCOL_VERSION_3), protocol_version); } -TEST_F(ConnectionTest, HeartBeat_NotSupported) { - // Arrange +TEST_F(ConnectionTest, SupportHeartBeat_WrongSessionId_Fail) { + uint8_t fake_id = 0u; + const SessionMap session_map = connection_->session_map(); + EXPECT_TRUE(session_map.empty()); + + EXPECT_FALSE(connection_->SupportHeartBeat(fake_id)); +} + +TEST_F(ConnectionTest, SupportHeartBeat_NotSupported_Fail) { StartDefaultSession(); uint8_t protocol_version; EXPECT_TRUE(connection_->ProtocolVersion(session_id, protocol_version)); EXPECT_EQ(static_cast<uint8_t>(PROTOCOL_VERSION_2), protocol_version); - // Assert EXPECT_FALSE(connection_->SupportHeartBeat(session_id)); } -TEST_F(ConnectionTest, HeartBeat_Protocol3_Supported) { - // Arrange +TEST_F(ConnectionTest, SupportHeartBeat_Protocol3_Success) { StartSession(); // Check execution if protocol version is 3 const uint8_t protocol_version = static_cast<uint8_t>(PROTOCOL_VERSION_3); @@ -211,8 +221,7 @@ TEST_F(ConnectionTest, HeartBeat_Protocol3_Supported) { EXPECT_TRUE(connection_->SupportHeartBeat(session_id)); } -TEST_F(ConnectionTest, HeartBeat_Protocol4_PositiveHeartBeat_Supported) { - // Arrange +TEST_F(ConnectionTest, SupportHeartBeat_Protocol4_PositiveHeartBeat_Success) { StartSession(); // Check execution if protocol version is 4 const uint8_t protocol_version = static_cast<uint8_t>(PROTOCOL_VERSION_4); @@ -220,16 +229,12 @@ TEST_F(ConnectionTest, HeartBeat_Protocol4_PositiveHeartBeat_Supported) { EXPECT_TRUE(connection_->SupportHeartBeat(session_id)); } -TEST_F(ConnectionTest, HeartBeat_Protocol4_ZeroHeartBeat_NotSupported) { - // Correctc of connection (need connection with heartbeat=0) - delete connection_; - connection_ = 0; - +TEST_F(ConnectionTest, SupportHeartBeat_Protocol4_ZeroHeartBeat_Fail) { const ConnectionHandle connectionHandle = 0; const DeviceHandle device_handle = 0u; const uint32_t heart_beat = 0u; - connection_ = new Connection( - connectionHandle, device_handle, connection_handler_, heart_beat); + connection_.reset(new Connection( + connectionHandle, device_handle, connection_handler_.get(), heart_beat)); StartSession(); // Check execution if protocol version is 4 const uint8_t protocol_version = static_cast<uint8_t>(PROTOCOL_VERSION_4); @@ -237,8 +242,7 @@ TEST_F(ConnectionTest, HeartBeat_Protocol4_ZeroHeartBeat_NotSupported) { EXPECT_FALSE(connection_->SupportHeartBeat(session_id)); } -TEST_F(ConnectionTest, HeartBeat_Protocol5_PositiveHeartBeat_Supported) { - // Arrange +TEST_F(ConnectionTest, SupportHeartBeat_Protocol5_PositiveHeartBeat_Success) { StartSession(); // Check execution if protocol version is 5 const uint8_t protocol_version = static_cast<uint8_t>(PROTOCOL_VERSION_5); @@ -246,16 +250,12 @@ TEST_F(ConnectionTest, HeartBeat_Protocol5_PositiveHeartBeat_Supported) { EXPECT_TRUE(connection_->SupportHeartBeat(session_id)); } -TEST_F(ConnectionTest, HeartBeat_Protocol5_ZeroHeartBeat_NotSupported) { - // Correctc of connection (need connection with heartbeat=0) - delete connection_; - connection_ = 0; - +TEST_F(ConnectionTest, SupportHeartBeat_Protocol5_ZeroHeartBeat_Fail) { const ConnectionHandle connectionHandle = 0; const DeviceHandle device_handle = 0u; const uint32_t heart_beat = 0u; - connection_ = new Connection( - connectionHandle, device_handle, connection_handler_, heart_beat); + connection_.reset(new Connection( + connectionHandle, device_handle, connection_handler_.get(), heart_beat)); StartSession(); // Check execution if protocol version is 5 const uint8_t protocol_version = static_cast<uint8_t>(PROTOCOL_VERSION_5); @@ -304,6 +304,16 @@ TEST_F(ConnectionTest, Session_AddControlService) { kControl, PROTECTION_ON, EXPECT_RETURN_FALSE, EXPECT_SERVICE_NOT_EXISTS); } +TEST_F(ConnectionTest, AddNewService_NotSupported) { + StartDefaultSession(); + uint8_t protocol_version; + EXPECT_TRUE(connection_->ProtocolVersion(session_id, protocol_version)); + EXPECT_EQ(static_cast<uint8_t>(PROTOCOL_VERSION_2), protocol_version); + + EXPECT_FALSE(connection_->AddNewService( + session_id, kAudio, false, kDefaultConnectionHandle)); +} + // Invalid Services couldnot be started anyway TEST_F(ConnectionTest, Session_AddInvalidService) { StartSession(); @@ -358,13 +368,11 @@ TEST_F(ConnectionTest, Session_AddAllOtherService_Protected) { TEST_F(ConnectionTest, FindAddedService) { StartSession(); - // Arrange SessionMap currentSessionMap = connection_->session_map(); Service* sessionWithService = currentSessionMap.find(session_id)->second.FindService(kAudio); EXPECT_EQ(NULL, sessionWithService); - // Act AddNewService( kAudio, PROTECTION_OFF, EXPECT_RETURN_TRUE, EXPECT_SERVICE_EXISTS); @@ -448,8 +456,8 @@ TEST_F(ConnectionTest, AddNewSession_VerifyAddSessionCalled) { ConnectionHandle connection_handle = 123; DeviceHandle device_handle = 0u; uint32_t heart_beat = 10000u; - Connection* connection = new Connection( - connection_handle, device_handle, &mock_connection_handler, heart_beat); + std::unique_ptr<Connection> connection(new Connection( + connection_handle, device_handle, &mock_connection_handler, heart_beat)); transport_manager::ConnectionUID connection_handle_uid = 1; uint32_t mock_session_id = 2; @@ -461,7 +469,6 @@ TEST_F(ConnectionTest, AddNewSession_VerifyAddSessionCalled) { EXPECT_CALL(mock_connection_handler, RemoveSession(mock_session_id)) .WillOnce(Return(true)); // invoked by destructor of connection - delete connection; } TEST_F(ConnectionTest, RemoveSession_VerifyRemoveSessionCalled) { @@ -470,8 +477,8 @@ TEST_F(ConnectionTest, RemoveSession_VerifyRemoveSessionCalled) { ConnectionHandle connection_handle = 123; DeviceHandle device_handle = 0u; uint32_t heart_beat = 10000u; - Connection* connection = new Connection( - connection_handle, device_handle, &mock_connection_handler, heart_beat); + std::unique_ptr<Connection> connection(new Connection( + connection_handle, device_handle, &mock_connection_handler, heart_beat)); transport_manager::ConnectionUID connection_handle_uid = 1; uint32_t mock_session_id = 10; @@ -486,7 +493,9 @@ TEST_F(ConnectionTest, RemoveSession_VerifyRemoveSessionCalled) { uint32_t ret = connection->RemoveSession(sid); EXPECT_EQ(sid, ret); - delete connection; + EXPECT_CALL(mock_connection_handler, RemoveSession(mock_session_id)) + .WillOnce(Return(true)); + EXPECT_EQ(0u, connection->RemoveSession(mock_session_id)); } TEST_F(ConnectionTest, SecondarySessionTest) { @@ -497,8 +506,8 @@ TEST_F(ConnectionTest, SecondarySessionTest) { const ConnectionHandle connectionHandle = 0; const DeviceHandle device_handle = 0u; const uint32_t heart_beat = 0u; - Connection* secondary_connection = new Connection( - connectionHandle, device_handle, connection_handler_, heart_beat); + std::unique_ptr<Connection> secondary_connection(new Connection( + connectionHandle, device_handle, connection_handler_.get(), heart_beat)); secondary_connection->SetPrimaryConnectionHandle(kDefaultConnectionHandle); connection_handler::ConnectionHandle expected_primary_connection_handle = @@ -508,8 +517,6 @@ TEST_F(ConnectionTest, SecondarySessionTest) { AddNewSecondaryService(kAudio); AddNewSecondaryService(kMobileNav); - - delete secondary_connection; } TEST_F(ConnectionTest, RemoveSecondaryServices_SUCCESS) { @@ -607,7 +614,7 @@ TEST_F(ConnectionTest, SetGetSSLContext) { TEST_F(ConnectionTest, SetProtectionFlagForRPC) { StartSession(); - // Arrange + SessionMap currentSessionMap = connection_->session_map(); Service* service_rpc = currentSessionMap.find(session_id)->second.FindService(kRpc); @@ -631,7 +638,7 @@ TEST_F(ConnectionTest, SetProtectionFlagForRPC) { TEST_F(ConnectionTest, SetProtectionFlagForBulk) { StartSession(); - // Arrange + SessionMap currentSessionMap = connection_->session_map(); Service* service_rpc = currentSessionMap.find(session_id)->second.FindService(kRpc); diff --git a/src/components/protocol_handler/include/protocol_handler/protocol_handler_impl.h b/src/components/protocol_handler/include/protocol_handler/protocol_handler_impl.h index 33abd4a96b..c79dbfcac3 100644 --- a/src/components/protocol_handler/include/protocol_handler/protocol_handler_impl.h +++ b/src/components/protocol_handler/include/protocol_handler/protocol_handler_impl.h @@ -43,6 +43,7 @@ #include "utils/prioritized_queue.h" #include "utils/threads/message_loop_thread.h" +#include "utils/convert_utils.h" #include "utils/custom_string.h" #include "utils/messagemeter.h" #include "utils/semantic_version.h" @@ -674,7 +675,7 @@ class ProtocolHandlerImpl RESULT_CODE HandleControlMessageHeartBeat(const ProtocolPacket& packet); - void PopValideAndExpirateMultiframes(); + void PopValidAndExpiredMultiframes(); // threads::MessageLoopThread<*>::Handler implementations // CALLED ON raw_ford_messages_from_mobile_ thread! diff --git a/src/components/protocol_handler/src/protocol_handler_impl.cc b/src/components/protocol_handler/src/protocol_handler_impl.cc index 1db599e656..c2fd2db4f3 100644 --- a/src/components/protocol_handler/src/protocol_handler_impl.cc +++ b/src/components/protocol_handler/src/protocol_handler_impl.cc @@ -52,13 +52,6 @@ namespace protocol_handler { SDL_CREATE_LOG_VARIABLE("ProtocolHandler") -/** - * Function return packet data as std::string. - * If packet data is not printable return error message - */ -std::string ConvertPacketDataToString(const uint8_t* data, - const size_t data_size); - const size_t kStackSize = 131072; const utils::SemanticVersion default_protocol_version(5, 3, 0); @@ -1283,7 +1276,7 @@ RESULT_CODE ProtocolHandlerImpl::SendFrame(const ProtocolFramePtr packet) { SDL_LOG_DEBUG( "Packet to be sent: " - << ConvertPacketDataToString(packet->data(), packet->data_size()) + << utils::ConvertBinaryDataToString(packet->data(), packet->data_size()) << " of size: " << packet->data_size()); const RawMessagePtr message_to_send = packet->serializePacket(); if (!message_to_send) { @@ -1448,7 +1441,7 @@ RESULT_CODE ProtocolHandlerImpl::HandleSingleFrameMessage( SDL_LOG_DEBUG( "FRAME_TYPE_SINGLE message of size " << packet->data_size() << "; message " - << ConvertPacketDataToString(packet->data(), packet->data_size())); + << utils::ConvertBinaryDataToString(packet->data(), packet->data_size())); // Replace a potential secondary transport ID in the packet with the primary // transport ID @@ -2073,7 +2066,7 @@ RESULT_CODE ProtocolHandlerImpl::HandleControlMessageHeartBeat( } } -void ProtocolHandlerImpl::PopValideAndExpirateMultiframes() { +void ProtocolHandlerImpl::PopValidAndExpiredMultiframes() { SDL_LOG_AUTO_TRACE(); const ProtocolFramePtrList& frame_list = multiframe_builder_.PopMultiframes(); for (ProtocolFramePtrList::const_iterator it = frame_list.begin(); @@ -2180,7 +2173,7 @@ void ProtocolHandlerImpl::Handle(const impl::RawFordMessageFromMobile message) { FRAME_TYPE_FIRST == message->frame_type()) { SDL_LOG_DEBUG("Packet: dataSize " << message->data_size()); HandleMessage(message); - PopValideAndExpirateMultiframes(); + PopValidAndExpiredMultiframes(); } else { SDL_LOG_WARN("handleMessagesFromMobileApp() - incorrect or NULL data"); } @@ -2396,24 +2389,6 @@ void ProtocolHandlerImpl::SetTelemetryObserver(PHTelemetryObserver* observer) { } #endif // TELEMETRY_MONITOR -std::string ConvertPacketDataToString(const uint8_t* data, - const size_t data_size) { - if (0 == data_size) - return std::string(); - bool is_printable_array = true; - std::locale loc; - const char* text = reinterpret_cast<const char*>(data); - // Check data for printability - for (size_t i = 0; i < data_size; ++i) { - if (!std::isprint(text[i], loc)) { - is_printable_array = false; - break; - } - } - return is_printable_array ? std::string(text, data_size) - : std::string("is raw data"); -} - uint8_t ProtocolHandlerImpl::SupportedSDLProtocolVersion() const { SDL_LOG_AUTO_TRACE(); return get_settings().max_supported_protocol_version(); diff --git a/src/components/protocol_handler/test/include/protocol_handler/mock_telemetry_observer.h b/src/components/protocol_handler/test/include/protocol_handler/mock_telemetry_observer.h index 9a91a5d1f7..f303e8ed57 100644 --- a/src/components/protocol_handler/test/include/protocol_handler/mock_telemetry_observer.h +++ b/src/components/protocol_handler/test/include/protocol_handler/mock_telemetry_observer.h @@ -34,8 +34,7 @@ #define SRC_COMPONENTS_PROTOCOL_HANDLER_TEST_INCLUDE_PROTOCOL_HANDLER_MOCK_TELEMETRY_OBSERVER_H_ #include "gmock/gmock.h" -#include "protocol_handler/time_metric_observer.h" -#include "utils/shared_ptr.h" +#include "protocol_handler/telemetry_observer.h" namespace test { namespace components { @@ -44,8 +43,9 @@ namespace protocol_handler_test { class MockPHTelemetryObserver : public ::protocol_handler::PHTelemetryObserver { public: MOCK_METHOD2(StartMessageProcess, - void(uint32_t, const date_time::TimeDuration&)); - MOCK_METHOD2(EndMessageProcess, void(std::shared_ptr<MessageMetric>)); + void(uint32_t message_id, + const date_time::TimeDuration& start_time)); + MOCK_METHOD1(EndMessageProcess, void(std::shared_ptr<MessageMetric> m)); }; } // namespace protocol_handler_test diff --git a/src/components/protocol_handler/test/protocol_handler_tm_test.cc b/src/components/protocol_handler/test/protocol_handler_tm_test.cc index 41c16817b5..fa2fc07824 100644 --- a/src/components/protocol_handler/test/protocol_handler_tm_test.cc +++ b/src/components/protocol_handler/test/protocol_handler_tm_test.cc @@ -41,6 +41,7 @@ #include "protocol_handler/mock_protocol_observer.h" #include "protocol_handler/mock_service_status_update_handler_listener.h" #include "protocol_handler/mock_session_observer.h" +#include "protocol_handler/mock_telemetry_observer.h" #include "protocol_handler/protocol_handler.h" #include "protocol_handler/protocol_handler_impl.h" #ifdef ENABLE_SECURITY @@ -130,6 +131,7 @@ using ::testing::AnyOf; using ::testing::AtLeast; using ::testing::ByRef; using ::testing::DoAll; +using ::testing::ElementsAreArray; using ::testing::Eq; using ::testing::Invoke; using ::testing::Return; @@ -137,6 +139,7 @@ using ::testing::ReturnNull; using ::testing::ReturnRef; using ::testing::ReturnRefOfCopy; using ::testing::SaveArg; +using ::testing::SaveArgPointee; using ::testing::SetArgPointee; using ::testing::SetArgReferee; @@ -151,6 +154,10 @@ ACTION_P5(InvokeMemberFuncWithArg3, ptr, memberFunc, a, b, c) { } namespace { +const bool kFinalMessage = true; +const uint8_t* const null_data_ = NULL; +const uint32_t kInvalidSessionId = 0u; +const uint32_t kValidSessionId = 1u; const uint32_t kAsyncExpectationsTimeout = 10000u; const uint32_t kMicrosecondsInMillisecond = 1000u; const uint32_t kAddSessionWaitTimeMs = 100u; @@ -388,6 +395,61 @@ class ProtocolHandlerImplTest : public ::testing::Test { data); } + void OnTMMessageSend() { + const uint8_t data_size = 0u; + ProtocolFramePtr protocol_packet = + std::make_shared<ProtocolPacket>(connection_id, + PROTOCOL_VERSION_3, + PROTECTION_OFF, + FRAME_TYPE_SINGLE, + kRpc, + FRAME_DATA_SINGLE, + session_id, + data_size, + message_id, + null_data_); + + RawMessagePtr message = protocol_packet->serializePacket(); + EXPECT_CALL(session_observer_mock, + PairFromKey(message->connection_key(), _, _)) + .WillOnce(DoAll(SetArgPointee<1>(connection_id), + SetArgPointee<2>(session_id))); + + protocol_handler::impl::RawFordMessageToMobile raw_message_to_mobile( + protocol_packet, kFinalMessage); + protocol_handler::impl::ToMobileQueue::Handler* + raw_message_to_mobile_handler = protocol_handler_impl.get(); + raw_message_to_mobile_handler->Handle(raw_message_to_mobile); + + MockProtocolObserver mock_protocol_observer; + protocol_handler_impl->AddProtocolObserver(&mock_protocol_observer); + EXPECT_CALL(mock_protocol_observer, OnMobileMessageSent(_)); + + EXPECT_CALL(session_observer_mock, + ProtocolVersionUsed(_, _, An<uint8_t&>())) + .WillOnce(Return(false)); + + tm_listener->OnTMMessageSend(message); + } + + ProtocolFramePtr CreateFramePtrWithData(const uint8_t data_value, + const uint8_t frame_type) { + const uint8_t data_size = 1u; + ProtocolFramePtr frame_ptr = + std::make_shared<ProtocolPacket>(connection_id, + PROTOCOL_VERSION_3, + PROTECTION_OFF, + frame_type, + kAudio, + FRAME_DATA_END_SERVICE_ACK, + session_id, + data_size, + message_id, + &data_value); + + return frame_ptr; + } + void VerifySecondaryTransportParamsInStartSessionAck( bool config_multiple_transports_enabled, const std::vector<std::string>& config_secondary_transports_for_usb, @@ -423,6 +485,10 @@ class ProtocolHandlerImplTest : public ::testing::Test { transport_manager_mock; testing::StrictMock<protocol_handler_test::MockSessionObserver> session_observer_mock; + testing::StrictMock<protocol_handler_test::MockProtocolObserver> + protocol_observer_mock; + testing::StrictMock<protocol_handler_test::MockPHTelemetryObserver> + telemetry_observer_mock; #ifdef ENABLE_SECURITY testing::NiceMock<security_manager_test::MockSecurityManager> security_manager_mock; @@ -454,12 +520,6 @@ class OnHandshakeDoneFunctor { #endif // ENABLE_SECURITY /* - * ProtocolHandler shall skip empty message - */ -TEST_F(ProtocolHandlerImplTest, RecieveEmptyRawMessage) { - tm_listener->OnTMMessageReceived(RawMessagePtr()); -} -/* * ProtocolHandler shall disconnect on no connection */ TEST_F(ProtocolHandlerImplTest, RecieveOnUnknownConnection) { @@ -2119,7 +2179,7 @@ void ProtocolHandlerImplTest::VerifyCloudAppParamsInStartSessionAck( session_id, input_protocol_version, hash_id, - protocol_handler::SERVICE_TYPE_RPC, + kRpc, false /* protection */, full_version); @@ -2587,7 +2647,7 @@ TEST_F(ProtocolHandlerImplTest, session_id, input_protocol_version, hash_id, - protocol_handler::SERVICE_TYPE_RPC, + kRpc, false /* protection */, full_version); @@ -2773,7 +2833,7 @@ TEST_F(ProtocolHandlerImplTest, session_id, input_protocol_version, hash_id, - protocol_handler::SERVICE_TYPE_RPC, + kRpc, false /* protection */, full_version); @@ -2894,7 +2954,7 @@ TEST_F(ProtocolHandlerImplTest, session_id, input_protocol_version, hash_id, - protocol_handler::SERVICE_TYPE_RPC, + kRpc, false /* protection */, full_version); @@ -3133,6 +3193,14 @@ TEST_F(ProtocolHandlerImplTest, DISABLED_FloodVerification) { .WillOnce(NotifyTestAsyncWaiter(waiter)); times++; + ON_CALL(session_observer_mock, KeyFromPair(connection_id, session_id)) + .WillByDefault(Return(connection_key)); + + connection_handler::SessionTransports st; + st.primary_transport = connection_id; + ON_CALL(connection_handler_mock, GetSessionTransports(session_id)) + .WillByDefault(Return(st)); + ON_CALL(protocol_handler_settings_mock, message_frequency_time()) .WillByDefault(Return(period_msec)); ON_CALL(protocol_handler_settings_mock, message_frequency_count()) @@ -3148,10 +3216,10 @@ TEST_F(ProtocolHandlerImplTest, DISABLED_FloodVerification) { session_id, some_data.size(), message_id, - &some_data[0]); + some_data.data()); } - EXPECT_TRUE(waiter->WaitFor(times, period_msec)); + EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); } TEST_F(ProtocolHandlerImplTest, DISABLED_FloodVerification_ThresholdValue) { @@ -3170,10 +3238,18 @@ TEST_F(ProtocolHandlerImplTest, DISABLED_FloodVerification_ThresholdValue) { ON_CALL(protocol_handler_settings_mock, message_frequency_count()) .WillByDefault(Return(max_messages)); + ON_CALL(session_observer_mock, KeyFromPair(connection_id, session_id)) + .WillByDefault(Return(connection_key)); + // Expect NO flood notification to CH EXPECT_CALL(session_observer_mock, OnApplicationFloodCallBack(connection_key)) .Times(0); + connection_handler::SessionTransports st; + st.primary_transport = connection_id; + ON_CALL(connection_handler_mock, GetSessionTransports(session_id)) + .WillByDefault(Return(st)); + for (size_t i = 0; i < max_messages - 1; ++i) { SendTMMessage(connection_id, PROTOCOL_VERSION_3, @@ -3184,10 +3260,10 @@ TEST_F(ProtocolHandlerImplTest, DISABLED_FloodVerification_ThresholdValue) { session_id, some_data.size(), message_id, - &some_data[0]); + some_data.data()); } - EXPECT_TRUE(waiter->WaitFor(times, period_msec)); + EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); } TEST_F(ProtocolHandlerImplTest, DISABLED_FloodVerification_VideoFrameSkip) { @@ -3201,6 +3277,14 @@ TEST_F(ProtocolHandlerImplTest, DISABLED_FloodVerification_VideoFrameSkip) { AddSession(waiter, times); + connection_handler::SessionTransports st; + st.primary_transport = connection_id; + ON_CALL(connection_handler_mock, GetSessionTransports(session_id)) + .WillByDefault(Return(st)); + + ON_CALL(session_observer_mock, KeyFromPair(connection_id, session_id)) + .WillByDefault(Return(connection_key)); + // Expect NO flood notification to CH on video data streaming for (size_t i = 0; i < max_messages + 1; ++i) { SendTMMessage(connection_id, @@ -3212,10 +3296,10 @@ TEST_F(ProtocolHandlerImplTest, DISABLED_FloodVerification_VideoFrameSkip) { session_id, some_data.size(), message_id, - &some_data[0]); + some_data.data()); } - EXPECT_TRUE(waiter->WaitFor(times, period_msec)); + EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); } TEST_F(ProtocolHandlerImplTest, DISABLED_FloodVerification_AudioFrameSkip) { @@ -3229,6 +3313,14 @@ TEST_F(ProtocolHandlerImplTest, DISABLED_FloodVerification_AudioFrameSkip) { AddSession(waiter, times); + connection_handler::SessionTransports st; + st.primary_transport = connection_id; + ON_CALL(connection_handler_mock, GetSessionTransports(session_id)) + .WillByDefault(Return(st)); + + ON_CALL(session_observer_mock, KeyFromPair(connection_id, session_id)) + .WillByDefault(Return(connection_key)); + // Expect NO flood notification to CH on video data streaming for (size_t i = 0; i < max_messages + 1; ++i) { SendTMMessage(connection_id, @@ -3240,10 +3332,10 @@ TEST_F(ProtocolHandlerImplTest, DISABLED_FloodVerification_AudioFrameSkip) { session_id, some_data.size(), message_id, - &some_data[0]); + some_data.data()); } - EXPECT_TRUE(waiter->WaitFor(times, period_msec)); + EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); } TEST_F(ProtocolHandlerImplTest, DISABLED_FloodVerificationDisable) { @@ -3257,6 +3349,14 @@ TEST_F(ProtocolHandlerImplTest, DISABLED_FloodVerificationDisable) { AddSession(waiter, times); + connection_handler::SessionTransports st; + st.primary_transport = connection_id; + ON_CALL(connection_handler_mock, GetSessionTransports(session_id)) + .WillByDefault(Return(st)); + + ON_CALL(session_observer_mock, KeyFromPair(connection_id, session_id)) + .WillByDefault(Return(connection_key)); + // Expect NO flood notification to session observer for (size_t i = 0; i < max_messages + 1; ++i) { SendTMMessage(connection_id, @@ -3268,7 +3368,7 @@ TEST_F(ProtocolHandlerImplTest, DISABLED_FloodVerificationDisable) { session_id, some_data.size(), message_id, - &some_data[0]); + some_data.data()); } EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); @@ -3300,7 +3400,7 @@ TEST_F(ProtocolHandlerImplTest, MalformedVerificationDisable) { session_id, some_data.size(), message_id, - &some_data[0]); + some_data.data()); } EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); @@ -3322,6 +3422,14 @@ TEST_F(ProtocolHandlerImplTest, DISABLED_MalformedLimitVerification) { .WillOnce(NotifyTestAsyncWaiter(waiter)); times++; + connection_handler::SessionTransports st; + st.primary_transport = connection_id; + ON_CALL(connection_handler_mock, GetSessionTransports(session_id)) + .WillByDefault(Return(st)); + + ON_CALL(session_observer_mock, KeyFromPair(connection_id, session_id)) + .WillByDefault(Return(connection_key)); + // Sending malformed packets const uint8_t malformed_version = PROTOCOL_VERSION_MAX; for (size_t i = 0; i < max_messages * 2; ++i) { @@ -3335,7 +3443,7 @@ TEST_F(ProtocolHandlerImplTest, DISABLED_MalformedLimitVerification) { session_id, some_data.size(), message_id, - &some_data[0]); + some_data.data()); // Common message SendTMMessage(connection_id, PROTOCOL_VERSION_1, @@ -3346,7 +3454,7 @@ TEST_F(ProtocolHandlerImplTest, DISABLED_MalformedLimitVerification) { session_id, some_data.size(), message_id, - &some_data[0]); + some_data.data()); } EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); @@ -3369,6 +3477,14 @@ TEST_F(ProtocolHandlerImplTest, .WillOnce(NotifyTestAsyncWaiter(waiter)); times++; + connection_handler::SessionTransports st; + st.primary_transport = connection_id; + ON_CALL(connection_handler_mock, GetSessionTransports(session_id)) + .WillByDefault(Return(st)); + + ON_CALL(session_observer_mock, KeyFromPair(connection_id, session_id)) + .WillByDefault(Return(connection_key)); + // Sending malformed packets const uint8_t malformed_version = PROTOCOL_VERSION_MAX; const uint8_t malformed_frame_type = FRAME_TYPE_MAX_VALUE; @@ -3384,7 +3500,7 @@ TEST_F(ProtocolHandlerImplTest, session_id, some_data.size(), message_id, - &some_data[0]); + some_data.data()); // Malformed message 2 SendTMMessage(connection_id, PROTOCOL_VERSION_1, @@ -3395,7 +3511,7 @@ TEST_F(ProtocolHandlerImplTest, session_id, some_data.size(), message_id, - &some_data[0]); + some_data.data()); // Malformed message 3 SendTMMessage(connection_id, PROTOCOL_VERSION_1, @@ -3406,7 +3522,7 @@ TEST_F(ProtocolHandlerImplTest, session_id, some_data.size(), message_id, - &some_data[0]); + some_data.data()); // Common message SendTMMessage(connection_id, @@ -3418,7 +3534,7 @@ TEST_F(ProtocolHandlerImplTest, session_id, some_data.size(), message_id, - &some_data[0]); + some_data.data()); } EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); @@ -3454,7 +3570,7 @@ TEST_F(ProtocolHandlerImplTest, MalformedLimitVerification_MalformedOnly) { session_id, some_data.size(), message_id, - &some_data[0]); + some_data.data()); // Malformed message 2 SendTMMessage(connection_id, PROTOCOL_VERSION_1, @@ -3465,7 +3581,7 @@ TEST_F(ProtocolHandlerImplTest, MalformedLimitVerification_MalformedOnly) { session_id, some_data.size(), message_id, - &some_data[0]); + some_data.data()); // Malformed message 3 SendTMMessage(connection_id, PROTOCOL_VERSION_1, @@ -3476,7 +3592,7 @@ TEST_F(ProtocolHandlerImplTest, MalformedLimitVerification_MalformedOnly) { session_id, some_data.size(), message_id, - &some_data[0]); + some_data.data()); // No common message } @@ -3511,7 +3627,7 @@ TEST_F(ProtocolHandlerImplTest, MalformedLimitVerification_NullTimePeriod) { session_id, some_data.size(), message_id, - &some_data[0]); + some_data.data()); } EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); @@ -3544,7 +3660,7 @@ TEST_F(ProtocolHandlerImplTest, MalformedLimitVerification_NullCount) { session_id, some_data.size(), message_id, - &some_data[0]); + some_data.data()); } EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); @@ -3565,7 +3681,7 @@ TEST_F(ProtocolHandlerImplTest, OnFinalMessageCallback) { session_id, total_data_size, message_id, - &data[0])); + data.data())); using RawFordMessageToMobile = protocol_handler::impl::RawFordMessageToMobile; using Handler = protocol_handler::impl::ToMobileQueue::Handler; @@ -3621,10 +3737,13 @@ TEST_F(ProtocolHandlerImplTest, AddSession(waiter, times); + ON_CALL(session_observer_mock, KeyFromPair(connection_id, session_id)) + .WillByDefault(Return(connection_key)); + // Expect check connection with ProtocolVersionUsed EXPECT_CALL(session_observer_mock, ProtocolVersionUsed(connection_id, session_id, An<uint8_t&>())) - .WillOnce(Return(true)); + .WillOnce(DoAll(SetArgReferee<2>(PROTOCOL_VERSION_3), Return(true))); // Expect send End Service EXPECT_CALL( transport_manager_mock, @@ -3732,6 +3851,30 @@ TEST_F(ProtocolHandlerImplTest, SendHeartBeatAck_Successful) { } TEST_F(ProtocolHandlerImplTest, + SendHeartBeatAck_ProtocolVersionUsedFail_Cancelled) { + // Arrange + std::shared_ptr<TestAsyncWaiter> waiter = std::make_shared<TestAsyncWaiter>(); + uint32_t times = 0; + + AddSession(waiter, times); + // ProtocolVersionUsed fails + EXPECT_CALL(session_observer_mock, + ProtocolVersionUsed(connection_id, _, An<uint8_t&>())) + .WillOnce(DoAll(NotifyTestAsyncWaiter(waiter), + SetArgReferee<2>(PROTOCOL_VERSION_3), + Return(true))) + .WillOnce(DoAll(NotifyTestAsyncWaiter(waiter), Return(false))); + times += 2; + // Expect no HeartBeatAck sent + EXPECT_CALL(transport_manager_mock, SendMessageToDevice(_)).Times(0); + // Act + SendControlMessage( + PROTECTION_OFF, kControl, session_id, FRAME_DATA_HEART_BEAT); + + EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); +} + +TEST_F(ProtocolHandlerImplTest, DISABLED_SendHeartBeatAck_WrongProtocolVersion_NotSent) { // Arrange std::shared_ptr<TestAsyncWaiter> waiter = std::make_shared<TestAsyncWaiter>(); @@ -3739,6 +3882,9 @@ TEST_F(ProtocolHandlerImplTest, AddSession(waiter, times); + ON_CALL(session_observer_mock, KeyFromPair(connection_id, session_id)) + .WillByDefault(Return(connection_key)); + // Expect two checks of connection and protocol version with // ProtocolVersionUsed EXPECT_CALL(session_observer_mock, @@ -3770,12 +3916,11 @@ TEST_F(ProtocolHandlerImplTest, AddSession(waiter, times); - const bool is_final = true; const uint32_t total_data_size = 1; UCharDataVector data(total_data_size); RawMessagePtr message = std::make_shared<RawMessage>(connection_key, PROTOCOL_VERSION_3, - &data[0], + data.data(), total_data_size, false, kControl); @@ -3799,7 +3944,7 @@ TEST_F(ProtocolHandlerImplTest, times++; // Act - protocol_handler_impl->SendMessageToMobileApp(message, false, is_final); + protocol_handler_impl->SendMessageToMobileApp(message, false, kFinalMessage); EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); } @@ -3812,12 +3957,11 @@ TEST_F(ProtocolHandlerImplTest, AddSession(waiter, times); - const bool is_final = true; const uint32_t total_data_size = 1; UCharDataVector data(total_data_size); RawMessagePtr message = std::make_shared<RawMessage>(connection_key, PROTOCOL_VERSION_3, - &data[0], + data.data(), total_data_size, false, kRpc); @@ -3846,7 +3990,7 @@ TEST_F(ProtocolHandlerImplTest, times++; // Act - protocol_handler_impl->SendMessageToMobileApp(message, false, is_final); + protocol_handler_impl->SendMessageToMobileApp(message, false, kFinalMessage); EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); } @@ -3858,13 +4002,12 @@ TEST_F(ProtocolHandlerImplTest, SendMessageToMobileApp_SendMultiframeMessage) { AddSession(waiter, times); - const bool is_final = true; const uint32_t total_data_size = MAXIMUM_FRAME_DATA_V2_SIZE * 2; UCharDataVector data(total_data_size); const uint8_t first_consecutive_frame = 0x01; RawMessagePtr message = std::make_shared<RawMessage>(connection_key, PROTOCOL_VERSION_3, - &data[0], + data.data(), total_data_size, false, kBulk); @@ -3905,11 +4048,1063 @@ TEST_F(ProtocolHandlerImplTest, SendMessageToMobileApp_SendMultiframeMessage) { times++; // Act - protocol_handler_impl->SendMessageToMobileApp(message, false, is_final); + protocol_handler_impl->SendMessageToMobileApp(message, false, kFinalMessage); EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); } +TEST_F(ProtocolHandlerImplTest, + SendMessageToMobileApp_NullMessagePointer_Cancelled) { + // Arrange + std::shared_ptr<TestAsyncWaiter> waiter = std::make_shared<TestAsyncWaiter>(); + uint32_t times = 0; + + AddSession(waiter, times); + +#ifdef ENABLE_SECURITY + EXPECT_CALL(session_observer_mock, GetSSLContext(_, _)).Times(0); +#endif // ENABLE_SECURITY + EXPECT_CALL(transport_manager_mock, SendMessageToDevice(_)).Times(0); + // Act + RawMessagePtr message; + protocol_handler_impl->SendMessageToMobileApp(message, false, kFinalMessage); + + EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); +} + +TEST_F(ProtocolHandlerImplTest, + OnTMMessageSend_ReadyToCloseConnection_Disconnect) { + OnTMMessageSend(); + const uint8_t data_size = 0u; + ProtocolFramePtr protocol_packet = + std::make_shared<ProtocolPacket>(connection_id, + PROTOCOL_VERSION_3, + PROTECTION_OFF, + FRAME_TYPE_SINGLE, + kRpc, + FRAME_DATA_SINGLE, + session_id, + data_size, + message_id, + null_data_); + + RawMessagePtr message = protocol_packet->serializePacket(); + EXPECT_CALL(session_observer_mock, + PairFromKey(message->connection_key(), _, _)) + .WillOnce( + DoAll(SetArgPointee<1>(connection_id), SetArgPointee<2>(session_id))); + + EXPECT_CALL(transport_manager_mock, Disconnect(connection_id)); + EXPECT_CALL(session_observer_mock, OnFinalMessageCallback(connection_id)); + EXPECT_CALL(session_observer_mock, ProtocolVersionUsed(_, _, An<uint8_t&>())) + .Times(0); + + tm_listener->OnTMMessageSend(message); +} + +TEST_F(ProtocolHandlerImplTest, OnTMMessageSend_InvalidData_Cancelled) { + const uint8_t data_size = 8u; + uint8_t data[data_size]; + // Create invalid data + for (uint8_t i = 0; i < data_size; ++i) { + data[i] = -1; + } + RawMessagePtr message = std::make_shared<RawMessage>( + connection_key, PROTOCOL_VERSION_3, data, data_size, PROTECTION_OFF); + EXPECT_CALL(session_observer_mock, + PairFromKey(message->connection_key(), _, _)) + .WillOnce( + DoAll(SetArgPointee<1>(connection_id), SetArgPointee<2>(session_id))); + + EXPECT_CALL(transport_manager_mock, Disconnect(0)).Times(0); + EXPECT_CALL(session_observer_mock, ProtocolVersionUsed(_, _, An<uint8_t&>())) + .Times(0); + + tm_listener->OnTMMessageSend(message); +} + +TEST_F(ProtocolHandlerImplTest, SendFrame_NullMessage_FAIL) { + auto protocol_handler_implas_listener = + static_cast<protocol_handler::impl::ToMobileQueue::Handler*>( + protocol_handler_impl.get()); + EXPECT_CALL(transport_manager_mock, SendMessageToDevice(_)).Times(0); + protocol_handler::impl::RawFordMessageToMobile null_message; + protocol_handler_implas_listener->Handle(null_message); +} + +TEST_F(ProtocolHandlerImplTest, SendFrame_SendMessageToDeviceFailed_FAIL) { + auto protocol_handler_implas_listener = + static_cast<protocol_handler::impl::ToMobileQueue::Handler*>( + protocol_handler_impl.get()); + protocol_handler::impl::RawFordMessageToMobile message( + std::make_shared<protocol_handler::ProtocolPacket>(connection_id, + PROTOCOL_VERSION_3, + PROTECTION_OFF, + FRAME_TYPE_CONTROL, + kControl, + FRAME_DATA_HEART_BEAT, + session_id, + some_data.size(), + message_id, + some_data.data()), + kFinalMessage); + EXPECT_CALL(transport_manager_mock, SendMessageToDevice(_)) + .WillOnce(Return(transport_manager::E_INTERNAL_ERROR)); + protocol_handler_implas_listener->Handle(message); +} + +TEST_F(ProtocolHandlerImplTest, Handle_SingleFrameMessage_SUCCESS) { + auto protocol_handler_implas_listener = + static_cast<protocol_handler::impl::FromMobileQueue::Handler*>( + protocol_handler_impl.get()); + const uint8_t data_size = 1u; + protocol_handler::impl::RawFordMessageFromMobile message( + std::make_shared<protocol_handler::ProtocolPacket>(connection_id, + PROTOCOL_VERSION_3, + PROTECTION_OFF, + FRAME_TYPE_SINGLE, + kControl, + FRAME_DATA_SINGLE, + session_id, + data_size, + message_id, + some_data.data())); + + connection_handler::SessionTransports st; + st.primary_transport = connection_id; + ON_CALL(connection_handler_mock, GetSessionTransports(session_id)) + .WillByDefault(Return(st)); + + protocol_handler_impl->SetTelemetryObserver(&telemetry_observer_mock); + EXPECT_CALL(telemetry_observer_mock, EndMessageProcess(_)); + + protocol_handler_implas_listener->Handle(message); +} + +TEST_F(ProtocolHandlerImplTest, + HandleControlMessageEndServiceACK_SessionKeyZero_FAIL) { + auto protocol_handler_impl_as_listener = + static_cast<protocol_handler::impl::FromMobileQueue::Handler*>( + protocol_handler_impl.get()); + protocol_handler::impl::RawFordMessageFromMobile message( + std::make_shared<protocol_handler::ProtocolPacket>( + connection_id, + PROTOCOL_VERSION_3, + PROTECTION_OFF, + FRAME_TYPE_CONTROL, + kControl, + FRAME_DATA_END_SERVICE_ACK, + session_id, + some_data.size(), + message_id, + some_data.data())); + + EXPECT_CALL(session_observer_mock, + OnSessionEndedCallback(connection_id, + session_id, + An<uint32_t*>(), + kControl, + An<std::string*>())) + .WillOnce(Return(kInvalidSessionId)); + + protocol_handler_impl_as_listener->Handle(message); +} + +TEST_F(ProtocolHandlerImplTest, Handle_ControlMessage_EndServiceACK_SUCCESS) { + auto protocol_handler_impl_as_listener = + static_cast<protocol_handler::impl::FromMobileQueue::Handler*>( + protocol_handler_impl.get()); + protocol_handler::impl::RawFordMessageFromMobile message( + std::make_shared<protocol_handler::ProtocolPacket>( + connection_id, + PROTOCOL_VERSION_3, + PROTECTION_OFF, + FRAME_TYPE_CONTROL, + kControl, + FRAME_DATA_END_SERVICE_ACK, + session_id, + some_data.size(), + message_id, + some_data.data())); + EXPECT_CALL(session_observer_mock, + OnSessionEndedCallback(connection_id, + session_id, + An<uint32_t*>(), + kControl, + An<std::string*>())) + .WillOnce(Return(kValidSessionId)); + + protocol_handler_impl_as_listener->Handle(message); +} + +TEST_F(ProtocolHandlerImplTest, Handle_ControlMessage_HeartBeatAck_SUCCESS) { + auto protocol_handler_impl_as_listener = + static_cast<protocol_handler::impl::FromMobileQueue::Handler*>( + protocol_handler_impl.get()); + protocol_handler::impl::RawFordMessageFromMobile message( + std::make_shared<protocol_handler::ProtocolPacket>( + connection_id, + PROTOCOL_VERSION_3, + PROTECTION_OFF, + FRAME_TYPE_CONTROL, + kControl, + FRAME_DATA_HEART_BEAT_ACK, + session_id, + some_data.size(), + message_id, + some_data.data())); + EXPECT_CALL(session_observer_mock, + OnSessionEndedCallback(_, _, An<uint32_t*>(), _)) + .Times(0); + EXPECT_CALL(session_observer_mock, OnSessionStartedCallback(_, _, _, _, _)) + .Times(0); + + protocol_handler_impl_as_listener->Handle(message); +} + +TEST_F(ProtocolHandlerImplTest, Handle_ControlMessage_Default_SUCCESS) { + auto protocol_handler_impl_as_listener = + static_cast<protocol_handler::impl::FromMobileQueue::Handler*>( + protocol_handler_impl.get()); + protocol_handler::impl::RawFordMessageFromMobile message( + std::make_shared<protocol_handler::ProtocolPacket>( + connection_id, + PROTOCOL_VERSION_3, + PROTECTION_OFF, + FRAME_TYPE_CONTROL, + kControl, + FRAME_DATA_END_SERVICE_NACK, + session_id, + some_data.size(), + message_id, + some_data.data())); + EXPECT_CALL(session_observer_mock, + OnSessionEndedCallback(_, _, An<uint32_t*>(), _)) + .Times(0); + EXPECT_CALL(session_observer_mock, OnSessionStartedCallback(_, _, _, _, _)) + .Times(0); + + protocol_handler_impl_as_listener->Handle(message); +} + +TEST_F(ProtocolHandlerImplTest, + GetHashId_FirstProtocolVersion_HashNotSupported) { + auto protocol_handler_impl_as_listener = + static_cast<protocol_handler::impl::FromMobileQueue::Handler*>( + protocol_handler_impl.get()); + protocol_handler::impl::RawFordMessageFromMobile message( + std::make_shared<protocol_handler::ProtocolPacket>( + connection_id, + PROTOCOL_VERSION_1, + PROTECTION_OFF, + FRAME_TYPE_CONTROL, + kControl, + FRAME_DATA_END_SERVICE_ACK, + session_id, + some_data.size(), + message_id, + some_data.data())); + + uint32_t hash_id; + EXPECT_CALL(session_observer_mock, + OnSessionEndedCallback(connection_id, + session_id, + An<uint32_t*>(), + kControl, + An<std::string*>())) + .WillOnce(DoAll(SaveArgPointee<2>(&hash_id), Return(kInvalidSessionId))); + + protocol_handler_impl_as_listener->Handle(message); + + EXPECT_EQ(hash_id, protocol_handler::HASH_ID_NOT_SUPPORTED); +} + +TEST_F(ProtocolHandlerImplTest, GetHashId_SecondProtocolVersion_HashWrong) { + auto protocol_handler_impl_as_listener = + static_cast<protocol_handler::impl::FromMobileQueue::Handler*>( + protocol_handler_impl.get()); + + some_data.resize(3); + std::fill(some_data.begin(), some_data.end(), 0xAA); + + protocol_handler::impl::RawFordMessageFromMobile message( + std::make_shared<protocol_handler::ProtocolPacket>( + connection_id, + PROTOCOL_VERSION_2, + PROTECTION_OFF, + FRAME_TYPE_CONTROL, + kControl, + FRAME_DATA_END_SERVICE_ACK, + session_id, + some_data.size(), + message_id, + some_data.data())); + + uint32_t hash_id; + EXPECT_CALL(session_observer_mock, + OnSessionEndedCallback(connection_id, + session_id, + An<uint32_t*>(), + kControl, + An<std::string*>())) + .WillOnce(DoAll(SaveArgPointee<2>(&hash_id), Return(kInvalidSessionId))); + + protocol_handler_impl_as_listener->Handle(message); + + EXPECT_EQ(hash_id, protocol_handler::HASH_ID_WRONG); +} + +TEST_F(ProtocolHandlerImplTest, GetHashId_CorrectData_CorrectHash) { + auto protocol_handler_impl_as_listener = + static_cast<protocol_handler::impl::FromMobileQueue::Handler*>( + protocol_handler_impl.get()); + + // 4 - because hash id not works with data.size < 4 + some_data.resize(4); + std::fill(some_data.begin(), some_data.end(), 0xAA); + + protocol_handler::impl::RawFordMessageFromMobile message( + std::make_shared<protocol_handler::ProtocolPacket>( + connection_id, + PROTOCOL_VERSION_3, + PROTECTION_OFF, + FRAME_TYPE_CONTROL, + kControl, + FRAME_DATA_END_SERVICE_ACK, + session_id, + some_data.size(), + message_id, + some_data.data())); + + const uint32_t exp_hash_id = + BE_TO_LE32(*(reinterpret_cast<uint32_t*>(some_data.data()))); + uint32_t hash_id; + + EXPECT_CALL(session_observer_mock, + OnSessionEndedCallback(connection_id, + session_id, + An<uint32_t*>(), + kControl, + An<std::string*>())) + .WillOnce(DoAll(SaveArgPointee<2>(&hash_id), Return(kValidSessionId))); + + protocol_handler_impl_as_listener->Handle(message); + + EXPECT_EQ(hash_id, exp_hash_id); +} + +TEST_F(ProtocolHandlerImplTest, GetHashId_InvalidData_WrongHash) { + auto protocol_handler_impl_as_listener = + static_cast<protocol_handler::impl::FromMobileQueue::Handler*>( + protocol_handler_impl.get()); + + some_data.resize(8); + std::fill(some_data.begin(), some_data.end(), 0); + + protocol_handler::impl::RawFordMessageFromMobile message( + std::make_shared<protocol_handler::ProtocolPacket>( + connection_id, + PROTOCOL_VERSION_3, + PROTECTION_OFF, + FRAME_TYPE_CONTROL, + kControl, + FRAME_DATA_END_SERVICE_ACK, + session_id, + some_data.size(), + message_id, + some_data.data())); + + uint32_t hash_id; + EXPECT_CALL(session_observer_mock, + OnSessionEndedCallback(connection_id, + session_id, + An<uint32_t*>(), + kControl, + An<std::string*>())) + .WillOnce(DoAll(SaveArgPointee<2>(&hash_id), Return(kValidSessionId))); + + protocol_handler_impl_as_listener->Handle(message); + + EXPECT_EQ(hash_id, protocol_handler::HASH_ID_WRONG); +} + +TEST_F(ProtocolHandlerImplTest, GetHashId_ProtocolVersion5_ValidData) { + auto protocol_handler_impl_as_listener = + static_cast<protocol_handler::impl::FromMobileQueue::Handler*>( + protocol_handler_impl.get()); + + const uint8_t data_size = 5u; + uint8_t data[data_size] = {data_size}; + + BsonObject obj = bson_object_from_bytes(data); + const uint32_t exp_hash_id = + (uint32_t)bson_object_get_int32(&obj, protocol_handler::strings::hash_id); + bson_object_deinitialize(&obj); + + protocol_handler::impl::RawFordMessageFromMobile message( + std::make_shared<protocol_handler::ProtocolPacket>( + connection_id, + PROTOCOL_VERSION_5, + PROTECTION_OFF, + FRAME_TYPE_CONTROL, + kControl, + FRAME_DATA_END_SERVICE_ACK, + session_id, + data_size, + message_id, + data)); + + uint32_t hash_id; + EXPECT_CALL(session_observer_mock, + OnSessionEndedCallback(connection_id, + session_id, + An<uint32_t*>(), + kControl, + An<std::string*>())) + .WillOnce(DoAll(SaveArgPointee<2>(&hash_id), Return(kValidSessionId))); + + protocol_handler_impl_as_listener->Handle(message); + + EXPECT_EQ(hash_id, exp_hash_id); +} + +TEST_F(ProtocolHandlerImplTest, SetHashId_CorrectHashId) { + std::shared_ptr<TestAsyncWaiter> waiter = std::make_shared<TestAsyncWaiter>(); + uint32_t times = 0; + + const uint8_t input_protocol_version = 3; + utils::SemanticVersion full_version(3, 1, 0); + // hash id should be any except HASH_ID_WRONG(0xFFFF0000) and + // HASH_ID_NOT_SUPPORTED(0) + const uint32_t hash_id = 42; + + const uint32_t hash_id_be = LE_TO_BE32(hash_id); + const uint8_t* exp_data = reinterpret_cast<const uint8_t*>(&hash_id_be); + const uint32_t exp_data_size = sizeof(hash_id_be); + + SetProtocolVersion2(); + + RawMessagePtr raw_message; + EXPECT_CALL(transport_manager_mock, SendMessageToDevice(_)) + .WillOnce(DoAll(NotifyTestAsyncWaiter(waiter), + SaveArg<0>(&raw_message), + Return(transport_manager::E_SUCCESS))); + times++; + + protocol_handler_impl->SendStartSessionAck(connection_id, + session_id, + input_protocol_version, + hash_id, + kRpc, + PROTECTION_OFF, + full_version); + + EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); + + ProtocolPacket protocol_packet; + RESULT_CODE res = protocol_packet.deserializePacket(raw_message->data(), + raw_message->data_size()); + ASSERT_EQ(protocol_handler::RESULT_CODE::RESULT_OK, res); + + EXPECT_THAT(std::vector<uint8_t>(exp_data, exp_data + exp_data_size), + ElementsAreArray(protocol_packet.data(), + protocol_packet.total_data_bytes())); +} + +TEST_F(ProtocolHandlerImplTest, SetHashId_HASH_ID_NOT_SUPPORTED) { + std::shared_ptr<TestAsyncWaiter> waiter = std::make_shared<TestAsyncWaiter>(); + uint32_t times = 0; + + const uint8_t input_protocol_version = 3; + const uint32_t hash_id = protocol_handler::HASH_ID_NOT_SUPPORTED; + utils::SemanticVersion full_version(3, 1, 0); + + SetProtocolVersion2(); + + RawMessagePtr raw_message; + EXPECT_CALL(transport_manager_mock, SendMessageToDevice(_)) + .WillOnce(DoAll(NotifyTestAsyncWaiter(waiter), + SaveArg<0>(&raw_message), + Return(transport_manager::E_SUCCESS))); + times++; + + protocol_handler_impl->SendStartSessionAck(connection_id, + session_id, + input_protocol_version, + hash_id, + kRpc, + PROTECTION_OFF, + full_version); + + EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); + + ProtocolPacket protocol_packet; + RESULT_CODE res = protocol_packet.deserializePacket(raw_message->data(), + raw_message->data_size()); + ASSERT_EQ(protocol_handler::RESULT_CODE::RESULT_OK, res); + + EXPECT_EQ(0u, protocol_packet.total_data_bytes()); +} + +TEST_F(ProtocolHandlerImplTest, SetHashId_HASH_ID_WRONG) { + std::shared_ptr<TestAsyncWaiter> waiter = std::make_shared<TestAsyncWaiter>(); + uint32_t times = 0; + + const uint8_t input_protocol_version = 3; + const uint32_t hash_id = protocol_handler::HASH_ID_WRONG; + utils::SemanticVersion full_version(3, 1, 0); + + SetProtocolVersion2(); + + RawMessagePtr raw_message; + EXPECT_CALL(transport_manager_mock, SendMessageToDevice(_)) + .WillOnce(DoAll(NotifyTestAsyncWaiter(waiter), + SaveArg<0>(&raw_message), + Return(transport_manager::E_SUCCESS))); + times++; + + protocol_handler_impl->SendStartSessionAck(connection_id, + session_id, + input_protocol_version, + hash_id, + kRpc, + PROTECTION_OFF, + full_version); + + EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); + + ProtocolPacket protocol_packet; + RESULT_CODE res = protocol_packet.deserializePacket(raw_message->data(), + raw_message->data_size()); + + ASSERT_EQ(protocol_handler::RESULT_CODE::RESULT_OK, res); + + EXPECT_EQ(0u, protocol_packet.total_data_bytes()); +} + +TEST_F(ProtocolHandlerImplTest, PopValidAndExpiredMultiframes) { + using namespace protocol_handler; + std::shared_ptr<TestAsyncWaiter> waiter = std::make_shared<TestAsyncWaiter>(); + uint32_t times = 0; + + AddSession(waiter, times); + + const uint8_t data_size = 1u; + uint8_t data = 7u; + ProtocolFramePtr first_frame = CreateFramePtrWithData(data, FRAME_TYPE_FIRST); + ProtocolFramePtr consecutive_frame = + std::make_shared<ProtocolPacket>(connection_id, + PROTOCOL_VERSION_3, + PROTECTION_OFF, + FRAME_TYPE_CONSECUTIVE, + kMobileNav, + FRAME_DATA_LAST_CONSECUTIVE, + session_id, + data_size, + message_id, + &data); + + protocol_handler_impl->AddProtocolObserver(&protocol_observer_mock); + + connection_handler::SessionTransports st; + st.primary_transport = connection_id; + ON_CALL(connection_handler_mock, GetSessionTransports(session_id)) + .WillByDefault(Return(st)); + + EXPECT_CALL(session_observer_mock, KeyFromPair(connection_id, _)) + .WillRepeatedly(Return(connection_id)); + + EXPECT_CALL(protocol_observer_mock, OnMessageReceived(_)); + + protocol_handler::impl::FromMobileQueue::Handler* handler = + protocol_handler_impl.get(); + protocol_handler::impl::RawFordMessageFromMobile first_message(first_frame); + handler->Handle(first_message); + protocol_handler::impl::RawFordMessageFromMobile consecutive_message( + consecutive_frame); + handler->Handle(consecutive_message); + + EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); +} + +TEST_F(ProtocolHandlerImplTest, HandleFromMobile_FrameTypeSingle_Handled) { + std::shared_ptr<TestAsyncWaiter> waiter = std::make_shared<TestAsyncWaiter>(); + uint32_t times = 0; + + AddSession(waiter, times); + + const uint8_t data_value = 7u; + ProtocolFramePtr frame_ptr = + CreateFramePtrWithData(data_value, FRAME_TYPE_SINGLE); + + connection_handler::SessionTransports st; + st.primary_transport = connection_id; + ON_CALL(connection_handler_mock, GetSessionTransports(session_id)) + .WillByDefault(Return(st)); + + protocol_handler_impl->AddProtocolObserver(&protocol_observer_mock); + EXPECT_CALL(protocol_observer_mock, OnMessageReceived(_)); + + protocol_handler::impl::FromMobileQueue::Handler* handler = + protocol_handler_impl.get(); + protocol_handler::impl::RawFordMessageFromMobile message(frame_ptr); + handler->Handle(message); + + EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); +} + +#ifdef ENABLE_SECURITY +TEST_F(ProtocolHandlerImplTest, EncryptFrame_NoSecurityManagerSet_Cancelled) { + std::shared_ptr<TestAsyncWaiter> waiter = std::make_shared<TestAsyncWaiter>(); + uint32_t times = 0; + + AddSession(waiter, times); + + const uint8_t data_size = 0u; + ProtocolFramePtr frame_ptr = + std::make_shared<ProtocolPacket>(connection_id, + PROTOCOL_VERSION_3, + PROTECTION_OFF, + FRAME_TYPE_SINGLE, + kAudio, + FRAME_DATA_END_SERVICE_ACK, + session_id, + data_size, + message_id, + null_data_); + + EXPECT_CALL(transport_manager_mock, SendMessageToDevice(_)).Times(0); + + protocol_handler::impl::ToMobileQueue::Handler* handler = + protocol_handler_impl.get(); + protocol_handler::impl::RawFordMessageToMobile message(frame_ptr, + kFinalMessage); + handler->Handle(message); + + EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); +} + +TEST_F(ProtocolHandlerImplTest, + EncryptFrame_ControlFrameType_ContinueUnencrypted) { + std::shared_ptr<TestAsyncWaiter> waiter = std::make_shared<TestAsyncWaiter>(); + uint32_t times = 0; + + AddSession(waiter, times); + AddSecurityManager(); + + const uint8_t data_value = 7u; + ProtocolFramePtr frame_ptr = + CreateFramePtrWithData(data_value, FRAME_TYPE_CONTROL); + + RawMessagePtr raw_message; + EXPECT_CALL(transport_manager_mock, SendMessageToDevice(_)) + .WillOnce(DoAll(NotifyTestAsyncWaiter(waiter), + SaveArg<0>(&raw_message), + Return(transport_manager::E_SUCCESS))); + times++; + + protocol_handler::impl::ToMobileQueue::Handler* handler = + protocol_handler_impl.get(); + protocol_handler::impl::RawFordMessageToMobile message(frame_ptr, + kFinalMessage); + handler->Handle(message); + + EXPECT_EQ(data_value, + raw_message->data()[protocol_handler::PROTOCOL_HEADER_V2_SIZE]); + + EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); +} + +TEST_F(ProtocolHandlerImplTest, + EncryptFrame_SSLContextInitNotCompleted_ContinueUnencrypted) { + std::shared_ptr<TestAsyncWaiter> waiter = std::make_shared<TestAsyncWaiter>(); + uint32_t times = 0; + + AddSession(waiter, times); + AddSecurityManager(); + + const uint8_t data_value = 7u; + ProtocolFramePtr frame_ptr = + CreateFramePtrWithData(data_value, FRAME_TYPE_SINGLE); + + EXPECT_CALL(session_observer_mock, GetSSLContext(connection_key, _)) + .WillOnce( + DoAll(NotifyTestAsyncWaiter(waiter), Return(&ssl_context_mock))); + times++; + EXPECT_CALL(ssl_context_mock, IsInitCompleted()) + .WillOnce(DoAll(NotifyTestAsyncWaiter(waiter), Return(false))); + times++; + + RawMessagePtr raw_message; + EXPECT_CALL(transport_manager_mock, SendMessageToDevice(_)) + .WillOnce(DoAll(NotifyTestAsyncWaiter(waiter), + SaveArg<0>(&raw_message), + Return(transport_manager::E_SUCCESS))); + times++; + + protocol_handler::impl::ToMobileQueue::Handler* handler = + protocol_handler_impl.get(); + protocol_handler::impl::RawFordMessageToMobile message(frame_ptr, + kFinalMessage); + handler->Handle(message); + + EXPECT_EQ(data_value, + raw_message->data()[protocol_handler::PROTOCOL_HEADER_V2_SIZE]); + + EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); +} + +TEST_F(ProtocolHandlerImplTest, + EncryptFrame_EncryptFailed_ContinueUnencrypted) { + std::shared_ptr<TestAsyncWaiter> waiter = std::make_shared<TestAsyncWaiter>(); + uint32_t times = 0; + + AddSession(waiter, times); + AddSecurityManager(); + + const uint8_t data_value = 7u; + ProtocolFramePtr frame_ptr = + CreateFramePtrWithData(data_value, FRAME_TYPE_SINGLE); + + EXPECT_CALL(session_observer_mock, GetSSLContext(connection_key, _)) + .WillOnce( + DoAll(NotifyTestAsyncWaiter(waiter), Return(&ssl_context_mock))); + times++; + + EXPECT_CALL(ssl_context_mock, IsInitCompleted()) + .WillOnce(DoAll(NotifyTestAsyncWaiter(waiter), Return(true))); + times++; + + EXPECT_CALL(session_observer_mock, + OnSessionEndedCallback(connection_id, + session_id, + An<uint32_t*>(), + kRpc, + An<std::string*>())); + + const uint8_t data_size = 1u; + EXPECT_CALL(ssl_context_mock, Encrypt(frame_ptr->data(), data_size, _, _)) + .WillOnce(DoAll(NotifyTestAsyncWaiter(waiter), Return(false))); + times++; + + RawMessagePtr raw_message; + EXPECT_CALL(transport_manager_mock, SendMessageToDevice(_)) + .WillOnce(DoAll(NotifyTestAsyncWaiter(waiter), + SaveArg<0>(&raw_message), + Return(transport_manager::E_SUCCESS))); + times++; + + protocol_handler::impl::ToMobileQueue::Handler* handler = + protocol_handler_impl.get(); + protocol_handler::impl::RawFordMessageToMobile message(frame_ptr, + kFinalMessage); + message->set_protection_flag(PROTECTION_ON); + handler->Handle(message); + + EXPECT_EQ(data_value, + raw_message->data()[protocol_handler::PROTOCOL_HEADER_V2_SIZE]); + + EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); +} + +TEST_F(ProtocolHandlerImplTest, + EncryptFrame_EncryptSucceeded_ContinueEncrypted) { + std::shared_ptr<TestAsyncWaiter> waiter = std::make_shared<TestAsyncWaiter>(); + uint32_t times = 0; + + AddSession(waiter, times); + AddSecurityManager(); + + const uint8_t data_value = 7u; + ProtocolFramePtr frame_ptr = + CreateFramePtrWithData(data_value, FRAME_TYPE_SINGLE); + + EXPECT_CALL(session_observer_mock, GetSSLContext(connection_key, _)) + .WillOnce( + DoAll(NotifyTestAsyncWaiter(waiter), Return(&ssl_context_mock))); + times++; + + EXPECT_CALL(ssl_context_mock, IsInitCompleted()) + .WillOnce(DoAll(NotifyTestAsyncWaiter(waiter), Return(true))); + times++; + + const uint8_t data_size = 1u; + const uint8_t encrypted_data = 8u; + EXPECT_CALL(ssl_context_mock, Encrypt(frame_ptr->data(), data_size, _, _)) + .WillOnce(DoAll(NotifyTestAsyncWaiter(waiter), + SetArgPointee<2>(&encrypted_data), + SetArgPointee<3>(data_size), + Return(true))); + times++; + + RawMessagePtr raw_message; + EXPECT_CALL(transport_manager_mock, SendMessageToDevice(_)) + .WillOnce(DoAll(NotifyTestAsyncWaiter(waiter), + SaveArg<0>(&raw_message), + Return(transport_manager::E_SUCCESS))); + times++; + + protocol_handler::impl::ToMobileQueue::Handler* handler = + protocol_handler_impl.get(); + protocol_handler::impl::RawFordMessageToMobile message(frame_ptr, + kFinalMessage); + message->set_protection_flag(PROTECTION_ON); + handler->Handle(message); + + EXPECT_EQ(encrypted_data, + raw_message->data()[protocol_handler::PROTOCOL_HEADER_V2_SIZE]); + + EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); +} + +TEST_F(ProtocolHandlerImplTest, DecryptFrame_NoSecurityManager_Cancelled) { + std::shared_ptr<TestAsyncWaiter> waiter = std::make_shared<TestAsyncWaiter>(); + uint32_t times = 0; + + AddSession(waiter, times); + + const uint8_t data_size = 1u; + const uint8_t data_value = 7u; + ProtocolFramePtr frame_ptr = + std::make_shared<ProtocolPacket>(connection_id, + PROTOCOL_VERSION_3, + PROTECTION_ON, + FRAME_TYPE_SINGLE, + kAudio, + FRAME_DATA_SINGLE, + session_id, + data_size, + message_id, + &data_value); + + EXPECT_CALL(ssl_context_mock, Decrypt(_, _, _, _)).Times(0); + + protocol_handler_impl->SetTelemetryObserver(&telemetry_observer_mock); + EXPECT_CALL(telemetry_observer_mock, StartMessageProcess(message_id, _)) + .Times(0); + + tm_listener->OnTMMessageReceived(frame_ptr->serializePacket()); + + EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); +} + +TEST_F(ProtocolHandlerImplTest, + DISABLED_DecryptFrame_ProtectionFlagOff_ContinueUndecrypted) { + std::shared_ptr<TestAsyncWaiter> waiter = std::make_shared<TestAsyncWaiter>(); + uint32_t times = 0; + + AddSession(waiter, times); + + const uint8_t data_size = 0u; + ProtocolFramePtr frame_ptr = + std::make_shared<ProtocolPacket>(connection_id, + PROTOCOL_VERSION_3, + PROTECTION_OFF, + FRAME_TYPE_FIRST, + kAudio, + FRAME_DATA_FIRST, + session_id, + data_size, + message_id, + null_data_); + EXPECT_CALL(ssl_context_mock, Decrypt(_, _, _, _)).Times(0); + + protocol_handler_impl->SetTelemetryObserver(&telemetry_observer_mock); + EXPECT_CALL(telemetry_observer_mock, StartMessageProcess(message_id, _)); + + connection_handler::SessionTransports st; + st.primary_transport = connection_id; + ON_CALL(connection_handler_mock, GetSessionTransports(session_id)) + .WillByDefault(Return(st)); + + tm_listener->OnTMMessageReceived(frame_ptr->serializePacket()); + + EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); +} + +TEST_F(ProtocolHandlerImplTest, + DISABLED_DecryptFrame_FrameTypeControl_ContinueUndecrypted) { + std::shared_ptr<TestAsyncWaiter> waiter = std::make_shared<TestAsyncWaiter>(); + uint32_t times = 0; + + AddSession(waiter, times); + + ON_CALL(session_observer_mock, KeyFromPair(connection_id, session_id)) + .WillByDefault(Return(connection_key)); + + const uint8_t data_size = 0u; + ProtocolFramePtr frame_ptr = + std::make_shared<ProtocolPacket>(connection_id, + PROTOCOL_VERSION_3, + PROTECTION_ON, + FRAME_TYPE_CONTROL, + kControl, + FRAME_DATA_FIRST, + session_id, + data_size, + message_id, + null_data_); + EXPECT_CALL(ssl_context_mock, Decrypt(_, _, _, _)).Times(0); + + protocol_handler_impl->SetTelemetryObserver(&telemetry_observer_mock); + EXPECT_CALL(telemetry_observer_mock, StartMessageProcess(message_id, _)) + .WillOnce(NotifyTestAsyncWaiter(waiter)); + times++; + + EXPECT_CALL(session_observer_mock, + ProtocolVersionUsed(connection_id, session_id, An<uint8_t&>())); + + tm_listener->OnTMMessageReceived(frame_ptr->serializePacket()); + + EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); +} + +TEST_F(ProtocolHandlerImplTest, + DecryptFrame_SSLContextInitNotCompleted_Cancelled) { + std::shared_ptr<TestAsyncWaiter> waiter = std::make_shared<TestAsyncWaiter>(); + uint32_t times = 0; + + AddSession(waiter, times); + AddSecurityManager(); + + const uint8_t data_size = 1u; + const uint8_t data_value = 7u; + ProtocolFramePtr frame_ptr = + std::make_shared<ProtocolPacket>(connection_id, + PROTOCOL_VERSION_3, + PROTECTION_ON, + FRAME_TYPE_SINGLE, + kAudio, + FRAME_DATA_SINGLE, + session_id, + data_size, + message_id, + &data_value); + + EXPECT_CALL(session_observer_mock, GetSSLContext(connection_key, _)) + .WillOnce(Return(&ssl_context_mock)); + EXPECT_CALL(ssl_context_mock, IsInitCompleted()).WillOnce(Return(false)); + EXPECT_CALL(ssl_context_mock, Decrypt(_, _, _, _)).Times(0); + + protocol_handler_impl->SetTelemetryObserver(&telemetry_observer_mock); + EXPECT_CALL(telemetry_observer_mock, StartMessageProcess(_, _)).Times(0); + + tm_listener->OnTMMessageReceived(frame_ptr->serializePacket()); + + EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); +} + +TEST_F(ProtocolHandlerImplTest, DecryptFrame_DecryptFailed_Cancelled) { + std::shared_ptr<TestAsyncWaiter> waiter = std::make_shared<TestAsyncWaiter>(); + uint32_t times = 0; + + AddSession(waiter, times); + AddSecurityManager(); + + const uint8_t data_size = 1u; + const uint8_t data_value = 7u; + ProtocolFramePtr frame_ptr = + std::make_shared<ProtocolPacket>(connection_id, + PROTOCOL_VERSION_3, + PROTECTION_ON, + FRAME_TYPE_SINGLE, + kAudio, + FRAME_DATA_SINGLE, + session_id, + data_size, + message_id, + &data_value); + + EXPECT_CALL(session_observer_mock, GetSSLContext(connection_key, _)) + .WillOnce(Return(&ssl_context_mock)); + EXPECT_CALL(ssl_context_mock, IsInitCompleted()).WillOnce(Return(true)); + EXPECT_CALL(ssl_context_mock, Decrypt(_, _, _, _)).WillOnce(Return(false)); + + uint32_t session_message_id; + EXPECT_CALL( + session_observer_mock, + OnSessionEndedCallback( + connection_id, session_id, An<uint32_t*>(), kRpc, An<std::string*>())) + .WillOnce(DoAll(SaveArgPointee<2>(&session_message_id), + Return(kInvalidSessionId))); + + protocol_handler_impl->SetTelemetryObserver(&telemetry_observer_mock); + EXPECT_CALL(telemetry_observer_mock, StartMessageProcess(_, _)).Times(0); + + tm_listener->OnTMMessageReceived(frame_ptr->serializePacket()); + + EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); + EXPECT_EQ(session_message_id, message_id); +} + +TEST_F(ProtocolHandlerImplTest, + DecryptFrame_DecryptSucceeded_ContinueDecrypted) { + std::shared_ptr<TestAsyncWaiter> waiter = std::make_shared<TestAsyncWaiter>(); + uint32_t times = 0; + + AddSession(waiter, times); + AddSecurityManager(); + + const uint8_t data_size = 1u; + const uint8_t data_value = 7u; + ProtocolFramePtr frame_ptr = + std::make_shared<ProtocolPacket>(connection_id, + PROTOCOL_VERSION_3, + PROTECTION_ON, + FRAME_TYPE_SINGLE, + kAudio, + FRAME_DATA_SINGLE, + session_id, + data_size, + message_id, + &data_value); + EXPECT_CALL(session_observer_mock, GetSSLContext(connection_key, _)) + .WillOnce(Return(&ssl_context_mock)); + EXPECT_CALL(ssl_context_mock, IsInitCompleted()).WillOnce(Return(true)); + + EXPECT_CALL(ssl_context_mock, Decrypt(_, _, _, _)) + .WillOnce(DoAll(SetArgPointee<2>(&data_value), + SetArgPointee<3>(data_size), + Return(true))); + EXPECT_CALL(session_observer_mock, + OnSessionEndedCallback(_, _, An<uint32_t*>(), _)) + .Times(0); + + connection_handler::SessionTransports st; + st.primary_transport = connection_id; + ON_CALL(connection_handler_mock, GetSessionTransports(session_id)) + .WillByDefault(Return(st)); + + protocol_handler_impl->SetTelemetryObserver(&telemetry_observer_mock); + EXPECT_CALL(telemetry_observer_mock, StartMessageProcess(message_id, _)); + EXPECT_CALL(telemetry_observer_mock, EndMessageProcess(_)); + + tm_listener->OnTMMessageReceived(frame_ptr->serializePacket()); + + EXPECT_TRUE(waiter->WaitFor(times, kAsyncExpectationsTimeout)); +} + +TEST_F(ProtocolHandlerImplTest, SendFrame_EncryptFailed_FAIL) { + auto protocol_handler_impl_as_listener = + static_cast<protocol_handler::impl::ToMobileQueue::Handler*>( + protocol_handler_impl.get()); + EXPECT_CALL(transport_manager_mock, SendMessageToDevice(_)).Times(0); + protocol_handler::impl::RawFordMessageToMobile message( + std::make_shared<protocol_handler::ProtocolPacket>(connection_id, + PROTOCOL_VERSION_3, + PROTECTION_OFF, + FRAME_TYPE_SINGLE, + kRpc, + FRAME_DATA_SINGLE, + session_id, + some_data.size(), + message_id, + some_data.data()), + kFinalMessage); + protocol_handler_impl_as_listener->Handle(message); +} +#endif // ENABLE_SECURITY + TEST_F(ProtocolHandlerImplTest, SendServiceDataAck_PreVersion5) { std::shared_ptr<TestAsyncWaiter> waiter = std::make_shared<TestAsyncWaiter>(); uint32_t times = 0; diff --git a/src/components/utils/include/utils/convert_utils.h b/src/components/utils/include/utils/convert_utils.h index 0609164a60..ba23b620dc 100644 --- a/src/components/utils/include/utils/convert_utils.h +++ b/src/components/utils/include/utils/convert_utils.h @@ -35,33 +35,44 @@ #include <stdint.h> #include <limits> +#include <string> namespace utils { /** - * Convert int64 value to long long int value + * @brief Convert int64 value to long long int value * Using when int64 value should be assign to JSON value + * @param value to be converted + * @return conversion result */ long long int ConvertInt64ToLongLongInt(const int64_t value); /** - * Convert long long int value to int64 value + * @brief Convert long long int value to int64 value + * @param value to be converted + * @return conversion result */ int64_t ConvertLongLongIntToInt64(const long long int value); /** - * Convert uint64 value to unsigned long long int value + * @brief Convert uint64 value to unsigned long long int value * Using when uint64 value should be assign to JSON value + * @param value to be converted + * @return conversion result */ unsigned long long int ConvertUInt64ToLongLongUInt(const uint64_t value); /** - * Convert unsigned long long int value to uint64 value + * @brief Convert unsigned long long int value to uint64 value + * @param value to be converted + * @return conversion result */ uint64_t ConvertLongLongUIntToUInt64(const unsigned long long int value); /** - * Convert one number value to another type value + * @brief Convert one number value to another type value + * @param value to be converted + * @return conversion result */ template <typename InputType, typename OutputType> OutputType SafeStaticCast(const InputType value) { @@ -72,6 +83,16 @@ OutputType SafeStaticCast(const InputType value) { return static_cast<OutputType>(value); } +/** + * @brief Convert binary data to a string value + * @param data raw binary data + * @param data_size string length. Required to check whether the data is a + * printable string. + * @return conversion result + */ +std::string ConvertBinaryDataToString(const uint8_t* data, + const size_t data_size); + } // namespace utils #endif // SRC_COMPONENTS_UTILS_INCLUDE_UTILS_CONVERT_UTILS_H_ diff --git a/src/components/utils/src/convert_utils.cc b/src/components/utils/src/convert_utils.cc index 9d90318731..83f2c37651 100644 --- a/src/components/utils/src/convert_utils.cc +++ b/src/components/utils/src/convert_utils.cc @@ -70,3 +70,23 @@ uint64_t utils::ConvertLongLongUIntToUInt64( return static_cast<uint64_t>(value); } + +std::string utils::ConvertBinaryDataToString(const uint8_t* data, + const size_t data_size) { + if (!data_size) { + return std::string(); + } + + bool is_printable_array = true; + std::locale loc; + const char* text = reinterpret_cast<const char*>(data); + // Check data for printability + for (size_t i = 0; i < data_size; ++i) { + if (!std::isprint(text[i], loc)) { + is_printable_array = false; + break; + } + } + return is_printable_array ? std::string(text, data_size) + : std::string("is raw data"); +} diff --git a/src/components/include/test/protocol_handler/mock_telemetry_observer.h b/src/components/utils/test/convert_utils_test.cc index 82c42775fa..250ea1eab4 100644 --- a/src/components/include/test/protocol_handler/mock_telemetry_observer.h +++ b/src/components/utils/test/convert_utils_test.cc @@ -1,5 +1,6 @@ + /* - * Copyright (c) 2014, Ford Motor Company + * Copyright (c) 2020, Ford Motor Company * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -30,26 +31,55 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#ifndef SRC_COMPONENTS_INCLUDE_TEST_PROTOCOL_HANDLER_MOCK_TELEMETRY_OBSERVER_H_ -#define SRC_COMPONENTS_INCLUDE_TEST_PROTOCOL_HANDLER_MOCK_TELEMETRY_OBSERVER_H_ - -#include "gmock/gmock.h" - -#include "protocol_handler/telemetry_observer.h" +#include "utils/convert_utils.h" +#include "gtest/gtest.h" namespace test { namespace components { -namespace protocol_handler_test { +namespace utils_test { + +using namespace ::utils; + +TEST(ConvertUtilsTest, ConvertInt64ToLongLongInt_CorrectValue) { + int64_t value_to_convert = 42; + EXPECT_EQ(typeid(long long int), + typeid(ConvertInt64ToLongLongInt(value_to_convert))); +} + +TEST(ConvertUtilsTest, ConvertLongLongIntToInt64_CorrectValue) { + long long int value_to_convert = 42; + EXPECT_EQ(typeid(int64_t), + typeid(ConvertLongLongIntToInt64(value_to_convert))); +} + +TEST(ConvertUtilsTest, ConvertUInt64ToLongLongUInt_CorrectValue) { + uint64_t value_to_convert = 42; + EXPECT_EQ(typeid(unsigned long long int), + typeid(ConvertUInt64ToLongLongUInt(value_to_convert))); +} + +TEST(ConvertUtilsTest, ConvertLongLongUIntToUInt64_CorrectValue) { + unsigned long long int value_to_convert = 42; + EXPECT_EQ(typeid(uint64_t), + typeid(ConvertLongLongUIntToUInt64(value_to_convert))); +} + +TEST(ConvertUtilsTest, ConvertBinaryDataToString_ValidCharacteres_CorrectText) { + const uint8_t data[] = {'s', 'u', 'c', 'c', 'e', 's', 's'}; + const std::string convertion_result = "success"; + const size_t data_size = 7; + EXPECT_EQ(convertion_result, ConvertBinaryDataToString(data, data_size)); +} -class MockPHTelemetryObserver : public PHTelemetryObserver { - public: - MOCK_METHOD2(StartMessageProcess, - void(uint32_t message_id, - const date_time::TimeDuration& start_time)); - MOCK_METHOD1(EndMessageProcess, void(std::shared_ptr<MessageMetric> m)); -}; +TEST(ConvertUtilsTest, + ConvertBinaryDataToString_NotValidCharacters_CorrectText) { + const size_t data_size = 7; + uint8_t data[data_size]; + data[0] = 0u; + const std::string is_raw_data = "is raw data"; + EXPECT_EQ(is_raw_data, ConvertBinaryDataToString(data, data_size)); +} -} // namespace protocol_handler_test +} // namespace utils_test } // namespace components } // namespace test -#endif // SRC_COMPONENTS_INCLUDE_TEST_PROTOCOL_HANDLER_MOCK_TELEMETRY_OBSERVER_H_ |