diff options
Diffstat (limited to 'src/components/protocol_handler/test/protocol_handler_tm_test.cc')
-rw-r--r-- | src/components/protocol_handler/test/protocol_handler_tm_test.cc | 342 |
1 files changed, 195 insertions, 147 deletions
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 d7850e4df1..77de1705da 100644 --- a/src/components/protocol_handler/test/protocol_handler_tm_test.cc +++ b/src/components/protocol_handler/test/protocol_handler_tm_test.cc @@ -41,8 +41,10 @@ #include "protocol_handler/mock_protocol_handler_settings.h" #include "protocol_handler/mock_session_observer.h" #include "connection_handler/mock_connection_handler.h" +#ifdef ENABLE_SECURITY #include "security_manager/mock_security_manager.h" #include "security_manager/mock_ssl_context.h" +#endif // ENABLE_SECURITY #include "transport_manager/mock_transport_manager.h" #include "utils/make_shared.h" #include "utils/test_async_waiter.h" @@ -105,6 +107,7 @@ using ::testing::An; using ::testing::AnyOf; using ::testing::ByRef; using ::testing::DoAll; +using ::testing::SaveArg; using ::testing::Eq; using ::testing::_; using ::testing::Invoke; @@ -114,12 +117,14 @@ using ::testing::SetArgPointee; typedef std::vector<uint8_t> UCharDataVector; // custom action to call a member function with 6 arguments -ACTION_P8(InvokeMemberFuncWithArg6, ptr, memberFunc, a, b, c, d, e, f) { - (ptr->*memberFunc)(a, b, c, d, e, f); +ACTION_P4(InvokeMemberFuncWithArg2, ptr, memberFunc, a, b) { + (ptr->*memberFunc)(a, b); } namespace { const uint32_t kAsyncExpectationsTimeout = 10000u; +const uint32_t kMicrosecondsInMillisecond = 1000u; +const uint32_t kAddSessionWaitTimeMs = 100u; } class ProtocolHandlerImplTest : public ::testing::Test { @@ -145,6 +150,13 @@ class ProtocolHandlerImplTest : public ::testing::Test { .WillByDefault(Return(malformd_max_messages)); ON_CALL(protocol_handler_settings_mock, multiframe_waiting_timeout()) .WillByDefault(Return(multiframe_waiting_timeout)); +#ifdef ENABLE_SECURITY + ON_CALL(protocol_handler_settings_mock, force_protected_service()) + .WillByDefault(ReturnRefOfCopy(force_protected_services)); + ON_CALL(protocol_handler_settings_mock, force_unprotected_service()) + .WillByDefault(ReturnRefOfCopy(force_unprotected_services)); +#endif + protocol_handler_impl.reset( new ProtocolHandlerImpl(protocol_handler_settings_mock, session_observer_mock, @@ -189,6 +201,21 @@ class ProtocolHandlerImplTest : public ::testing::Test { connection_id); } + protocol_handler::SessionContext GetSessionContext( + const transport_manager::ConnectionUID connection_id, + const uint8_t initial_session_id, + const uint8_t new_session_id, + const protocol_handler::ServiceType service_type, + const uint32_t hash_id, + const bool protection_flag) { + return protocol_handler::SessionContext(connection_id, + initial_session_id, + new_session_id, + service_type, + hash_id, + protection_flag); + } + void AddSession(const ::utils::SharedPtr<TestAsyncWaiter>& waiter, uint32_t& times) { using namespace protocol_handler; @@ -206,6 +233,14 @@ class ProtocolHandlerImplTest : public ::testing::Test { const bool callback_protection_flag = PROTECTION_OFF; #endif // ENABLE_SECURITY + const protocol_handler::SessionContext context = + GetSessionContext(connection_id, + NEW_SESSION_ID, + session_id, + start_service, + HASH_ID_WRONG, + callback_protection_flag); + // Expect ConnectionHandler check EXPECT_CALL(session_observer_mock, OnSessionStartedCallback(connection_id, @@ -215,16 +250,12 @@ class ProtocolHandlerImplTest : public ::testing::Test { An<const BsonObject*>())) . // Return sessions start success - WillOnce(DoAll(NotifyTestAsyncWaiter(waiter), - InvokeMemberFuncWithArg6( - protocol_handler_impl.get(), - &ProtocolHandler::NotifySessionStartedResult, - connection_id, - NEW_SESSION_ID, - session_id, - HASH_ID_WRONG, - callback_protection_flag, - ByRef(empty_rejected_param_)))); + WillOnce(DoAll( + NotifyTestAsyncWaiter(waiter), + InvokeMemberFuncWithArg2(protocol_handler_impl.get(), + &ProtocolHandler::NotifySessionStarted, + context, + ByRef(empty_rejected_param_)))); times++; // Expect send Ack with PROTECTION_OFF (on no Security Manager) @@ -236,6 +267,8 @@ class ProtocolHandlerImplTest : public ::testing::Test { SendControlMessage( PROTECTION_ON, start_service, NEW_SESSION_ID, FRAME_DATA_START_SERVICE); + + usleep(kAddSessionWaitTimeMs * kMicrosecondsInMillisecond); } #ifdef ENABLE_SECURITY @@ -316,6 +349,8 @@ class ProtocolHandlerImplTest : public ::testing::Test { testing::NiceMock<security_manager_test::MockSecurityManager> security_manager_mock; testing::NiceMock<security_manager_test::MockSSLContext> ssl_context_mock; + std::vector<int> force_protected_services; + std::vector<int> force_unprotected_services; #endif // ENABLE_SECURITY std::vector<std::string> empty_rejected_param_; }; @@ -371,6 +406,7 @@ TEST_F(ProtocolHandlerImplTest, TestAsyncWaiter waiter; uint32_t times = 0; + ServiceType service_type; // Expect ConnectionHandler check EXPECT_CALL( session_observer_mock, @@ -382,16 +418,18 @@ TEST_F(ProtocolHandlerImplTest, .Times(call_times) . // Return sessions start rejection - WillRepeatedly(DoAll( - NotifyTestAsyncWaiter(&waiter), - InvokeMemberFuncWithArg6(protocol_handler_impl.get(), - &ProtocolHandler::NotifySessionStartedResult, - connection_id, - NEW_SESSION_ID, - SESSION_START_REJECT, - HASH_ID_WRONG, - PROTECTION_OFF, - ByRef(empty_rejected_param_)))); + WillRepeatedly( + DoAll(NotifyTestAsyncWaiter(&waiter), + SaveArg<2>(&service_type), + InvokeMemberFuncWithArg2(protocol_handler_impl.get(), + &ProtocolHandler::NotifySessionStarted, + GetSessionContext(connection_id, + NEW_SESSION_ID, + SESSION_START_REJECT, + service_type, + HASH_ID_WRONG, + PROTECTION_OFF), + ByRef(empty_rejected_param_)))); times += call_times; // Expect send NAck @@ -436,6 +474,7 @@ TEST_F(ProtocolHandlerImplTest, StartSession_Protected_SessionObserverReject) { TestAsyncWaiter waiter; uint32_t times = 0; + ServiceType service_type; // Expect ConnectionHandler check EXPECT_CALL( session_observer_mock, @@ -449,13 +488,15 @@ TEST_F(ProtocolHandlerImplTest, StartSession_Protected_SessionObserverReject) { // Return sessions start rejection WillRepeatedly(DoAll( NotifyTestAsyncWaiter(&waiter), - InvokeMemberFuncWithArg6(protocol_handler_impl.get(), - &ProtocolHandler::NotifySessionStartedResult, - connection_id, - NEW_SESSION_ID, - SESSION_START_REJECT, - HASH_ID_WRONG, - callback_protection_flag, + SaveArg<2>(&service_type), + InvokeMemberFuncWithArg2(protocol_handler_impl.get(), + &ProtocolHandler::NotifySessionStarted, + GetSessionContext(connection_id, + NEW_SESSION_ID, + SESSION_START_REJECT, + service_type, + HASH_ID_WRONG, + callback_protection_flag), ByRef(empty_rejected_param_)))); times += call_times; @@ -501,16 +542,17 @@ TEST_F(ProtocolHandlerImplTest, An<const BsonObject*>())) . // Return sessions start success - WillOnce(DoAll( - NotifyTestAsyncWaiter(&waiter), - InvokeMemberFuncWithArg6(protocol_handler_impl.get(), - &ProtocolHandler::NotifySessionStartedResult, - connection_id, - NEW_SESSION_ID, - session_id, - HASH_ID_WRONG, - PROTECTION_OFF, - ByRef(empty_rejected_param_)))); + WillOnce( + DoAll(NotifyTestAsyncWaiter(&waiter), + InvokeMemberFuncWithArg2(protocol_handler_impl.get(), + &ProtocolHandler::NotifySessionStarted, + GetSessionContext(connection_id, + NEW_SESSION_ID, + session_id, + start_service, + HASH_ID_WRONG, + PROTECTION_OFF), + ByRef(empty_rejected_param_)))); times++; SetProtocolVersion2(); @@ -614,7 +656,7 @@ TEST_F(ProtocolHandlerImplTest, start_service, PROTECTION_OFF, An<const BsonObject*>())) - // don't call NotifySessionStartedResult() immediately, instead call it + // don't call NotifySessionStartedContext() immediately, instead call it // after second OnSessionStartedCallback() .WillOnce(NotifyTestAsyncWaiter(&waiter)); times++; @@ -640,21 +682,23 @@ TEST_F(ProtocolHandlerImplTest, An<const BsonObject*>())) .WillOnce(DoAll( NotifyTestAsyncWaiter(&waiter), - InvokeMemberFuncWithArg6(protocol_handler_impl.get(), - &ProtocolHandler::NotifySessionStartedResult, - connection_id2, - session_id2, - SESSION_START_REJECT, - HASH_ID_WRONG, - PROTECTION_OFF, + InvokeMemberFuncWithArg2(protocol_handler_impl.get(), + &ProtocolHandler::NotifySessionStarted, + GetSessionContext(connection_id2, + session_id2, + SESSION_START_REJECT, + start_service, + HASH_ID_WRONG, + PROTECTION_OFF), ByRef(rejected_param_list)), - InvokeMemberFuncWithArg6(protocol_handler_impl.get(), - &ProtocolHandler::NotifySessionStartedResult, - connection_id1, - session_id1, - generated_session_id1, - HASH_ID_WRONG, - PROTECTION_OFF, + InvokeMemberFuncWithArg2(protocol_handler_impl.get(), + &ProtocolHandler::NotifySessionStarted, + GetSessionContext(connection_id1, + session_id1, + generated_session_id1, + start_service, + HASH_ID_WRONG, + PROTECTION_OFF), ByRef(empty_rejected_param_)))); times++; @@ -802,10 +846,6 @@ TEST_F(ProtocolHandlerImplTest, EndSession_Success) { } #ifdef ENABLE_SECURITY -/* - * ProtocolHandler shall not call Security logics with Protocol version 1 - * Check session_observer with PROTECTION_OFF and Ack with PROTECTION_OFF - */ TEST_F(ProtocolHandlerImplTest, SecurityEnable_StartSessionProtocoloV1) { using namespace protocol_handler; ::utils::SharedPtr<TestAsyncWaiter> waiter = @@ -826,16 +866,17 @@ TEST_F(ProtocolHandlerImplTest, SecurityEnable_StartSessionProtocoloV1) { An<const BsonObject*>())) . // Return sessions start success - WillOnce(DoAll( - NotifyTestAsyncWaiter(waiter), - InvokeMemberFuncWithArg6(protocol_handler_impl.get(), - &ProtocolHandler::NotifySessionStartedResult, - connection_id, - NEW_SESSION_ID, - session_id, - HASH_ID_WRONG, - PROTECTION_OFF, - ByRef(empty_rejected_param_)))); + WillOnce( + DoAll(NotifyTestAsyncWaiter(waiter), + InvokeMemberFuncWithArg2(protocol_handler_impl.get(), + &ProtocolHandler::NotifySessionStarted, + GetSessionContext(connection_id, + NEW_SESSION_ID, + session_id, + start_service, + HASH_ID_WRONG, + PROTECTION_OFF), + ByRef(empty_rejected_param_)))); times++; SetProtocolVersion2(); @@ -881,16 +922,17 @@ TEST_F(ProtocolHandlerImplTest, SecurityEnable_StartSessionUnprotected) { An<const BsonObject*>())) . // Return sessions start success - WillOnce(DoAll( - NotifyTestAsyncWaiter(&waiter), - InvokeMemberFuncWithArg6(protocol_handler_impl.get(), - &ProtocolHandler::NotifySessionStartedResult, - connection_id, - NEW_SESSION_ID, - session_id, - HASH_ID_WRONG, - PROTECTION_OFF, - ByRef(empty_rejected_param_)))); + WillOnce( + DoAll(NotifyTestAsyncWaiter(&waiter), + InvokeMemberFuncWithArg2(protocol_handler_impl.get(), + &ProtocolHandler::NotifySessionStarted, + GetSessionContext(connection_id, + NEW_SESSION_ID, + session_id, + start_service, + HASH_ID_WRONG, + PROTECTION_OFF), + ByRef(empty_rejected_param_)))); times++; SetProtocolVersion2(); @@ -917,6 +959,15 @@ TEST_F(ProtocolHandlerImplTest, SecurityEnable_StartSessionProtected_Fail) { TestAsyncWaiter waiter; uint32_t times = 0; + + protocol_handler::SessionContext context = GetSessionContext(connection_id, + NEW_SESSION_ID, + session_id, + start_service, + HASH_ID_WRONG, + PROTECTION_ON); + context.is_new_service_ = true; + // Expect ConnectionHandler check EXPECT_CALL(session_observer_mock, OnSessionStartedCallback(connection_id, @@ -926,16 +977,12 @@ TEST_F(ProtocolHandlerImplTest, SecurityEnable_StartSessionProtected_Fail) { An<const BsonObject*>())) . // Return sessions start success - WillOnce(DoAll( - NotifyTestAsyncWaiter(&waiter), - InvokeMemberFuncWithArg6(protocol_handler_impl.get(), - &ProtocolHandler::NotifySessionStartedResult, - connection_id, - NEW_SESSION_ID, - session_id, - HASH_ID_WRONG, - PROTECTION_ON, - ByRef(empty_rejected_param_)))); + WillOnce( + DoAll(NotifyTestAsyncWaiter(&waiter), + InvokeMemberFuncWithArg2(protocol_handler_impl.get(), + &ProtocolHandler::NotifySessionStarted, + context, + ByRef(empty_rejected_param_)))); times++; SetProtocolVersion2(); @@ -980,16 +1027,17 @@ TEST_F(ProtocolHandlerImplTest, An<const BsonObject*>())) . // Return sessions start success - WillOnce(DoAll( - NotifyTestAsyncWaiter(&waiter), - InvokeMemberFuncWithArg6(protocol_handler_impl.get(), - &ProtocolHandler::NotifySessionStartedResult, - connection_id, - NEW_SESSION_ID, - session_id, - HASH_ID_WRONG, - PROTECTION_ON, - ByRef(empty_rejected_param_)))); + WillOnce( + DoAll(NotifyTestAsyncWaiter(&waiter), + InvokeMemberFuncWithArg2(protocol_handler_impl.get(), + &ProtocolHandler::NotifySessionStarted, + GetSessionContext(connection_id, + NEW_SESSION_ID, + session_id, + start_service, + HASH_ID_WRONG, + PROTECTION_ON), + ByRef(empty_rejected_param_)))); times++; SetProtocolVersion2(); @@ -1038,6 +1086,14 @@ TEST_F(ProtocolHandlerImplTest, TestAsyncWaiter waiter; uint32_t times = 0; + protocol_handler::SessionContext context = GetSessionContext(connection_id, + NEW_SESSION_ID, + session_id, + start_service, + HASH_ID_WRONG, + PROTECTION_ON); + context.is_new_service_ = true; + // Expect ConnectionHandler check EXPECT_CALL(session_observer_mock, OnSessionStartedCallback(connection_id, @@ -1047,24 +1103,20 @@ TEST_F(ProtocolHandlerImplTest, An<const BsonObject*>())) . // Return sessions start success - WillOnce(DoAll( - NotifyTestAsyncWaiter(&waiter), - InvokeMemberFuncWithArg6(protocol_handler_impl.get(), - &ProtocolHandler::NotifySessionStartedResult, - connection_id, - NEW_SESSION_ID, - session_id, - HASH_ID_WRONG, - PROTECTION_ON, - ByRef(empty_rejected_param_)))); + WillOnce( + DoAll(NotifyTestAsyncWaiter(&waiter), + InvokeMemberFuncWithArg2(protocol_handler_impl.get(), + &ProtocolHandler::NotifySessionStarted, + context, + ByRef(empty_rejected_param_)))); times++; std::vector<int> services; // TODO(AKutsan) : APPLINK-21398 use named constants instead of magic numbers services.push_back(0x0A); services.push_back(0x0B); - ON_CALL(protocol_handler_settings_mock, force_protected_service()) - .WillByDefault(ReturnRefOfCopy(services)); + EXPECT_CALL(protocol_handler_settings_mock, force_protected_service()) + .WillOnce(ReturnRefOfCopy(services)); // call new SSLContext creation EXPECT_CALL(security_manager_mock, CreateSSLContext(connection_key)) @@ -1093,13 +1145,6 @@ TEST_F(ProtocolHandlerImplTest, connection_key, security_manager::SSLContext::Handshake_Result_Fail))); - // Listener check SSLContext - EXPECT_CALL(session_observer_mock, - GetSSLContext(connection_key, start_service)) - . - // Emulate protection for service is not enabled - WillOnce(ReturnNull()); - // Expect send Ack with PROTECTION_OFF (on fail handshake) EXPECT_CALL(transport_manager_mock, SendMessageToDevice( @@ -1139,16 +1184,17 @@ TEST_F(ProtocolHandlerImplTest, An<const BsonObject*>())) . // Return sessions start success - WillOnce(DoAll( - NotifyTestAsyncWaiter(&waiter), - InvokeMemberFuncWithArg6(protocol_handler_impl.get(), - &ProtocolHandler::NotifySessionStartedResult, - connection_id, - NEW_SESSION_ID, - session_id, - HASH_ID_WRONG, - PROTECTION_ON, - ByRef(empty_rejected_param_)))); + WillOnce( + DoAll(NotifyTestAsyncWaiter(&waiter), + InvokeMemberFuncWithArg2(protocol_handler_impl.get(), + &ProtocolHandler::NotifySessionStarted, + GetSessionContext(connection_id, + NEW_SESSION_ID, + session_id, + start_service, + HASH_ID_WRONG, + PROTECTION_ON), + ByRef(empty_rejected_param_)))); times++; // call new SSLContext creation @@ -1236,16 +1282,17 @@ TEST_F( An<const BsonObject*>())) . // Return sessions start success - WillOnce(DoAll( - NotifyTestAsyncWaiter(&waiter), - InvokeMemberFuncWithArg6(protocol_handler_impl.get(), - &ProtocolHandler::NotifySessionStartedResult, - connection_id, - NEW_SESSION_ID, - session_id, - HASH_ID_WRONG, - PROTECTION_ON, - ByRef(empty_rejected_param_)))); + WillOnce( + DoAll(NotifyTestAsyncWaiter(&waiter), + InvokeMemberFuncWithArg2(protocol_handler_impl.get(), + &ProtocolHandler::NotifySessionStarted, + GetSessionContext(connection_id, + NEW_SESSION_ID, + session_id, + start_service, + HASH_ID_WRONG, + PROTECTION_ON), + ByRef(empty_rejected_param_)))); times++; // call new SSLContext creation @@ -1331,16 +1378,17 @@ TEST_F(ProtocolHandlerImplTest, An<const BsonObject*>())) . // Return sessions start success - WillOnce(DoAll( - NotifyTestAsyncWaiter(&waiter), - InvokeMemberFuncWithArg6(protocol_handler_impl.get(), - &ProtocolHandler::NotifySessionStartedResult, - connection_id, - NEW_SESSION_ID, - session_id, - HASH_ID_WRONG, - PROTECTION_ON, - ByRef(empty_rejected_param_)))); + WillOnce( + DoAll(NotifyTestAsyncWaiter(&waiter), + InvokeMemberFuncWithArg2(protocol_handler_impl.get(), + &ProtocolHandler::NotifySessionStarted, + GetSessionContext(connection_id, + NEW_SESSION_ID, + session_id, + start_service, + HASH_ID_WRONG, + PROTECTION_ON), + ByRef(empty_rejected_param_)))); times++; // call new SSLContext creation |