diff options
author | Ivo Stoilov (GitHub) <istoilov@luxoft.com> | 2017-01-24 15:40:04 +0200 |
---|---|---|
committer | Ivo Stoilov (GitHub) <istoilov@luxoft.com> | 2017-02-06 12:29:52 +0200 |
commit | dccf53d96d1dd6520b410a5ef88f7367f807e4d3 (patch) | |
tree | 9ba233a38f136abc70a8df33aa6c428d9a27f34a /src/components/security_manager | |
parent | 9ec62a5faa39d449ea4b2703a3fd94f355613c24 (diff) | |
download | sdl_core-dccf53d96d1dd6520b410a5ef88f7367f807e4d3.tar.gz |
Refactor SecurityManager test to use TestAsyncWaiter
Related tasks APPLINK-30588 APPLINK-30977
Diffstat (limited to 'src/components/security_manager')
-rw-r--r-- | src/components/security_manager/test/crypto_manager_impl_test.cc | 1 | ||||
-rw-r--r-- | src/components/security_manager/test/security_manager_test.cc | 153 |
2 files changed, 115 insertions, 39 deletions
diff --git a/src/components/security_manager/test/crypto_manager_impl_test.cc b/src/components/security_manager/test/crypto_manager_impl_test.cc index 27f91866ac..7fef33f1df 100644 --- a/src/components/security_manager/test/crypto_manager_impl_test.cc +++ b/src/components/security_manager/test/crypto_manager_impl_test.cc @@ -91,7 +91,6 @@ class CryptoManagerTest : public testing::Test { void TearDown() OVERRIDE { delete mock_security_manager_settings_; - testing::Mock::AsyncVerifyAndClearExpectations(1000); } void InitSecurityManager() { diff --git a/src/components/security_manager/test/security_manager_test.cc b/src/components/security_manager/test/security_manager_test.cc index b5f9ae2e78..ecb0b8d98e 100644 --- a/src/components/security_manager/test/security_manager_test.cc +++ b/src/components/security_manager/test/security_manager_test.cc @@ -44,6 +44,7 @@ #include "security_manager/mock_crypto_manager.h" #include "security_manager/mock_security_manager_listener.h" #include "utils/make_shared.h" +#include "utils/test_async_waiter.h" namespace test { namespace components { @@ -96,10 +97,6 @@ class SecurityManagerTest : public ::testing::Test { security_manager_->set_protocol_handler(&mock_protocol_handler); security_manager_->AddListener(&mock_sm_listener); } - void TearDown() OVERRIDE { - // Wait call methods in thread - testing::Mock::AsyncVerifyAndClearExpectations(1000); - } void SetMockCryptoManager() { EXPECT_CALL(mock_crypto_manager, IsCertificateUpdateRequired()) @@ -268,7 +265,8 @@ TEST_F(SecurityManagerTest, SecurityManager_NULLCryptoManager) { // Expect InternalError with ERROR_ID uint32_t connection_id = 0; uint8_t session_id = 0; - // uint8_t protocol_version = 0; + + TestAsyncWaiter waiter; EXPECT_CALL(mock_session_observer, PairFromKey(key, _, _)); EXPECT_CALL(mock_session_observer, ProtocolVersionUsed(connection_id, session_id, _)) @@ -277,12 +275,14 @@ TEST_F(SecurityManagerTest, SecurityManager_NULLCryptoManager) { EXPECT_CALL(mock_protocol_handler, SendMessageToMobileApp( InternalErrorWithErrId(SecurityManager::ERROR_NOT_SUPPORTED), - is_final)); + is_final)).WillOnce(NotifyTestAsyncWaiter(&waiter)); const SecurityQuery::QueryHeader header(SecurityQuery::REQUEST, // It could be any query id SecurityQuery::INVALID_QUERY_ID); const uint8_t data = 0; EmulateMobileMessage(header, &data, 1); + + EXPECT_TRUE(waiter.WaitFor(1, 1000)); } /* * Shall skip all OnMobileMessageSent @@ -354,21 +354,30 @@ TEST_F(SecurityManagerTest, GetInvalidQueryId) { SetMockCryptoManager(); uint32_t connection_id = 0; uint8_t session_id = 0; - // uint8_t protocol_version = 0; - EXPECT_CALL(mock_session_observer, PairFromKey(key, _, _)); + + TestAsyncWaiter waiter; + uint32_t times = 0; + EXPECT_CALL(mock_session_observer, PairFromKey(key, _, _)) + .WillOnce(NotifyTestAsyncWaiter(&waiter)); + times++; EXPECT_CALL(mock_session_observer, ProtocolVersionUsed(connection_id, session_id, _)) - .WillOnce(Return(true)); + .WillOnce(DoAll(NotifyTestAsyncWaiter(&waiter), Return(true))); + times++; + // Expect InternalError with ERROR_ID EXPECT_CALL( mock_protocol_handler, SendMessageToMobileApp( InternalErrorWithErrId(SecurityManager::ERROR_INVALID_QUERY_ID), - is_final)); + is_final)).WillOnce(NotifyTestAsyncWaiter(&waiter)); + times++; const SecurityQuery::QueryHeader header(SecurityQuery::REQUEST, SecurityQuery::INVALID_QUERY_ID); const uint8_t data = 0; EmulateMobileMessage(header, &data, 1); + + EXPECT_TRUE(waiter.WaitFor(times, 1000)); } /* * Shall send Internall Error on call @@ -608,7 +617,8 @@ TEST_F(SecurityManagerTest, ProccessHandshakeData_WrongDataSize) { SetMockCryptoManager(); uint32_t connection_id = 0; uint8_t session_id = 0; - // uint8_t protocol_version = 0; + + TestAsyncWaiter waiter; EXPECT_CALL(mock_session_observer, PairFromKey(key, _, _)); EXPECT_CALL(mock_session_observer, ProtocolVersionUsed(connection_id, session_id, _)) @@ -619,8 +629,11 @@ TEST_F(SecurityManagerTest, ProccessHandshakeData_WrongDataSize) { mock_protocol_handler, SendMessageToMobileApp( InternalErrorWithErrId(SecurityManager::ERROR_INVALID_QUERY_SIZE), - is_final)); + is_final)).WillOnce(NotifyTestAsyncWaiter(&waiter)); + EmulateMobileMessageHandshake(NULL, 0); + + EXPECT_TRUE(waiter.WaitFor(1, 1000)); } /* * Shall send InternallError on @@ -632,28 +645,38 @@ TEST_F(SecurityManagerTest, ProccessHandshakeData_ServiceNotProtected) { // Expect InternalError with ERROR_ID uint32_t connection_id = 0; uint8_t session_id = 0; - // uint8_t protocol_version = 0; - EXPECT_CALL(mock_session_observer, PairFromKey(key, _, _)); + + TestAsyncWaiter waiter; + uint32_t times = 0; + EXPECT_CALL(mock_session_observer, PairFromKey(key, _, _)) + .WillOnce(NotifyTestAsyncWaiter(&waiter)); + times++; EXPECT_CALL(mock_session_observer, ProtocolVersionUsed(connection_id, session_id, _)) - .WillOnce(Return(true)); - + .WillOnce(DoAll(NotifyTestAsyncWaiter(&waiter), Return(true))); + times++; EXPECT_CALL( mock_protocol_handler, SendMessageToMobileApp( InternalErrorWithErrId(SecurityManager::ERROR_SERVICE_NOT_PROTECTED), - is_final)); + is_final)).WillOnce(NotifyTestAsyncWaiter(&waiter)); + times++; + // Expect notifying listeners (unsuccess) EXPECT_CALL(mock_sm_listener, OnHandshakeDone(key, SSLContext::Handshake_Result_Fail)) - .WillOnce(Return(true)); + .WillOnce(DoAll(NotifyTestAsyncWaiter(&waiter), Return(true))); + times++; // Emulate SessionObserver result EXPECT_CALL(mock_session_observer, GetSSLContext(key, kControl)) - .WillOnce(ReturnNull()); + .WillOnce(DoAll(NotifyTestAsyncWaiter(&waiter), ReturnNull())); + times++; const uint8_t data[] = {0x1, 0x2}; EmulateMobileMessageHandshake(data, sizeof(data) / sizeof(data[0])); + + EXPECT_TRUE(waiter.WaitFor(times, 1000)); } /* * Shall send InternallError on getting @@ -668,29 +691,39 @@ TEST_F(SecurityManagerTest, ProccessHandshakeData_InvalidData) { uint32_t connection_id = 0; uint8_t session_id = 0; - // uint8_t protocol_version = 0; + + TestAsyncWaiter waiter; + uint32_t times = 0; EXPECT_CALL(mock_session_observer, PairFromKey(key, _, _)) - .Times(handshake_emulates); + .Times(handshake_emulates) + .WillRepeatedly(NotifyTestAsyncWaiter(&waiter)); + times += handshake_emulates; EXPECT_CALL(mock_session_observer, ProtocolVersionUsed(connection_id, session_id, _)) .Times(handshake_emulates) - .WillRepeatedly(Return(true)); + .WillRepeatedly(DoAll(NotifyTestAsyncWaiter(&waiter), Return(true))); + times += handshake_emulates; // Expect InternalError with ERROR_ID EXPECT_CALL( mock_protocol_handler, SendMessageToMobileApp( InternalErrorWithErrId(SecurityManager::ERROR_SSL_INVALID_DATA), - is_final)).Times(handshake_emulates); + is_final)) + .Times(handshake_emulates) + .WillRepeatedly(NotifyTestAsyncWaiter(&waiter)); + times += handshake_emulates; // Expect notifying listeners (unsuccess) EXPECT_CALL(mock_sm_listener, OnHandshakeDone(key, SSLContext::Handshake_Result_Fail)) - .WillOnce(Return(true)); + .WillOnce(DoAll(NotifyTestAsyncWaiter(&waiter), Return(true))); + times++; // Emulate SessionObserver and CryptoManager result EXPECT_CALL(mock_session_observer, GetSSLContext(key, kControl)) .Times(handshake_emulates) .WillRepeatedly(Return(&mock_ssl_context_exists)); + // Emulate DoHandshakeStep fail logics EXPECT_CALL( mock_ssl_context_exists, @@ -700,16 +733,21 @@ TEST_F(SecurityManagerTest, ProccessHandshakeData_InvalidData) { _)) .WillOnce(DoAll(SetArgPointee<2>(handshake_data_out_pointer), SetArgPointee<3>(handshake_data_out_size), + NotifyTestAsyncWaiter(&waiter), Return(SSLContext::Handshake_Result_AbnormalFail))) .WillOnce(DoAll(SetArgPointee<2>((uint8_t*)NULL), SetArgPointee<3>(handshake_data_out_size), + NotifyTestAsyncWaiter(&waiter), Return(SSLContext::Handshake_Result_AbnormalFail))) .WillOnce(DoAll(SetArgPointee<2>(handshake_data_out_pointer), SetArgPointee<3>(0), + NotifyTestAsyncWaiter(&waiter), Return(SSLContext::Handshake_Result_AbnormalFail))) .WillOnce(DoAll(SetArgPointee<2>((uint8_t*)NULL), SetArgPointee<3>(0), + NotifyTestAsyncWaiter(&waiter), Return(SSLContext::Handshake_Result_AbnormalFail))); + times += 4; // matches to each single call above // On each wrong handshake will be asked error EXPECT_CALL(mock_ssl_context_exists, LastError()).Times(handshake_emulates); @@ -717,6 +755,8 @@ TEST_F(SecurityManagerTest, ProccessHandshakeData_InvalidData) { // Emulate handshare #handshake_emulates times for 5 cases EmulateMobileMessageHandshake( handshake_data, handshake_data_size, handshake_emulates); + + EXPECT_TRUE(waiter.WaitFor(times, 1000)); } /* * Shall send HandshakeData on getting SEND_HANDSHAKE_DATA from mobile side @@ -729,31 +769,44 @@ TEST_F(SecurityManagerTest, ProccessHandshakeData_Answer) { uint32_t connection_id = 0; uint8_t session_id = 0; - // uint8_t protocol_version = 0; + + TestAsyncWaiter waiter; + uint32_t times = 0; EXPECT_CALL(mock_session_observer, PairFromKey(key, _, _)) - .Times(handshake_emulates); + .Times(handshake_emulates) + .WillRepeatedly(NotifyTestAsyncWaiter(&waiter)); + times += handshake_emulates; EXPECT_CALL(mock_session_observer, ProtocolVersionUsed(connection_id, session_id, _)) .Times(handshake_emulates) - .WillRepeatedly(Return(true)); + .WillRepeatedly(DoAll(NotifyTestAsyncWaiter(&waiter), Return(true))); + times += handshake_emulates; // Get size of raw message after const size_t raw_message_size = 15; - EXPECT_CALL(mock_protocol_handler, - SendMessageToMobileApp(RawMessageEqSize(raw_message_size), - is_final)).Times(handshake_emulates); + EXPECT_CALL( + mock_protocol_handler, + SendMessageToMobileApp(RawMessageEqSize(raw_message_size), is_final)) + .Times(handshake_emulates) + .WillRepeatedly(NotifyTestAsyncWaiter(&waiter)); + times += handshake_emulates; + // Expect notifying listeners (unsuccess) EXPECT_CALL(mock_sm_listener, OnHandshakeDone(key, SSLContext::Handshake_Result_Fail)) - .WillOnce(Return(true)); + .WillOnce(DoAll(NotifyTestAsyncWaiter(&waiter), Return(true))); + times++; // Emulate SessionObserver and CryptoManager result EXPECT_CALL(mock_ssl_context_exists, IsInitCompleted()) .Times(handshake_emulates) - .WillRepeatedly(Return(false)); + .WillRepeatedly(DoAll(NotifyTestAsyncWaiter(&waiter), Return(false))); + times += handshake_emulates; EXPECT_CALL(mock_session_observer, GetSSLContext(key, kControl)) .Times(handshake_emulates) - .WillRepeatedly(Return(&mock_ssl_context_exists)); + .WillRepeatedly(DoAll(NotifyTestAsyncWaiter(&waiter), + Return(&mock_ssl_context_exists))); + times += handshake_emulates; // Emulate DoHandshakeStep correct logics EXPECT_CALL( @@ -764,13 +817,18 @@ TEST_F(SecurityManagerTest, ProccessHandshakeData_Answer) { _)) .WillOnce(DoAll(SetArgPointee<2>(handshake_data_out_pointer), SetArgPointee<3>(handshake_data_out_size), + NotifyTestAsyncWaiter(&waiter), Return(SSLContext::Handshake_Result_Success))) .WillOnce(DoAll(SetArgPointee<2>(handshake_data_out_pointer), SetArgPointee<3>(handshake_data_out_size), + NotifyTestAsyncWaiter(&waiter), Return(SSLContext::Handshake_Result_Fail))); + times += 2; // matches to each single call above EmulateMobileMessageHandshake( handshake_data, handshake_data_size, handshake_emulates); + + EXPECT_TRUE(waiter.WaitFor(times, 1000)); } /* * Shall call all listeners on success end handshake @@ -781,19 +839,26 @@ TEST_F(SecurityManagerTest, ProccessHandshakeData_HandshakeFinished) { SetMockCryptoManager(); // Count handshake calls const int handshake_emulates = 6; + + TestAsyncWaiter waiter; + uint32_t times = 0; // Expect no errors // Expect notifying listeners (success) EXPECT_CALL(mock_sm_listener, OnHandshakeDone(key, SSLContext::Handshake_Result_Success)) - .WillOnce(Return(true)); + .WillOnce(DoAll(NotifyTestAsyncWaiter(&waiter), Return(true))); + times++; // Emulate SessionObserver and CryptoManager result EXPECT_CALL(mock_session_observer, GetSSLContext(key, kControl)) .Times(handshake_emulates) - .WillRepeatedly(Return(&mock_ssl_context_exists)); + .WillRepeatedly(DoAll(NotifyTestAsyncWaiter(&waiter), + Return(&mock_ssl_context_exists))); + times += handshake_emulates; EXPECT_CALL(mock_ssl_context_exists, IsInitCompleted()) .Times(handshake_emulates) - .WillRepeatedly(Return(true)); + .WillRepeatedly(DoAll(NotifyTestAsyncWaiter(&waiter), Return(true))); + times += handshake_emulates; EXPECT_CALL( mock_ssl_context_exists, @@ -805,26 +870,33 @@ TEST_F(SecurityManagerTest, ProccessHandshakeData_HandshakeFinished) { // two states with correct out data WillOnce(DoAll(SetArgPointee<2>(handshake_data_out_pointer), SetArgPointee<3>(handshake_data_out_size), + NotifyTestAsyncWaiter(&waiter), Return(SSLContext::Handshake_Result_Success))) .WillOnce(DoAll(SetArgPointee<2>(handshake_data_out_pointer), SetArgPointee<3>(handshake_data_out_size), + NotifyTestAsyncWaiter(&waiter), Return(SSLContext::Handshake_Result_Fail))) . // two states with with null pointer data WillOnce(DoAll(SetArgPointee<2>((uint8_t*)NULL), SetArgPointee<3>(handshake_data_out_size), + NotifyTestAsyncWaiter(&waiter), Return(SSLContext::Handshake_Result_Success))) .WillOnce(DoAll(SetArgPointee<2>((uint8_t*)NULL), SetArgPointee<3>(handshake_data_out_size), + NotifyTestAsyncWaiter(&waiter), Return(SSLContext::Handshake_Result_Fail))) . // two states with with null data size WillOnce(DoAll(SetArgPointee<2>(handshake_data_out_pointer), SetArgPointee<3>(0), + NotifyTestAsyncWaiter(&waiter), Return(SSLContext::Handshake_Result_Success))) .WillOnce(DoAll(SetArgPointee<2>(handshake_data_out_pointer), SetArgPointee<3>(0), + NotifyTestAsyncWaiter(&waiter), Return(SSLContext::Handshake_Result_Success))); + times += 6; // matches to each single call above // Expect send two message (with correct pointer and size data) @@ -835,14 +907,19 @@ TEST_F(SecurityManagerTest, ProccessHandshakeData_HandshakeFinished) { EXPECT_CALL(mock_session_observer, ProtocolVersionUsed(connection_id, session_id, _)) .Times(2) - .WillRepeatedly(Return(true)); + .WillRepeatedly(DoAll(NotifyTestAsyncWaiter(&waiter), Return(true))); + times += 2; // matches to the number above EXPECT_CALL(mock_protocol_handler, SendMessageToMobileApp(_, is_final)) - .Times(2); + .Times(2) + .WillRepeatedly(NotifyTestAsyncWaiter(&waiter)); + times += 2; // matches to the number above // Expect NO InternalError with ERROR_ID EmulateMobileMessageHandshake( handshake_data, handshake_data_size, handshake_emulates); + + EXPECT_TRUE(waiter.WaitFor(times, 1000)); } /* * Shall not any query on getting empty SEND_INTERNAL_ERROR |