diff options
author | Kozoriz <kozorizandriy@gmail.com> | 2016-02-29 10:27:38 +0200 |
---|---|---|
committer | Kozoriz <kozorizandriy@gmail.com> | 2016-03-04 10:11:18 +0200 |
commit | b14ac4915707bb39a24f4de8a27e497b21aa7032 (patch) | |
tree | 338c93094ea1e151d3bdd389d0676b7367548ce3 /src/components/connection_handler | |
parent | 7df5429476716d9fdba7da838e14ab92d0ce1126 (diff) | |
download | sdl_core-b14ac4915707bb39a24f4de8a27e497b21aa7032.tar.gz |
Remove ConnectionHandler singleton
Correctives in src and tests for ConnectionHandler class
Related to : APPLINK-21440
Diffstat (limited to 'src/components/connection_handler')
10 files changed, 290 insertions, 324 deletions
diff --git a/src/components/connection_handler/CMakeLists.txt b/src/components/connection_handler/CMakeLists.txt index 657c90d108..dc1b2d24d6 100644 --- a/src/components/connection_handler/CMakeLists.txt +++ b/src/components/connection_handler/CMakeLists.txt @@ -34,7 +34,6 @@ set(CH_SRC_DIR ${COMPONENTS_DIR}/connection_handler/src) include_directories ( include ${COMPONENTS_DIR}/protocol_handler/include/ - ${COMPONENTS_DIR}/config_profile/include/ ${COMPONENTS_DIR}/utils/include/ ${ENCRYPTION_INCLUDE_DIRECTORY} ${LOG4CXX_INCLUDE_DIRECTORY} @@ -48,7 +47,6 @@ set (SOURCES ) set(LIBRARIES - ConfigProfile ProtocolLibrary ) @@ -57,4 +55,4 @@ target_link_libraries(connectionHandler encryption) if(BUILD_TESTS) add_subdirectory(test) -endif()
\ No newline at end of file +endif() diff --git a/src/components/connection_handler/include/connection_handler/connection.h b/src/components/connection_handler/include/connection_handler/connection.h index fcb966ffc1..4c98aa79fb 100644 --- a/src/components/connection_handler/include/connection_handler/connection.h +++ b/src/components/connection_handler/include/connection_handler/connection.h @@ -297,6 +297,7 @@ class Connection { * @brief monitor that closes connection if there is no traffic over it */ HeartBeatMonitor* heartbeat_monitor_; + uint32_t heartbeat_timeout_; threads::Thread *heart_beat_monitor_thread_; DISALLOW_COPY_AND_ASSIGN(Connection); diff --git a/src/components/connection_handler/include/connection_handler/connection_handler.h b/src/components/connection_handler/include/connection_handler/connection_handler.h index ea79f76ae8..7aeb06bf54 100644 --- a/src/components/connection_handler/include/connection_handler/connection_handler.h +++ b/src/components/connection_handler/include/connection_handler/connection_handler.h @@ -33,6 +33,7 @@ #ifndef SRC_COMPONENTS_CONNECTION_HANDLER_INCLUDE_CONNECTION_HANDLER_CONNECTION_HANDLER_H_ #define SRC_COMPONENTS_CONNECTION_HANDLER_INCLUDE_CONNECTION_HANDLER_CONNECTION_HANDLER_H_ +#include "connection_handler/connection_handler_settings.h" #include "transport_manager/transport_manager_listener.h" #include "protocol_handler/session_observer.h" #include "connection_handler/device.h" @@ -45,14 +46,9 @@ */ namespace connection_handler { - enum CloseSessionReason { - kCommon = 0, - kFlood, - kMalformed, - kUnauthorizedApp - }; +enum CloseSessionReason { kCommon = 0, kFlood, kMalformed, kUnauthorizedApp }; - class ConnectionHandlerObserver; +class ConnectionHandlerObserver; /** * \class ConnectionHandler @@ -65,14 +61,7 @@ class ConnectionHandler { * \param observer Pointer to observer object. **/ virtual void set_connection_handler_observer( - ConnectionHandlerObserver *observer) = 0; - - /** - * \brief Sets pointer to TransportManager. - * \param transportManager Pointer to TransportManager object. - **/ - virtual void set_transport_manager( - transport_manager::TransportManager *transport_manager) = 0; + ConnectionHandlerObserver* observer) = 0; virtual void StartTransportManager() = 0; @@ -88,7 +77,8 @@ class ConnectionHandler { virtual void CloseRevokedConnection(uint32_t connection_key) = 0; /** - * \brief Close all associated sessions and close the connection pointed by handle + * \brief Close all associated sessions and close the connection pointed by + * handle */ virtual void CloseConnection(ConnectionHandle connection_handle) = 0; @@ -103,8 +93,8 @@ class ConnectionHandler { * @param mac_address * @return true if successfully */ - virtual bool GetDeviceID(const std::string &mac_address, - DeviceHandle *device_handle) = 0; + virtual bool GetDeviceID(const std::string& mac_address, + DeviceHandle* device_handle) = 0; /** * Close session associated with the key @@ -118,13 +108,13 @@ class ConnectionHandler { uint8_t session_id, CloseSessionReason close_reason) = 0; - /** - * @brief SendEndService allows to end up specific service. - * - * @param key application identifier whose service should be closed. - * - * @param service_type the service that should be closed. - */ + /** + * @brief SendEndService allows to end up specific service. + * + * @param key application identifier whose service should be closed. + * + * @param service_type the service that should be closed. + */ virtual void SendEndService(uint32_t key, uint8_t service_type) = 0; /** @@ -138,7 +128,7 @@ class ConnectionHandler { * \brief Send heartbeat to mobile app */ virtual void SendHeartBeat(ConnectionHandle connection_handle, - uint8_t session_id) = 0; + uint8_t session_id) = 0; /** * Sets heart beat timeout for specified session @@ -149,6 +139,13 @@ class ConnectionHandler { uint32_t timeout) = 0; /** + * \brief Keep connection associated with the key from being closed by + * heartbeat monitor + */ + virtual void KeepConnectionAlive(uint32_t connection_key, + uint8_t session_id) = 0; + + /** * \brief binds protocol version with session * * \param connection_key pair of connection and session id @@ -166,16 +163,33 @@ class ConnectionHandler { * \param device_id Returned: DeviceID * \return int32_t -1 in case of error or 0 in case of success */ - virtual int32_t GetDataOnSessionKey(uint32_t key, uint32_t* app_id, + virtual int32_t GetDataOnSessionKey(uint32_t key, + uint32_t* app_id, std::list<int32_t>* sessions_list, - uint32_t* device_id) = 0; + uint32_t* device_id) const = 0; + /** + * @brief GetConnectedDevicesMAC allows to obtain MAC adresses for all + * currently connected devices. + * + * @param device_macs collection of MAC adresses for connected devices. + */ + virtual void GetConnectedDevicesMAC(std::vector<std::string> &device_macs) const = 0; + + /** + * \brief Connection handler settings getter + * \return pointer to connection handler settings class + */ + virtual const ConnectionHandlerSettings& get_settings() const = 0; + + virtual const protocol_handler::SessionObserver& get_session_observer() = 0; + + virtual DevicesDiscoveryStarter& get_device_discovery_starter() = 0; protected: /** * \brief Destructor */ - virtual ~ConnectionHandler() { - } + virtual ~ConnectionHandler() {} }; } // namespace connection_handler diff --git a/src/components/connection_handler/include/connection_handler/connection_handler_impl.h b/src/components/connection_handler/include/connection_handler/connection_handler_impl.h index a4a74a4bf5..71d51e48b1 100644 --- a/src/components/connection_handler/include/connection_handler/connection_handler_impl.h +++ b/src/components/connection_handler/include/connection_handler/connection_handler_impl.h @@ -38,13 +38,6 @@ #include <string> #include <vector> -#include "utils/logger.h" -#include "utils/macro.h" -#include "utils/lock.h" -#include "utils/stl_utils.h" -#include "utils/singleton.h" -#include "utils/rwlock.h" - #include "transport_manager/transport_manager_listener_empty.h" #include "protocol_handler/session_observer.h" #include "protocol_handler/protocol_handler.h" @@ -54,6 +47,12 @@ #include "connection_handler/devices_discovery_starter.h" #include "connection_handler/connection_handler.h" +#include "utils/logger.h" +#include "utils/macro.h" +#include "utils/lock.h" +#include "utils/stl_utils.h" +#include "utils/rwlock.h" + /** * \namespace connection_handler * \brief SmartDeviceLink connection_handler namespace. @@ -69,28 +68,26 @@ namespace connection_handler { class ConnectionHandlerImpl : public ConnectionHandler, public transport_manager::TransportManagerListenerEmpty, public protocol_handler::SessionObserver, - public DevicesDiscoveryStarter, - public utils::Singleton<ConnectionHandlerImpl> { + public DevicesDiscoveryStarter { public: + + /** + * @brief ConnectionHandlerImpl + */ + ConnectionHandlerImpl(const ConnectionHandlerSettings& settings, + transport_manager::TransportManager& tm); /** * \brief Destructor */ - virtual ~ConnectionHandlerImpl(); + ~ConnectionHandlerImpl(); void Stop(); /** * \brief Sets observer pointer for connection_handler. * \param observer Pointer to observer object. */ - virtual void set_connection_handler_observer( - ConnectionHandlerObserver *observer); - - /** - * \brief Sets pointer to TransportManager. - * \param transport_mngr Pointer to TransportManager object. - **/ - virtual void set_transport_manager( - transport_manager::TransportManager *transport_mngr); + void set_connection_handler_observer( + ConnectionHandlerObserver *observer) OVERRIDE; /** * \brief Sets pointer to ProtocolHandler. @@ -103,16 +100,16 @@ class ConnectionHandlerImpl : public ConnectionHandler, * \brief Connects to all services of device * \param deviceHandle Handle of device to connect to */ - virtual void ConnectToDevice(connection_handler::DeviceHandle device_handle); + void ConnectToDevice(connection_handler::DeviceHandle device_handle) OVERRIDE; - virtual void ConnectToAllDevices(); + void ConnectToAllDevices() OVERRIDE; - virtual void StartTransportManager(); + void StartTransportManager() OVERRIDE; - virtual void OnDeviceListUpdated( - const std::vector<transport_manager::DeviceInfo> &); + void OnDeviceListUpdated( + const std::vector<transport_manager::DeviceInfo> &) OVERRIDE; - virtual void OnFindNewApplicationsRequest(); + void OnFindNewApplicationsRequest() OVERRIDE; /** * \brief Available devices list updated. @@ -122,44 +119,43 @@ class ConnectionHandlerImpl : public ConnectionHandler, * * \param DeviceList New list of available devices. **/ - virtual void OnDeviceFound(const transport_manager::DeviceInfo &device_info); - virtual void OnDeviceAdded(const transport_manager::DeviceInfo &device_info); - virtual void OnDeviceRemoved(const transport_manager::DeviceInfo &device_info); + void OnDeviceFound(const transport_manager::DeviceInfo &device_info) OVERRIDE; + void OnDeviceAdded(const transport_manager::DeviceInfo &device_info) OVERRIDE; + void OnDeviceRemoved(const transport_manager::DeviceInfo &device_info) OVERRIDE; - virtual void OnScanDevicesFinished(); - virtual void OnScanDevicesFailed( - const transport_manager::SearchDeviceError &error); + void OnScanDevicesFinished() OVERRIDE; + void OnScanDevicesFailed( + const transport_manager::SearchDeviceError &error) OVERRIDE; /** * \brief Notifies about established connection. * * \param connection_id ID of new connection. **/ - virtual void OnConnectionEstablished( - const transport_manager::DeviceInfo &device_info, - const transport_manager::ConnectionUID &connection_id); - virtual void OnConnectionFailed( + void OnConnectionEstablished(const transport_manager::DeviceInfo &device_info, + const transport_manager::ConnectionUID connection_id) OVERRIDE; + void OnConnectionFailed( const transport_manager::DeviceInfo &device_info, - const transport_manager::ConnectError &error); - virtual void OnConnectionClosed( - transport_manager::ConnectionUID connection_id); - virtual void OnConnectionClosedFailure( + const transport_manager::ConnectError &error) OVERRIDE; + void OnConnectionClosed( + transport_manager::ConnectionUID connection_id) OVERRIDE; + void OnConnectionClosedFailure( transport_manager::ConnectionUID connection_id, - const transport_manager::DisconnectError &error); - virtual void OnUnexpectedDisconnect( + const transport_manager::DisconnectError &error) OVERRIDE; + void OnUnexpectedDisconnect( transport_manager::ConnectionUID connection_id, - const transport_manager::CommunicationError &error); - virtual void OnDeviceConnectionLost( + const transport_manager::CommunicationError &error) OVERRIDE; + void OnDeviceConnectionLost( const connection_handler::DeviceHandle &device, - const transport_manager::DisconnectDeviceError &error); + const transport_manager::DisconnectDeviceError &error) OVERRIDE; /** * \brief Informs about failure during DisconnectDevice procedure of TM * \param device Information about disconnected device * \param error Information about possible reason of loosing connection */ - virtual void OnDisconnectFailed( + void OnDisconnectFailed( const connection_handler::DeviceHandle &device, - const transport_manager::DisconnectDeviceError &error); + const transport_manager::DisconnectDeviceError &error) OVERRIDE; /** * \brief Callback function used by ProtocolHandler @@ -171,7 +167,7 @@ class ConnectionHandlerImpl : public ConnectionHandler, * \param hash_id pointer for session hash identifier * \return uint32_t Id (number) of new session if successful, otherwise 0. */ - virtual uint32_t OnSessionStartedCallback(const transport_manager::ConnectionUID &connection_handle, + virtual uint32_t OnSessionStartedCallback(const transport_manager::ConnectionUID connection_handle, const uint8_t session_id, const protocol_handler::ServiceType &service_type, const bool is_protected, uint32_t* hash_id); @@ -185,10 +181,10 @@ class ConnectionHandlerImpl : public ConnectionHandler, * If not equal to hash assigned to session on start then operation fails. * \return uint32_t 0 if operation fails, session key otherwise */ - virtual uint32_t OnSessionEndedCallback( - const transport_manager::ConnectionUID &connection_handle, + uint32_t OnSessionEndedCallback( + const transport_manager::ConnectionUID connection_handle, const uint8_t session_id, const uint32_t &hashCode, - const protocol_handler::ServiceType &service_type); + const protocol_handler::ServiceType &service_type) OVERRIDE; /** * \brief Callback function used by ProtocolHandler @@ -212,9 +208,9 @@ class ConnectionHandlerImpl : public ConnectionHandler, * \param sessionId Identifier of the session * \return int32_t Unique key for session */ - virtual uint32_t KeyFromPair( + uint32_t KeyFromPair( transport_manager::ConnectionUID connection_handle, - uint8_t session_id); + uint8_t session_id) const OVERRIDE; /** * \brief Returns connection identifier and session number from given session key @@ -222,9 +218,9 @@ class ConnectionHandlerImpl : public ConnectionHandler, * \param connection_handle Returned: Connection identifier within which session exists * \param sessionId Returned: Number of session */ - virtual void PairFromKey(uint32_t key, + void PairFromKey(uint32_t key, transport_manager::ConnectionUID *connection_handle, - uint8_t *session_id); + uint8_t *session_id) const OVERRIDE; /** * \brief information about device @@ -235,11 +231,11 @@ class ConnectionHandlerImpl : public ConnectionHandler, * \param connection_type Returned: type of connection (USB, BT, etc.) * \return int32_t -1 in case of error or 0 in case of success */ - virtual int32_t GetDataOnDeviceID(connection_handler::DeviceHandle device_handle, + int32_t GetDataOnDeviceID(connection_handler::DeviceHandle device_handle, std::string *device_name = NULL, std::list<uint32_t> *applications_list = NULL, std::string *mac_address = NULL, - std::string* connection_type = NULL); + std::string* connection_type = NULL) const OVERRIDE; /** * @brief GetConnectedDevicesMAC allows to obtain MAC adresses for all @@ -247,7 +243,7 @@ class ConnectionHandlerImpl : public ConnectionHandler, * * @param device_macs collection of MAC adresses for connected devices. */ - void GetConnectedDevicesMAC(std::vector<std::string> &device_macs) const; + void GetConnectedDevicesMAC(std::vector<std::string> &device_macs) const OVERRIDE; #ifdef ENABLE_SECURITY /** @@ -290,40 +286,40 @@ class ConnectionHandlerImpl : public ConnectionHandler, * \param device_handle * \return true on sucess otherwise false. */ - virtual bool GetDeviceID(const std::string &mac_address, - DeviceHandle *device_handle); + bool GetDeviceID(const std::string &mac_address, + DeviceHandle *device_handle) OVERRIDE; /** * \brief Method which should start devices discovering */ - virtual void StartDevicesDiscovery(); + void StartDevicesDiscovery() OVERRIDE; /** * @brief Close the connection revoked by Policy * @param connection_key pair of connection and session id */ - virtual void CloseRevokedConnection(uint32_t connection_key); + void CloseRevokedConnection(uint32_t connection_key) OVERRIDE; /** * @brief Close the connection pointed by handle * @param connection_handle Connection unique id */ - virtual void CloseConnection(ConnectionHandle connection_handle) OVERRIDE; + void CloseConnection(ConnectionHandle connection_handle) OVERRIDE; /** * \brief Close session associated with the key * \param key Unique key used by other components as session identifier */ - virtual void CloseSession(uint32_t key, CloseSessionReason close_reason); + void CloseSession(uint32_t key, CloseSessionReason close_reason) OVERRIDE; /** * \brief Function used by HearbeatMonitior to close session on HB timeout * \param connection_handle Connection handler within which session exists * \param session_id Identifier of the session to be ended */ - virtual void CloseSession(ConnectionHandle connection_handle, + void CloseSession(ConnectionHandle connection_handle, uint8_t session_id, - CloseSessionReason close_reason); + CloseSessionReason close_reason) OVERRIDE; /** * \brief Function used by OnApplicationFloodCallback and @@ -339,13 +335,13 @@ class ConnectionHandlerImpl : public ConnectionHandler, * \brief Return count of session for specified connection * \param connection_key pair of connection handle and session id */ - virtual uint32_t GetConnectionSessionsCount(uint32_t connection_key); + uint32_t GetConnectionSessionsCount(uint32_t connection_key) OVERRIDE; /** * \brief Send heartbeat message to mobile app */ - virtual void SendHeartBeat(ConnectionHandle connection_handle, - uint8_t session_id); + void SendHeartBeat(ConnectionHandle connection_handle, + uint8_t session_id) OVERRIDE; /** * @brief SendEndService allows to end up specific service. @@ -354,22 +350,22 @@ class ConnectionHandlerImpl : public ConnectionHandler, * * @param service_type the service that should be closed. */ - virtual void SendEndService(uint32_t key, - uint8_t service_type); + void SendEndService(uint32_t key, + uint8_t service_type) OVERRIDE; /** * \brief Start heartbeat for specified session * * \param connection_key pair of connection and session id */ - virtual void StartSessionHeartBeat(uint32_t connection_key); + void StartSessionHeartBeat(uint32_t connection_key) OVERRIDE; /** * Sets heart beat timeout for specified session * @param connection_key pair of connection and session id * @param timeout in milliseconds */ - virtual void SetHeartBeatTimeout(uint32_t connection_key, uint32_t timeout); + void SetHeartBeatTimeout(uint32_t connection_key, uint32_t timeout) OVERRIDE; /** * \brief Keep connection associated with the key from being closed by heartbeat monitor @@ -383,8 +379,8 @@ class ConnectionHandlerImpl : public ConnectionHandler, * \param protocol_version contains protocol version of * \registered application. */ - virtual void BindProtocolVersionWithSession(uint32_t connection_key, - uint8_t protocol_version); + void BindProtocolVersionWithSession(uint32_t connection_key, + uint8_t protocol_version) OVERRIDE; /** * \brief returns TRUE if session supports sending HEART BEAT ACK to mobile side @@ -392,9 +388,9 @@ class ConnectionHandlerImpl : public ConnectionHandler, * \param sessionId Identifier of the session * \return TRUE if session has protocol version which supports heartbeat otherwise returns FALSE */ - virtual bool IsHeartBeatSupported( + bool IsHeartBeatSupported( transport_manager::ConnectionUID connection_handle, - uint8_t session_id); + uint8_t session_id) const OVERRIDE; /** * @brief returns protocol version which application supports @@ -403,19 +399,20 @@ class ConnectionHandlerImpl : public ConnectionHandler, * @param method writes value protocol version to protocol_version * @return TRUE if session and connection exist otherwise returns FALSE */ - virtual bool ProtocolVersionUsed(uint32_t connection_id, - uint8_t session_id, uint8_t& protocol_version); + bool ProtocolVersionUsed(uint32_t connection_id, + uint8_t session_id, uint8_t& protocol_version) const OVERRIDE; - virtual int32_t GetDataOnSessionKey(uint32_t key, uint32_t* app_id, - std::list<int32_t>* sessions_list, - uint32_t* device_id); + int32_t GetDataOnSessionKey(uint32_t key, + uint32_t* app_id, + std::list<int32_t>* sessions_list, + uint32_t* device_id) const OVERRIDE; - private: - /** - * \brief Default class constructor - */ - ConnectionHandlerImpl(); + const ConnectionHandlerSettings& get_settings() const OVERRIDE; + + const protocol_handler::SessionObserver& get_session_observer(); + DevicesDiscoveryStarter& get_device_discovery_starter(); + private: /** * \brief Disconnect application. * @@ -425,8 +422,9 @@ class ConnectionHandlerImpl : public ConnectionHandler, void RemoveConnection(const ConnectionHandle connection_handle); void OnConnectionEnded( - const transport_manager::ConnectionUID &connection_id); + const transport_manager::ConnectionUID connection_id); + const ConnectionHandlerSettings& settings_; /** * \brief Pointer to observer */ @@ -435,7 +433,7 @@ class ConnectionHandlerImpl : public ConnectionHandler, /** * \brief Pointer to TransportManager */ - transport_manager::TransportManager *transport_manager_; + transport_manager::TransportManager& transport_manager_; protocol_handler::ProtocolHandler *protocol_handler_; @@ -467,10 +465,9 @@ class ConnectionHandlerImpl : public ConnectionHandler, ConnectionList &getConnectionList(); void addDeviceConnection( const transport_manager::DeviceInfo &device_info, - const transport_manager::ConnectionUID &connection_id); + const transport_manager::ConnectionUID connection_id); #endif private: - FRIEND_BASE_SINGLETON_CLASS(ConnectionHandlerImpl); DISALLOW_COPY_AND_ASSIGN(ConnectionHandlerImpl); }; } // namespace connection_handler diff --git a/src/components/connection_handler/include/connection_handler/heartbeat_monitor.h b/src/components/connection_handler/include/connection_handler/heartbeat_monitor.h index edc4a5b174..c3e02a4715 100644 --- a/src/components/connection_handler/include/connection_handler/heartbeat_monitor.h +++ b/src/components/connection_handler/include/connection_handler/heartbeat_monitor.h @@ -83,7 +83,7 @@ class HeartBeatMonitor: public threads::ThreadDelegate { private: - // \brief Heartbeat timeout, should be read from profile + // \brief Heartbeat timeout uint32_t default_heartbeat_timeout_; // \brief Connection that must be closed when timeout elapsed Connection *connection_; diff --git a/src/components/connection_handler/src/connection.cc b/src/components/connection_handler/src/connection.cc index cb948392a3..bc340e5a7b 100644 --- a/src/components/connection_handler/src/connection.cc +++ b/src/components/connection_handler/src/connection.cc @@ -37,7 +37,6 @@ #include "connection_handler/connection.h" #include "connection_handler/connection_handler.h" #include "protocol_handler/protocol_packet.h" -#include "config_profile/profile.h" #include "utils/logger.h" #include "utils/macro.h" @@ -347,7 +346,7 @@ bool Connection::SupportHeartBeat(uint8_t session_id) { return ((::protocol_handler::PROTOCOL_VERSION_3 == session.protocol_version || ::protocol_handler::PROTOCOL_VERSION_4 == session.protocol_version) && - (profile::Profile::instance()->heart_beat_timeout())); + (0 != heartbeat_timeout_)); } bool Connection::ProtocolVersion(uint8_t session_id, uint8_t& protocol_version) { diff --git a/src/components/connection_handler/src/connection_handler_impl.cc b/src/components/connection_handler/src/connection_handler_impl.cc index 89910df6e7..261a8d232c 100644 --- a/src/components/connection_handler/src/connection_handler_impl.cc +++ b/src/components/connection_handler/src/connection_handler_impl.cc @@ -38,18 +38,11 @@ #include "connection_handler/connection_handler_impl.h" #include "transport_manager/info.h" -#include "config_profile/profile.h" #ifdef ENABLE_SECURITY #include "security_manager/security_manager.h" #endif // ENABLE_SECURITY -namespace { -uint32_t HeartBeatTimeout() { - return profile::Profile::instance()->heart_beat_timeout(); -} -} // namespace - /** * \namespace connection_handler * \brief SmartDeviceLink ConnectionHandler namespace. @@ -67,12 +60,15 @@ transport_manager::ConnectionUID ConnectionUIDFromHandle( return transport_manager::ConnectionUID(handle); } -ConnectionHandlerImpl::ConnectionHandlerImpl() - : connection_handler_observer_(NULL), - transport_manager_(NULL), - protocol_handler_(NULL), - connection_handler_observer_lock_(true), - connection_list_deleter_(&connection_list_) { +ConnectionHandlerImpl::ConnectionHandlerImpl( + const ConnectionHandlerSettings& settings, + transport_manager::TransportManager& tm) + : settings_(settings) + , connection_handler_observer_(NULL) + , transport_manager_(tm) + , protocol_handler_(NULL) + , connection_handler_observer_lock_(true) + , connection_list_deleter_(&connection_list_) { } ConnectionHandlerImpl::~ConnectionHandlerImpl() { @@ -99,17 +95,6 @@ void ConnectionHandlerImpl::set_connection_handler_observer( connection_handler_observer_ = observer; } -void ConnectionHandlerImpl::set_transport_manager( - transport_manager::TransportManager *transport_mngr) { - LOG4CXX_DEBUG(logger_, "ConnectionHandlerImpl::set_transport_manager() " - << transport_mngr); - if (!transport_mngr) { - LOG4CXX_ERROR(logger_, "Null pointer to TransportManager."); - return; - } - transport_manager_ = transport_mngr; -} - void ConnectionHandlerImpl::set_protocol_handler( protocol_handler::ProtocolHandler *protocol_handler) { LOG4CXX_DEBUG(logger_, "ConnectionHandlerImpl::set_protocol_handler()" @@ -194,7 +179,7 @@ void ConnectionHandlerImpl::OnScanDevicesFailed( void ConnectionHandlerImpl::OnConnectionEstablished( const transport_manager::DeviceInfo &device_info, - const transport_manager::ConnectionUID &connection_id) { + const transport_manager::ConnectionUID connection_id) { LOG4CXX_AUTO_TRACE(logger_); DeviceMap::iterator it = device_list_.find(device_info.device_handle()); @@ -208,7 +193,7 @@ void ConnectionHandlerImpl::OnConnectionEstablished( ConnectionList::value_type( connection_id, new Connection(connection_id, device_info.device_handle(), this, - HeartBeatTimeout()))); + get_settings().heart_beat_timeout()))); } void ConnectionHandlerImpl::OnConnectionFailed( @@ -262,39 +247,28 @@ void ConnectionHandlerImpl::RemoveConnection( } #ifdef ENABLE_SECURITY -namespace { -bool AllowProtection(const protocol_handler::ServiceType &service_type, - const bool is_protected) { - if (is_protected) { - // Check deliver-specific services (which shall not be protected) - const std::list<int> force_unprotected_list = - profile::Profile::instance()->ReadIntContainer( - "Security Manager", "ForceUnprotectedService", NULL); - if (std::find(force_unprotected_list.begin(), force_unprotected_list.end(), service_type) != - force_unprotected_list.end()) { - LOG4CXX_ERROR(logger_, "Service " << static_cast<int>(service_type) - << " is forbidden to be protected"); - return false; - } - } else { - // Check deliver-specific services (which shall be protected) - const std::list<int> force_protected_list = - profile::Profile::instance()->ReadIntContainer( - "Security Manager", "ForceProtectedService", NULL); - if (std::find(force_protected_list.begin(), force_protected_list.end(), service_type) != - force_protected_list.end()) { - LOG4CXX_ERROR(logger_, "Service " << static_cast<int>(service_type) - << " shall be protected"); - return false; - } +bool AllowProtection(const ConnectionHandlerSettings& settings, + const protocol_handler::ServiceType& service_type, + const bool is_protected) { + const std::vector<int>& force_unprotected_list = + is_protected ? settings.force_unprotected_service() + : settings.force_protected_service(); + + if (std::find(force_unprotected_list.begin(), force_unprotected_list.end(), service_type) != + force_unprotected_list.end()) { + LOG4CXX_ERROR(logger_, + "Service " << static_cast<int>(service_type) + << " shall be protected"); + return false; } + LOG4CXX_DEBUG(logger_, + "Service " << static_cast<int>(service_type) << " allowed"); return true; } -} // namespace #endif // ENABLE_SECURITY uint32_t ConnectionHandlerImpl::OnSessionStartedCallback( - const transport_manager::ConnectionUID &connection_handle, + const transport_manager::ConnectionUID connection_handle, const uint8_t session_id, const protocol_handler::ServiceType &service_type, const bool is_protected, uint32_t* hash_id) { LOG4CXX_AUTO_TRACE(logger_); @@ -303,7 +277,7 @@ uint32_t ConnectionHandlerImpl::OnSessionStartedCallback( *hash_id = protocol_handler::HASH_ID_WRONG; } #ifdef ENABLE_SECURITY - if (!AllowProtection(service_type, is_protected)) { + if (!AllowProtection(get_settings(), service_type, is_protected)) { return 0; } #endif // ENABLE_SECURITY @@ -389,7 +363,8 @@ void ConnectionHandlerImpl::OnMalformedMessageCallback( } uint32_t ConnectionHandlerImpl::OnSessionEndedCallback( - const uint32_t &connection_handle, const uint8_t session_id, + const transport_manager::ConnectionUID connection_handle, + const uint8_t session_id, const uint32_t &hashCode, const protocol_handler::ServiceType &service_type) { LOG4CXX_AUTO_TRACE(logger_); @@ -442,7 +417,8 @@ uint32_t ConnectionHandlerImpl::OnSessionEndedCallback( } uint32_t ConnectionHandlerImpl::KeyFromPair( - transport_manager::ConnectionUID connection_handle, uint8_t session_id) { + transport_manager::ConnectionUID connection_handle, + uint8_t session_id) const { const uint32_t key = connection_handle | (session_id << 16); LOG4CXX_DEBUG(logger_, "Key for ConnectionHandle:" << static_cast<uint32_t>(connection_handle) @@ -456,8 +432,8 @@ uint32_t ConnectionHandlerImpl::KeyFromPair( } void ConnectionHandlerImpl::PairFromKey(uint32_t key, - uint32_t *connection_handle, - uint8_t *session_id) { + transport_manager::ConnectionUID *connection_handle, + uint8_t *session_id) const { *connection_handle = key & 0xFF00FFFF; *session_id = key >> 16; LOG4CXX_DEBUG(logger_, "ConnectionHandle:" @@ -468,7 +444,7 @@ void ConnectionHandlerImpl::PairFromKey(uint32_t key, int32_t ConnectionHandlerImpl::GetDataOnSessionKey( uint32_t key, uint32_t* app_id, std::list<int32_t>* sessions_list, - uint32_t* device_id) { + uint32_t* device_id) const { LOG4CXX_AUTO_TRACE(logger_); const int32_t error_result = -1; @@ -476,7 +452,7 @@ int32_t ConnectionHandlerImpl::GetDataOnSessionKey( uint8_t session_id = 0; PairFromKey(key, &conn_handle, &session_id); - ConnectionList::iterator it = connection_list_.find(conn_handle); + ConnectionList::const_iterator it = connection_list_.find(conn_handle); if (connection_list_.end() == it) { LOG4CXX_ERROR(logger_, "Connection not found for key: " << key); return error_result; @@ -512,6 +488,20 @@ int32_t ConnectionHandlerImpl::GetDataOnSessionKey( return 0; } +const ConnectionHandlerSettings& ConnectionHandlerImpl::get_settings() const +{ + return settings_; +} + +const protocol_handler::SessionObserver& +ConnectionHandlerImpl::get_session_observer() { + return *this; +} + +DevicesDiscoveryStarter& ConnectionHandlerImpl::get_device_discovery_starter() { + return *this; +} + struct CompareMAC { explicit CompareMAC(const std::string &mac) : mac_(mac) {} bool operator() (const DeviceMap::value_type &device) { @@ -539,11 +529,11 @@ int32_t ConnectionHandlerImpl::GetDataOnDeviceID( DeviceHandle device_handle, std::string *device_name, std::list<uint32_t> *applications_list, std::string *mac_address, - std::string* connection_type ) { + std::string* connection_type ) const { LOG4CXX_AUTO_TRACE(logger_); int32_t result = -1; - DeviceMap::iterator it = device_list_.find(device_handle); + DeviceMap::const_iterator it = device_list_.find(device_handle); if (device_list_.end() == it) { LOG4CXX_ERROR(logger_, "Device not found!"); return result; @@ -558,13 +548,13 @@ int32_t ConnectionHandlerImpl::GetDataOnDeviceID( if (applications_list) { applications_list->clear(); sync_primitives::AutoReadLock connection_list_lock(connection_list_lock_); - for (ConnectionList::iterator itr = connection_list_.begin(); + for (ConnectionList::const_iterator itr = connection_list_.begin(); itr != connection_list_.end(); ++itr) { if (device_handle == (*itr).second->connection_device_handle()) { const SessionMap &session_map = (itr->second)->session_map(); for (SessionMap::const_iterator session_it = session_map.begin(); session_map.end() != session_it; ++session_it) { - const transport_manager::ConnectionUID &connection_handle = itr->first; + const transport_manager::ConnectionUID connection_handle = itr->first; const uint32_t session_id = session_it->first; const uint32_t application_id = KeyFromPair(connection_handle, session_id); applications_list->push_back(application_id); @@ -654,11 +644,7 @@ ConnectionHandlerImpl::GetHandshakeContext(uint32_t key) const { void ConnectionHandlerImpl::StartDevicesDiscovery() { LOG4CXX_AUTO_TRACE(logger_); - if (NULL == transport_manager_) { - LOG4CXX_ERROR(logger_, "Null pointer to TransportManager."); - return; - } - transport_manager_->SearchDevices(); + transport_manager_.SearchDevices(); sync_primitives::AutoLock lock(connection_handler_observer_lock_); if (connection_handler_observer_) { connection_handler_observer_->OnDeviceListUpdated(device_list_); @@ -672,17 +658,13 @@ void ConnectionHandlerImpl::ConnectToDevice( if (device_list_.end() != it_in) { LOG4CXX_INFO(logger_, "Connecting to device with handle " << device_handle); - if (transport_manager_) { - if (transport_manager::E_SUCCESS - != transport_manager_->ConnectDevice(device_handle)) { - LOG4CXX_WARN(logger_, "Can't connect to device"); - } + if (transport_manager::E_SUCCESS + != transport_manager_.ConnectDevice(device_handle)) { + LOG4CXX_WARN(logger_, "Can't connect to device"); } else { - LOG4CXX_ERROR(logger_, "Null pointer to TransportManager."); + LOG4CXX_ERROR( + logger_, "Application Manager wanted to connect to non-existing device"); } - } else { - LOG4CXX_ERROR( - logger_, "Application Manager wanted to connect to non-existing device"); } } @@ -695,12 +677,7 @@ void ConnectionHandlerImpl::ConnectToAllDevices() { void ConnectionHandlerImpl::StartTransportManager() { LOG4CXX_AUTO_TRACE(logger_); - if (NULL == transport_manager_) { - LOG4CXX_ERROR(logger_, "Null pointer to TransportManager."); - return; - } - - transport_manager_->Visibility(true); + transport_manager_.Visibility(true); } void ConnectionHandlerImpl::CloseRevokedConnection(uint32_t connection_key) { @@ -716,13 +693,9 @@ void ConnectionHandlerImpl::CloseRevokedConnection(uint32_t connection_key) { void ConnectionHandlerImpl::CloseConnection( ConnectionHandle connection_handle) { LOG4CXX_AUTO_TRACE(logger_); - if (!transport_manager_) { - LOG4CXX_ERROR(logger_, "Null pointer to TransportManager."); - return; - } transport_manager::ConnectionUID connection_uid = ConnectionUIDFromHandle(connection_handle); - transport_manager_->DisconnectForce(connection_uid); + transport_manager_.DisconnectForce(connection_uid); sync_primitives::AutoWriteLock connection_list_lock(connection_list_lock_); @@ -910,7 +883,7 @@ void ConnectionHandlerImpl::KeepConnectionAlive(uint32_t connection_key, } void ConnectionHandlerImpl::OnConnectionEnded( - const transport_manager::ConnectionUID &connection_id) { + const transport_manager::ConnectionUID connection_id) { LOG4CXX_INFO(logger_, "Delete Connection: " << static_cast<int32_t>(connection_id) << " from the list."); connection_list_lock_.AcquireForWriting(); @@ -955,11 +928,12 @@ void ConnectionHandlerImpl::BindProtocolVersionWithSession( } bool ConnectionHandlerImpl::IsHeartBeatSupported( - transport_manager::ConnectionUID connection_handle,uint8_t session_id) { + transport_manager::ConnectionUID connection_handle, + uint8_t session_id) const { LOG4CXX_AUTO_TRACE(logger_); sync_primitives::AutoReadLock lock(connection_list_lock_); uint32_t connection = static_cast<uint32_t>(connection_handle); - ConnectionList::iterator it = connection_list_.find(connection); + ConnectionList::const_iterator it = connection_list_.find(connection); if (connection_list_.end() == it) { LOG4CXX_WARN(logger_, "Connection not found !"); return false; @@ -968,10 +942,10 @@ bool ConnectionHandlerImpl::IsHeartBeatSupported( } bool ConnectionHandlerImpl::ProtocolVersionUsed(uint32_t connection_id, - uint8_t session_id, uint8_t& protocol_version) { + uint8_t session_id, uint8_t& protocol_version) const { LOG4CXX_AUTO_TRACE(logger_); sync_primitives::AutoReadLock lock(connection_list_lock_); - ConnectionList::iterator it = connection_list_.find(connection_id); + ConnectionList::const_iterator it = connection_list_.find(connection_id); if (connection_list_.end() != it) { return it->second->ProtocolVersion(session_id, protocol_version); } @@ -988,9 +962,8 @@ const DeviceMap& ConnectionHandlerImpl::getDeviceList() { return device_list_; } -void ConnectionHandlerImpl::addDeviceConnection( - const transport_manager::DeviceInfo &device_info, - const transport_manager::ConnectionUID &connection_id) { +void ConnectionHandlerImpl::addDeviceConnection(const transport_manager::DeviceInfo &device_info, + const transport_manager::ConnectionUID connection_id) { // Add Device OnDeviceAdded(device_info); // Add connection 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 ed9fda44e0..1d32355171 100644 --- a/src/components/connection_handler/test/connection_handler_impl_test.cc +++ b/src/components/connection_handler/test/connection_handler_impl_test.cc @@ -30,23 +30,24 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include <gtest/gtest.h> +#include "gtest/gtest.h" #include <fstream> #include <cstdint> +#include <vector> #include "connection_handler/connection_handler_impl.h" #include "protocol/common.h" -#include "config_profile/profile.h" // TODO(EZamakhov): move security test #include "security_manager/mock_security_manager.h" #include "security_manager/mock_ssl_context.h" -#include "protocol_handler_mock.h" +#include "protocol_handler/protocol_handler_mock.h" #include "connection_handler_observer_mock.h" -#include "transport_manager_mock.h" +#include "connection_handler/mock_connection_handler_settings.h" +#include "transport_manager/transport_manager_mock.h" #include "encryption/hashing.h" namespace test { namespace components { -namespace connection_handle_test { +namespace connection_handler_test { using namespace ::connection_handler; using ::protocol_handler::ServiceType; @@ -55,18 +56,38 @@ using ::testing::_; using ::testing::InSequence; using ::testing::Mock; using ::testing::Return; +using ::testing::ReturnRefOfCopy; // For service types and PROTECTION_ON/OFF +enum UnnamedService { + served_service1 = 0x06, + served_service2 = 0x08 +}; + class ConnectionHandlerTest : public ::testing::Test { protected: void SetUp() OVERRIDE { - connection_handler_ = ConnectionHandlerImpl::instance(); + connection_handler_ = new ConnectionHandlerImpl( + mock_connection_handler_settings, + mock_transport_manager); uid_ = 1u; connection_key_ = connection_handler_->KeyFromPair(0, 0u); + protected_services_.clear(); + unprotected_services_.clear(); + SetSpecificServices(); } void TearDown() OVERRIDE { - ConnectionHandlerImpl::destroy(); + delete connection_handler_; + } + + void SetSpecificServices() { +#ifdef ENABLE_SECURITY + ON_CALL(mock_connection_handler_settings, force_protected_service()) + .WillByDefault(ReturnRefOfCopy(protected_services_)); + ON_CALL(mock_connection_handler_settings, force_unprotected_service()) + .WillByDefault(ReturnRefOfCopy(unprotected_services_)); +#endif // ENABLE_SECURITY } // Additional SetUp void AddTestDeviceConnection() { @@ -84,7 +105,6 @@ class ConnectionHandlerTest : public ::testing::Test { connection_handler_->addDeviceConnection(device_info, uid_); connection_key_ = connection_handler_->KeyFromPair(uid_, 0u); // Remove all specific services - SetSpecificServices("", ""); } void AddTestSession() { start_session_id_ = connection_handler_->OnSessionStartedCallback( @@ -107,22 +127,6 @@ class ConnectionHandlerTest : public ::testing::Test { EXPECT_EQ(session_id, start_session_id_); } - // Additional SetUp - void SetSpecificServices(const std::string& protect, const std::string& not_protect) { - const char* config_file = "config.ini"; - std::ofstream file_config(config_file); - ASSERT_TRUE(file_config.is_open()); - const std::string non("NON"); - file_config << "[Security Manager]" << std::endl - << "; Force protected services (could be id's from 0x01 to 0xFF)" - << std::endl << "ForceProtectedService = " - << (protect.empty() ? non : protect) << std::endl - << "; Force unprotected services" << std::endl - << "ForceUnprotectedService = " - << (not_protect.empty() ? non : not_protect) << std::endl; - file_config.close(); - profile::Profile::instance()->config_file_name(config_file); - } // Check Service Wrapper // If session_id is NULL - check that there is no sessions in connection void CheckSessionExists(const int connectionId, const int session_id) { @@ -226,6 +230,10 @@ class ConnectionHandlerTest : public ::testing::Test { } ConnectionHandlerImpl* connection_handler_; + testing::NiceMock<transport_manager_test::TransportManagerMock> + mock_transport_manager; + testing::NiceMock<MockConnectionHandlerSettings> + mock_connection_handler_settings; transport_manager::DeviceHandle device_handle_; transport_manager::ConnectionUID uid_; uint32_t connection_key_; @@ -236,6 +244,9 @@ class ConnectionHandlerTest : public ::testing::Test { std::string device_name_; std::string mac_address_; + const uint32_t heartbeat_timeout = 100u; + std::vector<int> protected_services_; + std::vector<int> unprotected_services_; }; TEST_F(ConnectionHandlerTest, StartSession_NoConnection) { @@ -389,7 +400,6 @@ TEST_F(ConnectionHandlerTest, GetPairFromKey) { TEST_F(ConnectionHandlerTest, IsHeartBeatSupported) { AddTestDeviceConnection(); AddTestSession(); - ::profile::Profile::instance()->config_file_name("smartDeviceLink.ini"); ChangeProtocol(uid_, start_session_id_, PROTOCOL_VERSION_3); EXPECT_TRUE(connection_handler_->IsHeartBeatSupported(uid_, start_session_id_)); } @@ -513,8 +523,7 @@ TEST_F(ConnectionHandlerTest, OnApplicationFloodCallBack_SessionFound) { TEST_F(ConnectionHandlerTest, StartDevicesDiscovery) { AddTestDeviceConnection(); AddTestSession(); - transport_manager_test::TransportManagerMock mock_transport_manager; - connection_handler_->set_transport_manager(&mock_transport_manager); + connection_handler_test::ConnectionHandlerObserverMock mock_connection_handler_observer; connection_handler_->set_connection_handler_observer(&mock_connection_handler_observer); @@ -614,8 +623,7 @@ TEST_F(ConnectionHandlerTest, GetConnectedDevicesMAC) { TEST_F(ConnectionHandlerTest, StartTransportManager) { AddTestDeviceConnection(); AddTestSession(); - transport_manager_test::TransportManagerMock mock_transport_manager; - connection_handler_->set_transport_manager(&mock_transport_manager); + EXPECT_CALL(mock_transport_manager, Visibility(true)); connection_handler_->StartTransportManager(); } @@ -706,8 +714,6 @@ TEST_F(ConnectionHandlerTest, ConnectToDevice) { connection_handler_->OnDeviceAdded(device1); connection_handler_->OnDeviceAdded(device2); - transport_manager_test::TransportManagerMock mock_transport_manager; - connection_handler_->set_transport_manager(&mock_transport_manager); EXPECT_CALL(mock_transport_manager, ConnectDevice(dev_handle1)) .WillOnce(Return(transport_manager::E_SUCCESS)); EXPECT_CALL(mock_transport_manager, ConnectDevice(dev_handle2)).Times(0); @@ -726,8 +732,6 @@ TEST_F(ConnectionHandlerTest, ConnectToAllDevices) { connection_handler_->OnDeviceAdded(device1); connection_handler_->OnDeviceAdded(device2); - transport_manager_test::TransportManagerMock mock_transport_manager; - connection_handler_->set_transport_manager(&mock_transport_manager); EXPECT_CALL(mock_transport_manager, ConnectDevice(dev_handle1)) .WillOnce(Return(transport_manager::E_SUCCESS)); EXPECT_CALL(mock_transport_manager, ConnectDevice(dev_handle2)) @@ -738,8 +742,7 @@ TEST_F(ConnectionHandlerTest, ConnectToAllDevices) { TEST_F(ConnectionHandlerTest, CloseConnection) { AddTestDeviceConnection(); AddTestSession(); - transport_manager_test::TransportManagerMock mock_transport_manager; - connection_handler_->set_transport_manager(&mock_transport_manager); + EXPECT_CALL(mock_transport_manager, DisconnectForce(uid_)); connection_handler_->CloseConnection(uid_); } @@ -747,8 +750,7 @@ TEST_F(ConnectionHandlerTest, CloseConnection) { TEST_F(ConnectionHandlerTest, CloseRevokedConnection) { AddTestDeviceConnection(); AddTestSession(); - transport_manager_test::TransportManagerMock mock_transport_manager; - connection_handler_->set_transport_manager(&mock_transport_manager); + EXPECT_CALL(mock_transport_manager, DisconnectForce(uid_)); connection_handler_->CloseRevokedConnection(connection_key_); } @@ -1001,10 +1003,13 @@ TEST_F(ConnectionHandlerTest, SessionStarted_WithRpc) { } TEST_F(ConnectionHandlerTest, SessionStarted_StartSession_SecureSpecific_Unprotect) { + EXPECT_CALL(mock_connection_handler_settings, heart_beat_timeout()) + .WillOnce(Return(heartbeat_timeout)); // Add virtual device and connection AddTestDeviceConnection(); // Forbid start kRPC without encryption - SetSpecificServices("0x07", ""); + protected_services_.push_back(kRpc); + SetSpecificServices(); // Start new session with RPC service const uint32_t session_id_fail = connection_handler_->OnSessionStartedCallback( uid_, 0, kRpc, PROTECTION_OFF, &out_hash_id_); @@ -1017,7 +1022,9 @@ TEST_F(ConnectionHandlerTest, SessionStarted_StartSession_SecureSpecific_Unprote #endif // ENABLE_SECURITY // Allow start kRPC without encryption - SetSpecificServices("0x00, Non", ""); + protected_services_.clear(); + protected_services_.push_back(kControl); + SetSpecificServices(); // Start new session with RPC service const uint32_t session_id = connection_handler_->OnSessionStartedCallback( uid_, 0, kRpc, PROTECTION_OFF, &out_hash_id_); @@ -1030,7 +1037,11 @@ TEST_F(ConnectionHandlerTest, SessionStarted_StartSession_SecureSpecific_Protect // Add virtual device and connection AddTestDeviceConnection(); // Forbid start kRPC with encryption - SetSpecificServices("", "0x06, 0x07, 0x08, Non"); + unprotected_services_.push_back(UnnamedService::served_service1); + unprotected_services_.push_back(kRpc); + unprotected_services_.push_back(UnnamedService::served_service2); + unprotected_services_.push_back(kControl); + SetSpecificServices(); // Start new session with RPC service const uint32_t session_id_fail = connection_handler_->OnSessionStartedCallback( uid_, 0, kRpc, PROTECTION_ON, NULL); @@ -1041,7 +1052,9 @@ TEST_F(ConnectionHandlerTest, SessionStarted_StartSession_SecureSpecific_Protect #endif // ENABLE_SECURITY // Allow start kRPC with encryption - SetSpecificServices("", "0x00, 0x05, Non"); + unprotected_services_.clear(); + unprotected_services_.push_back(kControl); + SetSpecificServices(); // Start new session with RPC service const uint32_t session_id = connection_handler_->OnSessionStartedCallback( uid_, 0, kRpc, PROTECTION_ON, &out_hash_id_); @@ -1056,11 +1069,12 @@ TEST_F(ConnectionHandlerTest, SessionStarted_StartService_SecureSpecific_Unprote AddTestDeviceConnection(); AddTestSession(); - // Audio is 0x0A - ASSERT_EQ(0x0A, kAudio); - // Forbid start kAudio without encryption - SetSpecificServices("0x06, 0x0A, 0x08, Non", ""); + protected_services_.push_back(UnnamedService::served_service1); + protected_services_.push_back(kAudio); + protected_services_.push_back(UnnamedService::served_service2); + protected_services_.push_back(kControl); + SetSpecificServices(); // Start new session with Audio service const uint32_t session_id2 = connection_handler_->OnSessionStartedCallback( uid_, start_session_id_, kAudio, PROTECTION_OFF, NULL); @@ -1070,7 +1084,12 @@ TEST_F(ConnectionHandlerTest, SessionStarted_StartService_SecureSpecific_Unprote EXPECT_EQ(1u, session_id2); #endif // ENABLE_SECURITY // Allow start kAudio without encryption - SetSpecificServices("0x06, 0x0B, 0x08, Non", ""); + protected_services_.clear(); + protected_services_.push_back(UnnamedService::served_service1); + protected_services_.push_back(kMobileNav); + protected_services_.push_back(UnnamedService::served_service2); + protected_services_.push_back(kControl); + SetSpecificServices(); const uint32_t session_id3 = connection_handler_->OnSessionStartedCallback( uid_, start_session_id_, kAudio, PROTECTION_OFF, &out_hash_id_); // Returned original session id @@ -1090,10 +1109,12 @@ TEST_F(ConnectionHandlerTest, SessionStarted_StartService_SecureSpecific_Protect AddTestDeviceConnection(); AddTestSession(); - // Audio is 0x0A - ASSERT_EQ(0x0A, kAudio); // Forbid start kAudio with encryption - SetSpecificServices("", "0x06, 0x0A, 0x08, Non"); + unprotected_services_.push_back(UnnamedService::served_service1); + unprotected_services_.push_back(kAudio); + unprotected_services_.push_back(UnnamedService::served_service2); + unprotected_services_.push_back(kControl); + SetSpecificServices(); // Start new session with Audio service const uint32_t session_id_reject = connection_handler_->OnSessionStartedCallback( uid_, start_session_id_, kAudio, PROTECTION_ON, NULL); @@ -1103,7 +1124,8 @@ TEST_F(ConnectionHandlerTest, SessionStarted_StartService_SecureSpecific_Protect EXPECT_EQ(1u, session_id_reject); #endif // ENABLE_SECURITY // Allow start kAudio with encryption - SetSpecificServices("", "Non"); + unprotected_services_.clear(); + SetSpecificServices(); const uint32_t session_id3 = connection_handler_->OnSessionStartedCallback( uid_, start_session_id_, kAudio, PROTECTION_ON, &out_hash_id_); // Returned original session id diff --git a/src/components/connection_handler/test/connection_test.cc b/src/components/connection_handler/test/connection_test.cc index b9c27b6768..c884b51bbd 100644 --- a/src/components/connection_handler/test/connection_test.cc +++ b/src/components/connection_handler/test/connection_test.cc @@ -37,9 +37,10 @@ #include "connection_handler/connection_handler_impl.h" #include "protocol/service_type.h" #include "utils/shared_ptr.h" +#include "connection_handler/mock_connection_handler_settings.h" +#include "transport_manager/transport_manager_mock.h" #include "security_manager/mock_security_manager.h" #include "security_manager/mock_ssl_context.h" -#include "config_profile/profile.h" #define EXPECT_RETURN_TRUE true #define EXPECT_RETURN_FALSE false @@ -48,14 +49,15 @@ namespace test { namespace components { -namespace connection_handle { +namespace connection_handler_test { using namespace ::connection_handler; using namespace ::protocol_handler; class ConnectionTest : public ::testing::Test { protected: void SetUp() OVERRIDE { - connection_handler_ = ConnectionHandlerImpl::instance(); + connection_handler_ = new ConnectionHandlerImpl( + mock_connection_handler_settings, transport_manager_mock); const ConnectionHandle connectionHandle = 0; const DeviceHandle device_handle = 0; connection_.reset( @@ -65,7 +67,7 @@ class ConnectionTest : public ::testing::Test { void TearDown() OVERRIDE { connection_.reset(); - ConnectionHandlerImpl::destroy(); + delete connection_handler_; } void StartSession() { session_id = connection_->AddNewSession(); @@ -126,6 +128,9 @@ class ConnectionTest : public ::testing::Test { } ::utils::SharedPtr<Connection> connection_; + MockConnectionHandlerSettings mock_connection_handler_settings; + testing::StrictMock<transport_manager_test::TransportManagerMock> + transport_manager_mock; ConnectionHandlerImpl* connection_handler_; uint32_t session_id; }; @@ -163,7 +168,6 @@ TEST_F(ConnectionTest, HeartBeat_NotSupported) { TEST_F(ConnectionTest, HeartBeat_Supported) { // Arrange StartSession(); - ::profile::Profile::instance()->config_file_name("smartDeviceLink.ini"); // Check if protocol version is 3 const uint8_t protocol_version = static_cast<uint8_t>(PROTOCOL_VERSION_3); connection_->UpdateProtocolVersionSession(session_id, protocol_version); 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 b295ed430b..e442a144bf 100644 --- a/src/components/connection_handler/test/heart_beat_monitor_test.cc +++ b/src/components/connection_handler/test/heart_beat_monitor_test.cc @@ -32,11 +32,11 @@ #include <string> #include <iostream> -#include "gmock/gmock.h" +#include "gtest/gtest.h" #include "connection_handler/heartbeat_monitor.h" #include "connection_handler/connection.h" #include "connection_handler/connection_handler.h" -#include "config_profile/profile.h" +#include "connection_handler/mock_connection_handler.h" namespace { const int32_t MILLISECONDS_IN_SECOND = 1000; @@ -49,56 +49,14 @@ namespace components { namespace connection_handler_test { using ::testing::_; -class ConnectionHandlerMock : public connection_handler::ConnectionHandler { - public: - MOCK_METHOD1(set_connection_handler_observer, - void(connection_handler::ConnectionHandlerObserver*)); - MOCK_METHOD1(set_transport_manager, - void(transport_manager::TransportManager*)); - MOCK_METHOD0(StartTransportManager, void()); - MOCK_METHOD1(ConnectToDevice, - void(connection_handler::DeviceHandle device_handle)); - MOCK_METHOD0(ConnectToAllDevices, void()); - MOCK_METHOD1(CloseRevokedConnection, void(uint32_t connection_key)); - MOCK_METHOD1(CloseConnection, - void(connection_handler::ConnectionHandle connection_handle)); - MOCK_METHOD1(GetConnectionSessionsCount, uint32_t(uint32_t connection_key)); - MOCK_METHOD2(GetDeviceID, - bool(const std::string& mac_address, - connection_handler::DeviceHandle* device_handle)); - MOCK_CONST_METHOD1(GetConnectedDevicesMAC, - void(std::vector<std::string>& device_macs)); - MOCK_METHOD2(CloseSession, - void(uint32_t key, - connection_handler::CloseSessionReason close_reason)); - MOCK_METHOD3(CloseSession, - void(connection_handler::ConnectionHandle connection_handle, - uint8_t session_id, - connection_handler::CloseSessionReason close_reason)); - MOCK_METHOD2(SendEndService, void(uint32_t key, uint8_t service_type)); - - MOCK_METHOD1(StartSessionHeartBeat, void(uint32_t key)); - MOCK_METHOD2(SendHeartBeat, - void(connection_handler::ConnectionHandle connection_handle, - uint8_t session_id)); - MOCK_METHOD2(SetHeartBeatTimeout, - void(uint32_t connection_key, uint32_t timeout)); - MOCK_METHOD2(BindProtocolVersionWithSession, - void(uint32_t connection_key, uint8_t protocol_version)); - MOCK_METHOD4(GetDataOnSessionKey, - int32_t(uint32_t key, uint32_t* app_id, - std::list<int32_t>* sessions_list, uint32_t* device_id)); -}; - class HeartBeatMonitorTest : public testing::Test { public: HeartBeatMonitorTest() : conn(NULL) { - profile::Profile::instance()->config_file_name("smartDeviceLink.ini"); - kTimeout = profile::Profile::instance()->heart_beat_timeout(); + kTimeout = 5000u; } protected: - testing::NiceMock<ConnectionHandlerMock> connection_handler_mock; + testing::NiceMock<MockConnectionHandler> connection_handler_mock; connection_handler::Connection* conn; uint32_t kTimeout; static const connection_handler::ConnectionHandle kConnectionHandle = |