diff options
-rw-r--r-- | src/components/connection_handler/test/connection_handler_impl_test.cc | 84 | ||||
-rw-r--r-- | src/components/connection_handler/test/heart_beat_monitor_test.cc | 172 |
2 files changed, 103 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 d0b9ce4ae4..69c52648e0 100644 --- a/src/components/connection_handler/test/connection_handler_impl_test.cc +++ b/src/components/connection_handler/test/connection_handler_impl_test.cc @@ -37,10 +37,8 @@ #include "connection_handler/connection_handler_impl.h" #include "protocol/common.h" // TODO(EZamakhov): move security test -#ifdef ENABLE_SECURITY #include "security_manager/mock_security_manager.h" #include "security_manager/mock_ssl_context.h" -#endif // ENABLE_SECURITY #include "protocol_handler/mock_protocol_handler.h" #include "connection_handler/mock_connection_handler_observer.h" #include "connection_handler/mock_connection_handler_settings.h" @@ -88,7 +86,7 @@ class ConnectionHandlerTest : public ::testing::Test { mac_address_ = "test_address"; connection_handler_ = new ConnectionHandlerImpl( - mock_connection_handler_settings, mock_transport_manager_); + mock_connection_handler_settings_, mock_transport_manager_); uid_ = 1u; connection_key_ = connection_handler_->KeyFromPair(0, 0u); protected_services_.clear(); @@ -101,23 +99,29 @@ class ConnectionHandlerTest : public ::testing::Test { void SetSpecificServices() { #ifdef ENABLE_SECURITY - ON_CALL(mock_connection_handler_settings, force_protected_service()) + ON_CALL(mock_connection_handler_settings_, force_protected_service()) .WillByDefault(ReturnRefOfCopy(protected_services_)); - ON_CALL(mock_connection_handler_settings, force_unprotected_service()) + ON_CALL(mock_connection_handler_settings_, force_unprotected_service()) .WillByDefault(ReturnRefOfCopy(unprotected_services_)); #endif // ENABLE_SECURITY } + // Additional SetUp void AddTestDeviceConnection() { const transport_manager::DeviceInfo device_info( device_handle_, mac_address_, device_name_, connection_type_); // Add Device and connection - ON_CALL(mock_connection_handler_settings, heart_beat_timeout()) + ON_CALL(mock_connection_handler_settings_, heart_beat_timeout()) .WillByDefault(Return(1000u)); connection_handler_->addDeviceConnection(device_info, uid_); connection_key_ = connection_handler_->KeyFromPair(uid_, 0u); // Remove all specific services } + + /** + * @brief Adds test session + * @return session_id + */ void AddTestSession() { protocol_handler_test::MockProtocolHandler temp_protocol_handler; connection_handler_->set_protocol_handler(&temp_protocol_handler); @@ -134,9 +138,11 @@ class ConnectionHandlerTest : public ::testing::Test { connection_handler_->KeyFromPair(uid_, out_context_.new_session_id_); CheckSessionExists(uid_, out_context_.new_session_id_); } + uint32_t SessionHash(const uint32_t connection, const uint32_t session) { return connection_handler_->KeyFromPair(connection, session); } + void AddTestService(ServiceType service_type) { EXPECT_NE(0u, out_context_.new_session_id_); EXPECT_EQ(SessionHash(uid_, out_context_.new_session_id_), @@ -177,7 +183,7 @@ class ConnectionHandlerTest : public ::testing::Test { ASSERT_FALSE(connection_list.empty()); ConnectionList::const_iterator conn_it = connection_list.find(connectionId); ASSERT_NE(conn_it, connection_list.end()); - const Connection& connection = *conn_it->second; + const Connection& connection = *connection_list.begin()->second; const SessionMap& session_map = connection.session_map(); SessionMap::const_iterator sess_it = session_map.find(session_id); @@ -208,7 +214,7 @@ class ConnectionHandlerTest : public ::testing::Test { ASSERT_FALSE(connection_list.empty()); ConnectionList::const_iterator conn_it = connection_list.find(connectionId); ASSERT_NE(conn_it, connection_list.end()); - const Connection& connection = *conn_it->second; + const Connection& connection = *connection_list.begin()->second; const SessionMap& session_map = connection.session_map(); ASSERT_FALSE(session_map.empty()); @@ -311,7 +317,7 @@ class ConnectionHandlerTest : public ::testing::Test { testing::NiceMock<transport_manager_test::MockTransportManager> mock_transport_manager_; testing::NiceMock<MockConnectionHandlerSettings> - mock_connection_handler_settings; + mock_connection_handler_settings_; protocol_handler_test::MockProtocolHandler mock_protocol_handler_; transport_manager::DeviceHandle device_handle_; transport_manager::ConnectionUID uid_; @@ -376,23 +382,23 @@ TEST_F(ConnectionHandlerTest, GetAppIdOnSessionKey) { AddTestSession(); uint32_t app_id = 0; - const uint32_t testid = SessionHash(uid_, out_context_.new_session_id_); + const uint32_t test_id = SessionHash(uid_, out_context_.new_session_id_); - connection_handler::DeviceHandle null_handle = 0; + connection_handler::DeviceHandle* device_id = NULL; EXPECT_EQ(0, connection_handler_->GetDataOnSessionKey( - connection_key_, &app_id, NULL, &null_handle)); - EXPECT_EQ(testid, app_id); + connection_key_, &app_id, NULL, device_id)); + EXPECT_EQ(test_id, app_id); } TEST_F(ConnectionHandlerTest, GetAppIdOnSessionKey_SessionNotStarted) { AddTestDeviceConnection(); uint32_t app_id = 0; - connection_handler::DeviceHandle null_handle = 0; + connection_handler::DeviceHandle* device_id = NULL; EXPECT_EQ(-1, connection_handler_->GetDataOnSessionKey( - connection_key_, &app_id, NULL, &null_handle)); + connection_key_, &app_id, NULL, device_id)); } TEST_F(ConnectionHandlerTest, GetDeviceID) { @@ -508,13 +514,6 @@ TEST_F(ConnectionHandlerTest, IsHeartBeatSupported) { uid_, out_context_.new_session_id_)); } -MATCHER_P(SameDevice, device, "") { - return arg.device_handle() == device.device_handle() && - arg.user_friendly_name() == device.user_friendly_name() && - arg.mac_address() == device.mac_address() && - arg.connection_type() == device.connection_type(); -} - TEST_F(ConnectionHandlerTest, SendEndServiceWithoutSetProtocolHandler) { AddTestDeviceConnection(); AddTestSession(); @@ -1235,8 +1234,8 @@ TEST_F(ConnectionHandlerTest, SessionStop_CheckSpecificHash) { for (uint32_t session = 0; session < 0xFF; ++session) { AddTestSession(); - uint32_t wrong_hash = protocol_handler::HASH_ID_WRONG; - uint32_t hash = protocol_handler::HASH_ID_NOT_SUPPORTED; + uint32_t hash = connection_key_; + uint32_t wrong_hash = hash + 1; const uint32_t end_audio_wrong_hash = connection_handler_->OnSessionEndedCallback( @@ -1474,7 +1473,7 @@ TEST_F(ConnectionHandlerTest, ServiceStarted_Video_Multiple) { TEST_F(ConnectionHandlerTest, SessionStarted_StartSession_SecureSpecific_Unprotect) { - EXPECT_CALL(mock_connection_handler_settings, heart_beat_timeout()) + EXPECT_CALL(mock_connection_handler_settings_, heart_beat_timeout()) .WillOnce(Return(heartbeat_timeout)); // Add virtual device and connection AddTestDeviceConnection(); @@ -1978,41 +1977,6 @@ TEST_F(ConnectionHandlerTest, RunAppOnDevice_AppOnDevice_SUCCESS) { connection_handler_->RunAppOnDevice(hash_of_mac_address, bundle_id); } -TEST_F(ConnectionHandlerTest, OnDeviceConnectionSwitching) { - connection_handler_test::MockConnectionHandlerObserver - mock_connection_handler_observer; - connection_handler_->set_connection_handler_observer( - &mock_connection_handler_observer); - - const transport_manager::DeviceInfo device_info_1( - device_handle_, mac_address_, device_name_, connection_type_); - - connection_handler_->OnDeviceAdded(device_info_1); - - const auto second_mac_address = "second_mac_address"; - const transport_manager::DeviceInfo device_info_2(device_handle_ + 1, - second_mac_address, - "second_device_name", - "second_connection_type"); - - connection_handler_->OnDeviceAdded(device_info_2); - - connection_handler::Device d1(device_info_1.device_handle(), - device_info_1.name(), - device_info_1.mac_address(), - device_info_1.connection_type()); - - connection_handler::Device d2(device_info_2.device_handle(), - device_info_2.name(), - device_info_2.mac_address(), - device_info_2.connection_type()); - - EXPECT_CALL(mock_connection_handler_observer, - OnDeviceSwitchingStart(SameDevice(d1), SameDevice(d2))); - - connection_handler_->OnDeviceSwitchingStart(mac_address_, second_mac_address); -} - } // namespace connection_handler_test } // namespace components } // namespace test diff --git a/src/components/connection_handler/test/heart_beat_monitor_test.cc b/src/components/connection_handler/test/heart_beat_monitor_test.cc index 4c67c97191..69d588a415 100644 --- a/src/components/connection_handler/test/heart_beat_monitor_test.cc +++ b/src/components/connection_handler/test/heart_beat_monitor_test.cc @@ -43,6 +43,7 @@ namespace { const int32_t MILLISECONDS_IN_SECOND = 1000; const int32_t MICROSECONDS_IN_MILLISECONDS = 1000; const int32_t MICROSECONDS_IN_SECOND = 1000 * 1000; +const uint32_t kTime_offset = 20u; } namespace test { @@ -54,24 +55,28 @@ using ::testing::_; class HeartBeatMonitorTest : public testing::Test { public: - HeartBeatMonitorTest() : conn(NULL) { - kTimeout = 5000u; - } + HeartBeatMonitorTest() + : connection_(NULL) + , timeout_(100u) + , time_allowance_multiplier_(1.5) + , expiration_timeout_(timeout_ * 2u) {} protected: - testing::NiceMock<MockConnectionHandler> connection_handler_mock; - connection_handler::Connection* conn; - uint32_t kTimeout; - static const connection_handler::ConnectionHandle kConnectionHandle = + testing::NiceMock<MockConnectionHandler> connection_handler_mock_; + connection_handler::Connection* connection_; + const uint32_t timeout_; + const float_t time_allowance_multiplier_; + const uint32_t expiration_timeout_; + static const connection_handler::ConnectionHandle connection_handle_ = 0xABCDEF; - virtual void SetUp() { - conn = new connection_handler::Connection( - kConnectionHandle, 0, &connection_handler_mock, kTimeout); + void SetUp() OVERRIDE { + connection_ = new connection_handler::Connection( + connection_handle_, 0, &connection_handler_mock_, timeout_); } void TearDown() OVERRIDE { - delete conn; + delete connection_; } }; @@ -81,158 +86,139 @@ ACTION_P2(RemoveSession, conn, session_id) { TEST_F(HeartBeatMonitorTest, TimerNotStarted) { // Whithout StartHeartBeat nothing to be call - EXPECT_CALL(connection_handler_mock, CloseSession(_, _)).Times(0); - EXPECT_CALL(connection_handler_mock, CloseConnection(_)).Times(0); - EXPECT_CALL(connection_handler_mock, SendHeartBeat(_, _)).Times(0); + EXPECT_CALL(connection_handler_mock_, CloseSession(_, _)).Times(0); + EXPECT_CALL(connection_handler_mock_, CloseConnection(_)).Times(0); + EXPECT_CALL(connection_handler_mock_, SendHeartBeat(_, _)).Times(0); - conn->AddNewSession(); + connection_->AddNewSession(); } TEST_F(HeartBeatMonitorTest, TimerNotElapsed) { - EXPECT_CALL(connection_handler_mock, SendHeartBeat(_, _)).Times(0); - EXPECT_CALL(connection_handler_mock, CloseSession(_, _)).Times(0); - EXPECT_CALL(connection_handler_mock, CloseConnection(_)).Times(0); + EXPECT_CALL(connection_handler_mock_, SendHeartBeat(_, _)).Times(0); + EXPECT_CALL(connection_handler_mock_, CloseSession(_, _)).Times(0); + EXPECT_CALL(connection_handler_mock_, CloseConnection(_)).Times(0); - const uint32_t session = conn->AddNewSession(); - conn->StartHeartBeat(session); + const uint32_t session = connection_->AddNewSession(); + connection_->StartHeartBeat(session); } TEST_F(HeartBeatMonitorTest, TimerElapsed) { - const uint32_t session = conn->AddNewSession(); + const uint32_t session = connection_->AddNewSession(); TestAsyncWaiter waiter; uint32_t times = 0; - EXPECT_CALL(connection_handler_mock, CloseSession(_, session, _)) - .WillOnce( - DoAll(NotifyTestAsyncWaiter(&waiter), RemoveSession(conn, session))); - times++; - EXPECT_CALL(connection_handler_mock, CloseConnection(_)) - .WillOnce(NotifyTestAsyncWaiter(&waiter)); + EXPECT_CALL(connection_handler_mock_, CloseSession(_, session, _)) + .WillOnce(DoAll(NotifyTestAsyncWaiter(&waiter), + RemoveSession(connection_, session))); times++; - EXPECT_CALL(connection_handler_mock, SendHeartBeat(_, session)) + EXPECT_CALL(connection_handler_mock_, SendHeartBeat(_, session)) .WillOnce(NotifyTestAsyncWaiter(&waiter)); times++; - conn->StartHeartBeat(session); + connection_->StartHeartBeat(session); EXPECT_TRUE(waiter.WaitFor( times, - 2 * kTimeout * MICROSECONDS_IN_MILLISECONDS + MICROSECONDS_IN_SECOND)); + 2 * timeout_ * MICROSECONDS_IN_MILLISECONDS + MICROSECONDS_IN_SECOND)); } TEST_F(HeartBeatMonitorTest, KeptAlive) { - EXPECT_CALL(connection_handler_mock, CloseSession(_, _)).Times(0); - EXPECT_CALL(connection_handler_mock, CloseConnection(_)).Times(0); - EXPECT_CALL(connection_handler_mock, SendHeartBeat(_, _)).Times(0); - - const uint32_t session = conn->AddNewSession(); - conn->StartHeartBeat(session); - usleep(kTimeout * MICROSECONDS_IN_MILLISECONDS - MICROSECONDS_IN_SECOND); - conn->KeepAlive(session); - usleep(kTimeout * MICROSECONDS_IN_MILLISECONDS - MICROSECONDS_IN_SECOND); - conn->KeepAlive(session); - usleep(kTimeout * MICROSECONDS_IN_MILLISECONDS - MICROSECONDS_IN_SECOND); - conn->KeepAlive(session); - usleep(kTimeout * MICROSECONDS_IN_MILLISECONDS - MICROSECONDS_IN_SECOND); + EXPECT_CALL(connection_handler_mock_, CloseSession(_, _)).Times(0); + EXPECT_CALL(connection_handler_mock_, CloseConnection(_)).Times(0); + EXPECT_CALL(connection_handler_mock_, SendHeartBeat(_, _)).Times(0); + + const uint32_t session = connection_->AddNewSession(); + connection_->StartHeartBeat(session); + usleep(timeout_); + connection_->KeepAlive(session); + usleep(timeout_); + connection_->KeepAlive(session); } TEST_F(HeartBeatMonitorTest, NotKeptAlive) { - const uint32_t session = conn->AddNewSession(); + const uint32_t session = connection_->AddNewSession(); TestAsyncWaiter waiter; uint32_t times = 0; - EXPECT_CALL(connection_handler_mock, SendHeartBeat(_, session)) + EXPECT_CALL(connection_handler_mock_, SendHeartBeat(_, session)) .WillOnce(NotifyTestAsyncWaiter(&waiter)); times++; - EXPECT_CALL(connection_handler_mock, CloseSession(_, session, _)) - .WillOnce( - DoAll(NotifyTestAsyncWaiter(&waiter), RemoveSession(conn, session))); - times++; - EXPECT_CALL(connection_handler_mock, CloseConnection(_)) - .WillOnce(NotifyTestAsyncWaiter(&waiter)); + EXPECT_CALL(connection_handler_mock_, CloseSession(_, session, _)) + .WillOnce(DoAll(NotifyTestAsyncWaiter(&waiter), + RemoveSession(connection_, session))); times++; - conn->StartHeartBeat(session); - usleep(kTimeout * MICROSECONDS_IN_MILLISECONDS - MICROSECONDS_IN_SECOND); - conn->KeepAlive(session); - usleep(kTimeout * MICROSECONDS_IN_MILLISECONDS - MICROSECONDS_IN_SECOND); - conn->KeepAlive(session); - usleep(kTimeout * MICROSECONDS_IN_MILLISECONDS - MICROSECONDS_IN_SECOND); - conn->KeepAlive(session); - usleep(2 * kTimeout * MICROSECONDS_IN_MILLISECONDS + MICROSECONDS_IN_SECOND); + connection_->StartHeartBeat(session); + usleep(timeout_); + connection_->KeepAlive(session); EXPECT_TRUE(waiter.WaitFor( times, - 2 * kTimeout * MICROSECONDS_IN_MILLISECONDS + MICROSECONDS_IN_SECOND)); + 2 * timeout_ * MICROSECONDS_IN_MILLISECONDS + MICROSECONDS_IN_SECOND)); } TEST_F(HeartBeatMonitorTest, TwoSessionsElapsed) { - const uint32_t kSession1 = conn->AddNewSession(); - const uint32_t kSession2 = conn->AddNewSession(); + const uint32_t session_1 = connection_->AddNewSession(); + const uint32_t session_2 = connection_->AddNewSession(); TestAsyncWaiter waiter; uint32_t times = 0; - EXPECT_CALL(connection_handler_mock, CloseSession(_, kSession1, _)) + EXPECT_CALL(connection_handler_mock_, CloseSession(_, session_1, _)) .WillOnce(DoAll(NotifyTestAsyncWaiter(&waiter), - RemoveSession(conn, kSession1))); + RemoveSession(connection_, session_1))); times++; - EXPECT_CALL(connection_handler_mock, CloseSession(_, kSession2, _)) + EXPECT_CALL(connection_handler_mock_, CloseSession(_, session_2, _)) .WillOnce(DoAll(NotifyTestAsyncWaiter(&waiter), - RemoveSession(conn, kSession2))); + RemoveSession(connection_, session_2))); times++; - EXPECT_CALL(connection_handler_mock, CloseConnection(_)) - .WillOnce(NotifyTestAsyncWaiter(&waiter)); - times++; - EXPECT_CALL(connection_handler_mock, SendHeartBeat(_, kSession1)) + + EXPECT_CALL(connection_handler_mock_, SendHeartBeat(_, session_1)) .WillOnce(NotifyTestAsyncWaiter(&waiter)); times++; - EXPECT_CALL(connection_handler_mock, SendHeartBeat(_, kSession2)) + EXPECT_CALL(connection_handler_mock_, SendHeartBeat(_, session_2)) .WillOnce(NotifyTestAsyncWaiter(&waiter)); times++; - conn->StartHeartBeat(kSession1); - conn->StartHeartBeat(kSession2); + connection_->StartHeartBeat(session_1); + connection_->StartHeartBeat(session_2); EXPECT_TRUE(waiter.WaitFor( times, - 2 * kTimeout * MICROSECONDS_IN_MILLISECONDS + MICROSECONDS_IN_SECOND)); + 2 * timeout_ * MICROSECONDS_IN_MILLISECONDS + MICROSECONDS_IN_SECOND)); } TEST_F(HeartBeatMonitorTest, IncreaseHeartBeatTimeout) { - const uint32_t kSession = conn->AddNewSession(); + const uint32_t session = connection_->AddNewSession(); - EXPECT_CALL(connection_handler_mock, CloseSession(_, _)).Times(0); - EXPECT_CALL(connection_handler_mock, CloseConnection(_)).Times(0); - EXPECT_CALL(connection_handler_mock, SendHeartBeat(_, _)).Times(0); + EXPECT_CALL(connection_handler_mock_, CloseSession(_, _)).Times(0); + EXPECT_CALL(connection_handler_mock_, CloseConnection(_)).Times(0); + EXPECT_CALL(connection_handler_mock_, SendHeartBeat(_, _)).Times(0); - const uint32_t kNewTimeout = kTimeout + MICROSECONDS_IN_MILLISECONDS; - conn->StartHeartBeat(kSession); - conn->SetHeartBeatTimeout(kNewTimeout, kSession); + const uint32_t kNewTimeout = timeout_ + MICROSECONDS_IN_MILLISECONDS; + connection_->StartHeartBeat(session); + connection_->SetHeartBeatTimeout(kNewTimeout, session); } TEST_F(HeartBeatMonitorTest, DecreaseHeartBeatTimeout) { - const uint32_t kSession = conn->AddNewSession(); + const uint32_t session = connection_->AddNewSession(); TestAsyncWaiter waiter; uint32_t times = 0; - EXPECT_CALL(connection_handler_mock, CloseSession(_, kSession, _)) - .WillOnce( - DoAll(NotifyTestAsyncWaiter(&waiter), RemoveSession(conn, kSession))); - times++; - EXPECT_CALL(connection_handler_mock, CloseConnection(_)) - .WillOnce(NotifyTestAsyncWaiter(&waiter)); + EXPECT_CALL(connection_handler_mock_, CloseSession(_, session, _)) + .WillOnce(DoAll(NotifyTestAsyncWaiter(&waiter), + RemoveSession(connection_, session))); times++; - EXPECT_CALL(connection_handler_mock, SendHeartBeat(_, kSession)) + EXPECT_CALL(connection_handler_mock_, SendHeartBeat(_, session)) .WillOnce(NotifyTestAsyncWaiter(&waiter)); times++; - const uint32_t kNewTimeout = kTimeout - MICROSECONDS_IN_MILLISECONDS; - conn->StartHeartBeat(kSession); - conn->SetHeartBeatTimeout(kNewTimeout, kSession); + const uint32_t new_timeout = timeout_ - kTime_offset; + connection_->StartHeartBeat(session); + connection_->SetHeartBeatTimeout(new_timeout, session); EXPECT_TRUE(waiter.WaitFor( times, - 2 * kTimeout * MICROSECONDS_IN_MILLISECONDS + MICROSECONDS_IN_SECOND)); + 2 * timeout_ * MICROSECONDS_IN_MILLISECONDS + MICROSECONDS_IN_SECOND)); } } // namespace connection_handler_test |