summaryrefslogtreecommitdiff
path: root/src/components/connection_handler/test/connection_test.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/connection_handler/test/connection_test.cc')
-rw-r--r--src/components/connection_handler/test/connection_test.cc178
1 files changed, 169 insertions, 9 deletions
diff --git a/src/components/connection_handler/test/connection_test.cc b/src/components/connection_handler/test/connection_test.cc
index de21dd1e97..98b83f5fc4 100644
--- a/src/components/connection_handler/test/connection_test.cc
+++ b/src/components/connection_handler/test/connection_test.cc
@@ -36,6 +36,7 @@
#include "protocol/common.h"
#include "connection_handler/connection.h"
+#include "connection_handler/mock_connection_handler.h"
#include "connection_handler/connection_handler_impl.h"
#include "protocol/service_type.h"
#include "connection_handler/mock_connection_handler_settings.h"
@@ -56,6 +57,8 @@ namespace connection_handler_test {
using namespace ::connection_handler;
using namespace ::protocol_handler;
+using ::testing::Return;
+
class ConnectionTest : public ::testing::Test {
protected:
void SetUp() OVERRIDE {
@@ -78,7 +81,7 @@ class ConnectionTest : public ::testing::Test {
session_id, protocol_handler::PROTOCOL_VERSION_3);
}
void StartDefaultSession() {
- session_id = connection_->AddNewSession();
+ session_id = connection_->AddNewSession(kDefaultConnectionHandle);
EXPECT_NE(session_id, 0u);
const SessionMap sessionMap = connection_->session_map();
EXPECT_FALSE(sessionMap.empty());
@@ -88,13 +91,14 @@ class ConnectionTest : public ::testing::Test {
std::find(serviceList.begin(), serviceList.end(), kRpc);
const bool found_result = (it != serviceList.end());
EXPECT_TRUE(found_result);
+ EXPECT_EQ(connection_->primary_connection_handle(), 0);
}
void AddNewService(const ServiceType service_type,
const bool protection,
const bool expect_add_new_service_call_result,
const bool expect_exist_service) {
- const bool result =
- connection_->AddNewService(session_id, service_type, protection);
+ const bool result = connection_->AddNewService(
+ session_id, service_type, protection, kDefaultConnectionHandle);
EXPECT_EQ(result, expect_add_new_service_call_result);
#ifdef ENABLE_SECURITY
@@ -110,12 +114,35 @@ class ConnectionTest : public ::testing::Test {
std::find(newServiceList.begin(), newServiceList.end(), service_type);
const bool found_result = it != newServiceList.end();
EXPECT_EQ(expect_exist_service, found_result);
-#ifdef ENABLE_SECURITY
if (found_result) {
const Service& service = *it;
+ transport_manager::ConnectionUID expected_connection_handle =
+ kDefaultConnectionHandle;
+ EXPECT_EQ(service.connection_id, expected_connection_handle);
+#ifdef ENABLE_SECURITY
EXPECT_EQ(service.is_protected_, protection);
- }
#endif // ENABLE_SECURITY
+ }
+ }
+ void AddNewSecondaryService(const ServiceType service_type) {
+ const bool result = connection_->AddNewService(
+ session_id, service_type, false, kSecondaryConnectionHandle);
+ EXPECT_EQ(result, true);
+
+ const SessionMap session_map = connection_->session_map();
+ EXPECT_FALSE(session_map.empty());
+ const ServiceList newServiceList = session_map.begin()->second.service_list;
+ EXPECT_FALSE(newServiceList.empty());
+ const ServiceList::const_iterator it =
+ std::find(newServiceList.begin(), newServiceList.end(), service_type);
+ const bool found_result = it != newServiceList.end();
+ EXPECT_TRUE(found_result);
+ if (found_result) {
+ const Service& service = *it;
+ transport_manager::ConnectionUID expected_secondary_connection_handle =
+ kSecondaryConnectionHandle;
+ EXPECT_EQ(service.connection_id, expected_secondary_connection_handle);
+ }
}
void RemoveService(const ServiceType service_type,
@@ -141,6 +168,8 @@ class ConnectionTest : public ::testing::Test {
transport_manager_mock;
ConnectionHandlerImpl* connection_handler_;
uint32_t session_id;
+ static const transport_manager::ConnectionUID kDefaultConnectionHandle = 1;
+ static const transport_manager::ConnectionUID kSecondaryConnectionHandle = 2;
};
TEST_F(ConnectionTest, Session_TryGetProtocolVersionWithoutSession) {
@@ -236,13 +265,17 @@ TEST_F(ConnectionTest, HeartBeat_Protocol5_ZeroHeartBeat_NotSupported) {
// Try to add service without session
TEST_F(ConnectionTest, Session_AddNewServiceWithoutSession) {
- EXPECT_EQ(connection_->AddNewService(session_id, kAudio, true),
+ EXPECT_EQ(connection_->AddNewService(
+ session_id, kAudio, true, kDefaultConnectionHandle),
EXPECT_RETURN_FALSE);
- EXPECT_EQ(connection_->AddNewService(session_id, kAudio, false),
+ EXPECT_EQ(connection_->AddNewService(
+ session_id, kAudio, false, kDefaultConnectionHandle),
EXPECT_RETURN_FALSE);
- EXPECT_EQ(connection_->AddNewService(session_id, kMobileNav, true),
+ EXPECT_EQ(connection_->AddNewService(
+ session_id, kMobileNav, true, kDefaultConnectionHandle),
EXPECT_RETURN_FALSE);
- EXPECT_EQ(connection_->AddNewService(session_id, kMobileNav, false),
+ EXPECT_EQ(connection_->AddNewService(
+ session_id, kMobileNav, false, kDefaultConnectionHandle),
EXPECT_RETURN_FALSE);
}
@@ -409,6 +442,133 @@ TEST_F(ConnectionTest, RemoveSession) {
EXPECT_EQ(0u, connection_->RemoveSession(session_id));
}
+TEST_F(ConnectionTest, AddNewSession_VerifyAddSessionCalled) {
+ MockConnectionHandler mock_connection_handler;
+
+ ConnectionHandle connection_handle = 123;
+ DeviceHandle device_handle = 0u;
+ uint32_t heart_beat = 10000u;
+ Connection* connection = new Connection(
+ connection_handle, device_handle, &mock_connection_handler, heart_beat);
+
+ transport_manager::ConnectionUID connection_handle_uid = 1;
+ uint32_t mock_session_id = 2;
+ EXPECT_CALL(mock_connection_handler, AddSession(connection_handle_uid))
+ .WillOnce(Return(mock_session_id));
+
+ uint32_t sid = connection->AddNewSession(connection_handle_uid);
+ EXPECT_EQ(mock_session_id, sid);
+
+ EXPECT_CALL(mock_connection_handler, RemoveSession(mock_session_id))
+ .WillOnce(Return(true)); // invoked by destructor of connection
+ delete connection;
+}
+
+TEST_F(ConnectionTest, RemoveSession_VerifyRemoveSessionCalled) {
+ MockConnectionHandler mock_connection_handler;
+
+ ConnectionHandle connection_handle = 123;
+ DeviceHandle device_handle = 0u;
+ uint32_t heart_beat = 10000u;
+ Connection* connection = new Connection(
+ connection_handle, device_handle, &mock_connection_handler, heart_beat);
+
+ transport_manager::ConnectionUID connection_handle_uid = 1;
+ uint32_t mock_session_id = 10;
+ EXPECT_CALL(mock_connection_handler, AddSession(connection_handle_uid))
+ .WillOnce(Return(mock_session_id));
+ EXPECT_CALL(mock_connection_handler,
+ RemoveSession(static_cast<uint8_t>(mock_session_id)))
+ .WillOnce(Return(true));
+
+ uint32_t sid = connection->AddNewSession(connection_handle_uid);
+
+ uint32_t ret = connection->RemoveSession(sid);
+ EXPECT_EQ(sid, ret);
+
+ delete connection;
+}
+
+TEST_F(ConnectionTest, SecondarySessionTest) {
+ StartSession();
+ AddNewService(
+ kRpc, PROTECTION_OFF, EXPECT_RETURN_FALSE, EXPECT_SERVICE_EXISTS);
+
+ const ConnectionHandle connectionHandle = 0;
+ const DeviceHandle device_handle = 0u;
+ const uint32_t heart_beat = 0u;
+ Connection* secondary_connection = new Connection(
+ connectionHandle, device_handle, connection_handler_, heart_beat);
+
+ secondary_connection->SetPrimaryConnectionHandle(kDefaultConnectionHandle);
+ connection_handler::ConnectionHandle expected_primary_connection_handle =
+ kDefaultConnectionHandle;
+ EXPECT_EQ(secondary_connection->primary_connection_handle(),
+ expected_primary_connection_handle);
+
+ AddNewSecondaryService(kAudio);
+ AddNewSecondaryService(kMobileNav);
+
+ delete secondary_connection;
+}
+
+TEST_F(ConnectionTest, RemoveSecondaryServices_SUCCESS) {
+ StartSession();
+
+ ServiceType services[2] = {kMobileNav, kAudio};
+ AddNewSecondaryService(services[0]);
+ AddNewSecondaryService(services[1]);
+ size_t services_count = sizeof(services) / sizeof(services[0]);
+
+ std::list<ServiceType> removed_services;
+ uint8_t ret_session_id = connection_->RemoveSecondaryServices(
+ kSecondaryConnectionHandle, removed_services);
+
+ // check return value
+ EXPECT_EQ(session_id, ret_session_id);
+ // check returned list
+ EXPECT_EQ(services_count, removed_services.size());
+ std::list<protocol_handler::ServiceType>::iterator it;
+ it = std::find(removed_services.begin(), removed_services.end(), services[0]);
+ EXPECT_TRUE(it != removed_services.end());
+ it = std::find(removed_services.begin(), removed_services.end(), services[1]);
+ EXPECT_TRUE(it != removed_services.end());
+}
+
+TEST_F(ConnectionTest, RemoveSecondaryServices_NoService) {
+ StartSession();
+ /* do not call AddNewSecondaryService() */
+
+ std::list<ServiceType> removed_services;
+ uint8_t ret_session_id = connection_->RemoveSecondaryServices(
+ kSecondaryConnectionHandle, removed_services);
+
+ // check return value
+ EXPECT_EQ(0, ret_session_id);
+ // check returned list
+ EXPECT_EQ(0u, removed_services.size());
+}
+
+TEST_F(ConnectionTest, RemoveSecondaryServices_InvalidConnectionHandle) {
+ StartSession();
+
+ ServiceType services[2] = {kMobileNav, kAudio};
+ AddNewSecondaryService(services[0]);
+ AddNewSecondaryService(services[1]);
+
+ transport_manager::ConnectionUID invalid_connection_handle = 123;
+ ASSERT_TRUE(kSecondaryConnectionHandle != invalid_connection_handle);
+
+ std::list<ServiceType> removed_services;
+ uint8_t ret_session_id = connection_->RemoveSecondaryServices(
+ invalid_connection_handle, removed_services);
+
+ // check return value
+ EXPECT_EQ(0, ret_session_id);
+ // check returned list
+ EXPECT_EQ(0u, removed_services.size());
+}
+
#ifdef ENABLE_SECURITY
TEST_F(ConnectionTest, SetSSLContextWithoutSession) {