summaryrefslogtreecommitdiff
path: root/src/components/protocol_handler
diff options
context:
space:
mode:
authoriAndrew5 <abyzhynar@luxoft.com>2016-04-04 17:21:43 +0300
committeriAndrew5 <abyzhynar@luxoft.com>2016-04-04 20:42:48 +0300
commit5e4585b5353967f9c20d38387dfcb225821fc8ba (patch)
tree46adcb814a033770603de1d2cb51cb264f870c76 /src/components/protocol_handler
parent728406b86fdb32be171559235db8d22b358f949f (diff)
downloadsdl_core-5e4585b5353967f9c20d38387dfcb225821fc8ba.tar.gz
Change other unit-tests according new code changes
1) Changed header files in tests 2) Renamed transport_manager_mock to mok=ck)transport_manager 3) Renamed other mock classes to match naming rules 4) Deleted redundant headers and namespaces. 5) Changed year in Ford headers 6) Changed raw "new" operator using to MakeShared for creating shared_ptr 7) Corrected header guards 8) Added new method CheckRegularMatches to control_message_matcher Relates: APPLINK-22767
Diffstat (limited to 'src/components/protocol_handler')
-rw-r--r--src/components/protocol_handler/test/CMakeLists.txt14
-rw-r--r--src/components/protocol_handler/test/include/protocol_handler/control_message_matcher.h173
-rw-r--r--src/components/protocol_handler/test/multiframe_builder_test.cc457
-rw-r--r--src/components/protocol_handler/test/protocol_handler_tm_test.cc17
4 files changed, 378 insertions, 283 deletions
diff --git a/src/components/protocol_handler/test/CMakeLists.txt b/src/components/protocol_handler/test/CMakeLists.txt
index 08640f802a..87d91bfd54 100644
--- a/src/components/protocol_handler/test/CMakeLists.txt
+++ b/src/components/protocol_handler/test/CMakeLists.txt
@@ -1,4 +1,4 @@
-# Copyright (c) 2015, Ford Motor Company
+# Copyright (c) 2016, Ford Motor Company
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
@@ -28,9 +28,9 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
-if(BUILD_TESTS)
+if (BUILD_TESTS)
-include_directories(
+include_directories (
include
${COMPONENTS_DIR}/resumption/include
${GMOCK_INCLUDE_DIRECTORY}
@@ -40,7 +40,7 @@ include_directories(
${JSONCPP_INCLUDE_DIRECTORY}
)
-set(LIBRARIES
+set (LIBRARIES
gmock
ProtocolHandler
connectionHandler
@@ -49,7 +49,7 @@ set(LIBRARIES
ProtocolLibrary
)
-set(SOURCES
+set (SOURCES
incoming_data_handler_test.cc
protocol_header_validator_test.cc
protocol_handler_tm_test.cc
@@ -58,6 +58,6 @@ set(SOURCES
multiframe_builder_test.cc
)
-create_test("protocol_handler_test" "${SOURCES}" "${LIBRARIES}")
+create_test ("protocol_handler_test" "${SOURCES}" "${LIBRARIES}")
-endif()
+endif ()
diff --git a/src/components/protocol_handler/test/include/protocol_handler/control_message_matcher.h b/src/components/protocol_handler/test/include/protocol_handler/control_message_matcher.h
index 9239d0847f..f7acc91f0b 100644
--- a/src/components/protocol_handler/test/include/protocol_handler/control_message_matcher.h
+++ b/src/components/protocol_handler/test/include/protocol_handler/control_message_matcher.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2014, Ford Motor Company
+ * Copyright (c) 2016, Ford Motor Company
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -29,10 +29,11 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef TEST_COMPONENTS_INCLUDE_PROTOCOL_HANDLER_CONTROL_MESSAGE_MATCHER_H_
-#define TEST_COMPONENTS_INCLUDE_PROTOCOL_HANDLER_CONTROL_MESSAGE_MATCHER_H_
+#ifndef SRC_COMPONENTS_PROTOCOL_HANDLER_TEST_INCLUDE_PROTOCOL_HANDLER_CONTROL_MESSAGE_MATCHER_H_
+#define SRC_COMPONENTS_PROTOCOL_HANDLER_TEST_INCLUDE_PROTOCOL_HANDLER_CONTROL_MESSAGE_MATCHER_H_
-#include <gmock/gmock.h>
+#include "gmock/gmock.h"
+#include <ios>
#include <string>
#include <vector>
#include "protocol/raw_message.h"
@@ -41,82 +42,106 @@
namespace test {
namespace components {
namespace protocol_handler_test {
-/*
- * Matcher for checking RawMessage with ControlMessage
- * Check error id
- */
-MATCHER_P2(ControlMessage, ExpectedFrameData, ExpectedEncryption,
- (std::string(ExpectedEncryption ? "Protected" : "Unprotected")
- + " control message ")) {
- // Nack shall be always with flag protected off
- DCHECK(ExpectedFrameData != 0x03 /*FRAME_DATA_START_SERVICE_NACK*/ ||
- !ExpectedEncryption);
- const ::protocol_handler::RawMessagePtr message = arg;
- ::protocol_handler::ProtocolPacket packet(message->connection_key());
- const protocol_handler::RESULT_CODE result =
- packet.deserializePacket(message->data(), message->data_size());
+using protocol_handler::ProtocolPacket;
+using protocol_handler::RawMessagePtr;
+using protocol_handler::RESULT_CODE;
+using protocol_handler::FRAME_TYPE_CONTROL;
+using protocol_handler::FRAME_DATA_START_SERVICE_NACK;
+
+bool CheckRegularMatches(const ProtocolPacket& packet,
+ RESULT_CODE result,
+ testing::MatchResultListener& result_listener,
+ uint8_t ExpectedFrameType,
+ uint8_t ExpectedFrameData,
+ uint8_t ExpectedEncryption) {
if (result != protocol_handler::RESULT_OK) {
- *result_listener << "Error while message deserialization.";
+ result_listener << "Error while message deserialization.";
return false;
}
- if (::protocol_handler::FRAME_TYPE_CONTROL != packet.frame_type()) {
- *result_listener << "Is not control message";
+ if (ExpectedFrameType != packet.frame_type()) {
+ result_listener << "Message with frame type 0x" << std::hex
+ << static_cast<int>(packet.frame_type()) << ", not 0x"
+ << std::hex << static_cast<int>(ExpectedFrameType);
return false;
}
if (ExpectedFrameData != packet.frame_data()) {
- *result_listener << "Control message with data 0x"
- << std::hex << static_cast<int>(packet.frame_data())
- << ", not 0x"
- << std::hex << static_cast<int>(ExpectedFrameData);
+ result_listener << "Message with data 0x" << std::hex
+ << static_cast<int>(packet.frame_data()) << ", not 0x"
+ << std::hex << static_cast<int>(ExpectedFrameData);
return false;
}
if (ExpectedEncryption != packet.protection_flag()) {
- *result_listener << "Control message is " <<
- (ExpectedEncryption ? "" : "not ") << "protected";
+ result_listener << "Message is " << (ExpectedEncryption ? "" : "not ")
+ << "protected";
return false;
}
return true;
}
+/*
+ * Matcher for checking RawMessage with ControlMessage
+ * Check error id
+ */
-
-MATCHER_P4(ControlMessage, ExpectedFrameData, ExpectedEncryption,
- ConnectionKey, VectorMatcher,
- (std::string(ExpectedEncryption ? "Protected" : "Unprotected")
- + " control message ")) {
+MATCHER_P2(ControlMessage,
+ ExpectedFrameData,
+ ExpectedEncryption,
+ (std::string(ExpectedEncryption ? "Protected" : "Unprotected") +
+ " control message ")) {
// Nack shall be always with flag protected off
- DCHECK(ExpectedFrameData != 0x03 /*FRAME_DATA_START_SERVICE_NACK*/ ||
- !ExpectedEncryption);
-
- const ::protocol_handler::RawMessagePtr message = arg;
- ::protocol_handler::ProtocolPacket packet(message->connection_key());
- const protocol_handler::RESULT_CODE result =
- packet.deserializePacket(message->data(), message->data_size());
- if (result != protocol_handler::RESULT_OK) {
- *result_listener << "Error while message deserialization.";
+ if (ExpectedFrameData == FRAME_DATA_START_SERVICE_NACK &&
+ ExpectedEncryption) {
+ *result_listener << "NACK message with PROTECYION_ON flag";
return false;
}
+ const RawMessagePtr message = arg;
+ ProtocolPacket packet(message->connection_key());
+ const RESULT_CODE result =
+ packet.deserializePacket(message->data(), message->data_size());
- if (::protocol_handler::FRAME_TYPE_CONTROL != packet.frame_type()) {
- *result_listener << "Is not control message";
+ if (!CheckRegularMatches(packet,
+ result,
+ *result_listener,
+ FRAME_TYPE_CONTROL,
+ ExpectedFrameData,
+ ExpectedEncryption)) {
return false;
}
- if (ExpectedFrameData != packet.frame_data()) {
- *result_listener << "Control message with data 0x"
- << std::hex << static_cast<int>(packet.frame_data())
- << ", not 0x"
- << std::hex << static_cast<int>(ExpectedFrameData);
+ return true;
+}
+
+MATCHER_P4(ControlMessage,
+ ExpectedFrameData,
+ ExpectedEncryption,
+ ConnectionKey,
+ VectorMatcher,
+ (std::string(ExpectedEncryption ? "Protected" : "Unprotected") +
+ " control message ")) {
+ // Nack shall be always with flag protected off
+ if (ExpectedFrameData == FRAME_DATA_START_SERVICE_NACK &&
+ ExpectedEncryption) {
+ *result_listener << "NACK message with PROTECYION_ON flag";
return false;
}
- if (ExpectedEncryption != packet.protection_flag()) {
- *result_listener << "Control message is " <<
- (ExpectedEncryption ? "" : "not ") << "protected";
+ const RawMessagePtr message = arg;
+ ProtocolPacket packet(message->connection_key());
+ const RESULT_CODE result =
+ packet.deserializePacket(message->data(), message->data_size());
+
+ if (!CheckRegularMatches(packet,
+ result,
+ *result_listener,
+ FRAME_TYPE_CONTROL,
+ ExpectedFrameData,
+ ExpectedEncryption)) {
return false;
}
+
if (ConnectionKey != message->connection_key()) {
- *result_listener << "Message for connection_id " << message->connection_key() <<
- ", expected for connection_id " << ConnectionKey;
+ *result_listener << "Message for connection_id "
+ << message->connection_key()
+ << ", expected for connection_id " << ConnectionKey;
return false;
}
std::vector<uint8_t> data_vector;
@@ -128,17 +153,55 @@ MATCHER_P4(ControlMessage, ExpectedFrameData, ExpectedEncryption,
*result_listener << "Message with " << data_vector.size()
<< " byte data : 0x";
for (size_t i = 0u; i < data_vector.size(); ++i) {
- *result_listener << std::hex << static_cast<int>(data_vector[i]);
+ *result_listener << std::hex << static_cast<int>(data_vector[i]);
}
return false;
}
return true;
}
+/*
+ * Matcher for checking RawMessage with any ExpectedMessage
+ */
+MATCHER_P4(ExpectedMessage,
+ ExpectedFrameType,
+ ExpectedFrameData,
+ ExpectedEncryption,
+ ExpectedServiceType,
+ (std::string(ExpectedEncryption ? "Protected" : "Unprotected") +
+ " message ")) {
+ // Nack shall be always with flag protected off
+ if (ExpectedFrameType == FRAME_TYPE_CONTROL &&
+ ExpectedFrameData == FRAME_DATA_START_SERVICE_NACK &&
+ ExpectedEncryption) {
+ *result_listener << "NACK message with PROTECYION_ON flag";
+ return false;
+ }
+ const RawMessagePtr message = arg;
+ ProtocolPacket packet(message->connection_key());
+ const RESULT_CODE result =
+ packet.deserializePacket(message->data(), message->data_size());
+ if (!CheckRegularMatches(packet,
+ result,
+ *result_listener,
+ ExpectedFrameType,
+ ExpectedFrameData,
+ ExpectedEncryption)) {
+ return false;
+ }
+ if (ExpectedServiceType != packet.service_type()) {
+ *result_listener << "Service type is 0x" << std::hex
+ << static_cast<int>(packet.service_type()) << ", not 0x"
+ << std::hex << static_cast<int>(ExpectedServiceType);
+ return false;
+ }
+ return true;
+}
} // namespace protocol_handler_test
} // namespace components
} // namespace test
-#endif // TEST_COMPONENTS_INCLUDE_PROTOCOL_HANDLER_CONTROL_MESSAGE_MATCHER_H_
+
+#endif // SRC_COMPONENTS_PROTOCOL_HANDLER_TEST_INCLUDE_PROTOCOL_HANDLER_CONTROL_MESSAGE_MATCHER_H_
diff --git a/src/components/protocol_handler/test/multiframe_builder_test.cc b/src/components/protocol_handler/test/multiframe_builder_test.cc
index d1910c18e1..3f7a22ec95 100644
--- a/src/components/protocol_handler/test/multiframe_builder_test.cc
+++ b/src/components/protocol_handler/test/multiframe_builder_test.cc
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2015, Ford Motor Company
+ * Copyright (c) 2016, Ford Motor Company
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -29,8 +29,10 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
-#include <gtest/gtest.h>
+#include "gtest/gtest.h"
#include <vector>
+#include <map>
+#include <algorithm>
#include <utility>
#include <limits>
#include "utils/make_shared.h"
@@ -42,8 +44,8 @@ namespace protocol_handler_test {
using namespace protocol_handler;
-typedef std::vector<ConnectionID> ConnectionList;
-typedef std::vector<uint8_t> UCharDataVector;
+typedef std::vector<ConnectionID> ConnectionList;
+typedef std::vector<uint8_t> UCharDataVector;
struct MutiframeData {
UCharDataVector binary_data;
@@ -57,44 +59,40 @@ typedef std::map<MessageID, MutiframeData> MessageIDToMutiframeDataTestMap;
/**
*\brief Map of MessageIDToMutiframeDataMap by SessionID key
*/
-typedef std::map<SessionID, MessageIDToMutiframeDataTestMap> SessionToMutiframeDataTestMap;
+typedef std::map<SessionID, MessageIDToMutiframeDataTestMap>
+ SessionToMutiframeDataTestMap;
/**
*\brief Map of SessionToMutiframeDataMap by ConnectionID key
*/
typedef std::map<ConnectionID, SessionToMutiframeDataTestMap> MultiFrameTestMap;
-template<typename IntegerType>
-std::vector<IntegerType> getTestVector() {
+template <typename IntegerType>
+std::vector<IntegerType> GetTestVector() {
// Prepare array with a few minimals, middle and a few maximum values
- const IntegerType array[] = {
- std::numeric_limits<IntegerType>::min(),
- std::numeric_limits<IntegerType>::min() + 1,
- std::numeric_limits<IntegerType>::max() / 2,
- std::numeric_limits<IntegerType>::max() - 1 ,
- std::numeric_limits<IntegerType>::max()
- };
+ const IntegerType array[] = {std::numeric_limits<IntegerType>::min(),
+ std::numeric_limits<IntegerType>::min() + 1,
+ std::numeric_limits<IntegerType>::max() / 2,
+ std::numeric_limits<IntegerType>::max() - 1,
+ std::numeric_limits<IntegerType>::max()};
return std::vector<IntegerType>(array,
- array + sizeof(array) / sizeof(array[0]) );
+ array + sizeof(array) / sizeof(array[0]));
}
-template<typename IntegerType>
+
+template <typename IntegerType>
struct Incrementor {
IntegerType value;
- Incrementor(const IntegerType value = 0u)
- : value(value) {
- }
- IntegerType operator() () {
+ explicit Incrementor(const IntegerType value = 0u) : value(value) {}
+ IntegerType operator()() {
return ++value;
}
};
-
class MultiFrameBuilderTest : public ::testing::Test {
protected:
void SetUp() OVERRIDE {
-
- const std::vector<ConnectionID> connections = getTestVector<ConnectionID>();
- const std::vector<SessionID> sessions = getTestVector<SessionID>();
- const std::vector<MessageID> messages = getTestVector<MessageID>();
+ const std::vector<ConnectionID> connections = GetTestVector<ConnectionID>();
+ const std::vector<SessionID> sessions = GetTestVector<SessionID>();
+ const std::vector<MessageID> messages = GetTestVector<MessageID>();
MutiframeData some_data;
@@ -121,16 +119,17 @@ class MultiFrameBuilderTest : public ::testing::Test {
ASSERT_GT(multi_frames_count, 1);
data_vector.resize(++multi_frames_count * mtu_);
- std::generate(data_vector.begin(), data_vector.end(), Incrementor<uint8_t>(0u));
+ std::generate(
+ data_vector.begin(), data_vector.end(), Incrementor<uint8_t>(0u));
PrepareMultiFrames(connection_id,
- protocol_version,
- service_type,
- session_id,
- message_id,
- mtu_,
- data_vector,
- some_data.multiframes);
+ protocol_version,
+ service_type,
+ session_id,
+ message_id,
+ mtu_,
+ data_vector,
+ some_data.multiframes);
messages_map.insert(std::make_pair(message_id, some_data));
}
sessions_map.insert(std::make_pair(session_id, messages_map));
@@ -139,22 +138,127 @@ class MultiFrameBuilderTest : public ::testing::Test {
}
}
- // Support method for first and consecutive frame disassembling
- static void PrepareMultiFrames(
- const ConnectionID connection_id,
- const uint8_t protocol_version,
- const uint8_t service_type,
- const uint8_t session_id,
- const uint32_t message_id,
- const size_t max_frame_size,
- const UCharDataVector& data,
- ProtocolFramePtrList& out_frames);
+ void VerifyConsecutiveAdd(const MutiframeData& multiframe_data) {
+ const ProtocolFramePtrList& multiframes = multiframe_data.multiframes;
+ const UCharDataVector& binary_data = multiframe_data.binary_data;
+ ASSERT_FALSE(multiframes.empty());
- void AddConnection(const ConnectionID connection_id);
+ // Frame of multiframe loop
+ ProtocolFramePtrList::const_iterator it = multiframes.begin();
+ // Skip last final frame
+ const ProtocolFramePtrList::const_iterator it_last = --(multiframes.end());
+ while (it != it_last) {
+ const ProtocolFramePtr frame = *it;
+ ASSERT_TRUE(frame);
+ EXPECT_EQ(RESULT_OK, multiframe_builder_.AddFrame(frame))
+ << "Non final CONSECUTIVE frame: " << frame;
+ EXPECT_EQ(ProtocolFramePtrList(), multiframe_builder_.PopMultiframes())
+ << "Non final CONSECUTIVE frame: " << frame;
+ ++it;
+ // Skip last final frame
+ }
+
+ const ProtocolFramePtr final_frame = multiframes.back();
- void AddConnections();
+ EXPECT_EQ(RESULT_OK, multiframe_builder_.AddFrame(final_frame))
+ << "Final CONSECUTIVE frame: " << final_frame;
- void VerifyConsecutiveAdd(const MutiframeData& multiframe_data);
+ const ProtocolFramePtrList& multiframe_list =
+ multiframe_builder_.PopMultiframes();
+ ASSERT_EQ(1u, multiframe_list.size());
+
+ const ProtocolFramePtr result_multiframe = multiframe_list.front();
+ EXPECT_EQ(binary_data,
+ UCharDataVector(result_multiframe->data(),
+ result_multiframe->data() +
+ result_multiframe->payload_size()));
+ }
+
+ // Support method for first and consecutive frame disassembling
+ void PrepareMultiFrames(const ConnectionID connection_id,
+ const uint8_t protocol_version,
+ const uint8_t service_type,
+ const uint8_t session_id,
+ const uint32_t message_id,
+ const size_t max_payload_size,
+ const UCharDataVector& data,
+ ProtocolFramePtrList& out_frames) {
+ ASSERT_LT(FIRST_FRAME_DATA_SIZE, max_payload_size);
+ ASSERT_EQ(0x08, FIRST_FRAME_DATA_SIZE)
+ << "Size of FIRST_FRAME_DATA: " << FIRST_FRAME_DATA_SIZE
+ << ", it must be only 0x08";
+
+ // TODO(EZamakhov): move to the separate class
+ const size_t data_size = data.size();
+ // remainder of last frame
+ const size_t lastframe_remainder = data_size % max_payload_size;
+ // size of last frame (full fill or not)
+ const size_t lastframe_size =
+ lastframe_remainder > 0 ? lastframe_remainder : max_payload_size;
+
+ const size_t frames_count = data_size / max_payload_size +
+ // add last frame if not empty
+ (lastframe_remainder > 0 ? 1 : 0);
+
+ uint8_t out_data[FIRST_FRAME_DATA_SIZE];
+ out_data[0] = data_size >> 24;
+ out_data[1] = data_size >> 16;
+ out_data[2] = data_size >> 8;
+ out_data[3] = data_size;
+
+ out_data[4] = frames_count >> 24;
+ out_data[5] = frames_count >> 16;
+ out_data[6] = frames_count >> 8;
+ out_data[7] = frames_count;
+
+ ProtocolFramePtr first_frame(new ProtocolPacket(connection_id,
+ protocol_version,
+ PROTECTION_OFF,
+ FRAME_TYPE_FIRST,
+ service_type,
+ FRAME_DATA_FIRST,
+ session_id,
+ FIRST_FRAME_DATA_SIZE,
+ message_id,
+ out_data));
+ // Note: PHIMpl already prepare First frames the total_data_bytes on
+ // desirialization
+ first_frame->set_total_data_bytes(data_size);
+
+ out_frames.clear();
+ out_frames.push_back(first_frame);
+
+ for (size_t i = 0; i < frames_count; ++i) {
+ const bool is_last_frame = ((frames_count - 1) == i);
+ const size_t frame_size =
+ is_last_frame ? lastframe_size : max_payload_size;
+ const uint8_t data_type = is_last_frame
+ ? FRAME_DATA_LAST_CONSECUTIVE
+ : (i % FRAME_DATA_MAX_CONSECUTIVE + 1);
+
+ const ProtocolFramePtr consecutive_frame(
+ new ProtocolPacket(connection_id,
+ protocol_version,
+ PROTECTION_OFF,
+ FRAME_TYPE_CONSECUTIVE,
+ service_type,
+ data_type,
+ session_id,
+ frame_size,
+ message_id,
+ &data[max_payload_size * i]));
+ out_frames.push_back(consecutive_frame);
+ }
+ }
+
+ void AddConnections() {
+ for (MultiFrameTestMap::iterator connection_it = test_data_map_.begin();
+ connection_it != test_data_map_.end();
+ ++connection_it) {
+ const ConnectionID connection_id = connection_it->first;
+ ASSERT_TRUE(multiframe_builder_.AddConnection(connection_id));
+ }
+ }
MultiFrameBuilder multiframe_builder_;
MultiFrameTestMap test_data_map_;
@@ -164,30 +268,26 @@ class MultiFrameBuilderTest : public ::testing::Test {
size_t MultiFrameBuilderTest::mtu_ = 10;
TEST_F(MultiFrameBuilderTest, Pop_Frames_From_Empty_builder) {
- EXPECT_EQ(ProtocolFramePtrList(),
- multiframe_builder_.PopMultiframes());
+ EXPECT_EQ(ProtocolFramePtrList(), multiframe_builder_.PopMultiframes());
}
TEST_F(MultiFrameBuilderTest, Pop_Frames_with_existing_connections) {
AddConnections();
- EXPECT_EQ(ProtocolFramePtrList(),
- multiframe_builder_.PopMultiframes());
+ EXPECT_EQ(ProtocolFramePtrList(), multiframe_builder_.PopMultiframes());
}
TEST_F(MultiFrameBuilderTest, Add_EmptyFrame) {
- EXPECT_EQ(RESULT_FAIL,
- multiframe_builder_.AddFrame(ProtocolFramePtr()));
- EXPECT_EQ(ProtocolFramePtrList(),
- multiframe_builder_.PopMultiframes());
+ EXPECT_EQ(RESULT_FAIL, multiframe_builder_.AddFrame(ProtocolFramePtr()));
+ EXPECT_EQ(ProtocolFramePtrList(), multiframe_builder_.PopMultiframes());
}
TEST_F(MultiFrameBuilderTest, Add_NonSingleOrConsecutive_Frames) {
UCharDataVector types;
types.reserve(std::numeric_limits<uint8_t>::max());
for (uint8_t type = std::numeric_limits<uint8_t>::min();
- type < std::numeric_limits<uint8_t>::max(); ++type) {
- if (type != FRAME_TYPE_FIRST &&
- type != FRAME_TYPE_CONSECUTIVE) {
+ type < std::numeric_limits<uint8_t>::max();
+ ++type) {
+ if (type != FRAME_TYPE_FIRST && type != FRAME_TYPE_CONSECUTIVE) {
types.push_back(type);
}
}
@@ -195,43 +295,51 @@ TEST_F(MultiFrameBuilderTest, Add_NonSingleOrConsecutive_Frames) {
for (UCharDataVector::iterator it = types.begin(); it != types.end(); ++it) {
const uint8_t frame_type = *it;
const ProtocolFramePtr unexpected_frame(
- new ProtocolPacket( 0u, PROTOCOL_VERSION_3, PROTECTION_OFF, frame_type,
- SERVICE_TYPE_RPC, FRAME_DATA_FIRST, 0u, 0u, 0u));
- EXPECT_EQ(RESULT_FAIL,
- multiframe_builder_.AddFrame(unexpected_frame))
+ new ProtocolPacket(0u,
+ PROTOCOL_VERSION_3,
+ PROTECTION_OFF,
+ frame_type,
+ SERVICE_TYPE_RPC,
+ FRAME_DATA_FIRST,
+ 0u,
+ 0u,
+ 0u));
+ EXPECT_EQ(RESULT_FAIL, multiframe_builder_.AddFrame(unexpected_frame))
<< "Unexpected frame: " << unexpected_frame;
- EXPECT_EQ(ProtocolFramePtrList(),
- multiframe_builder_.PopMultiframes())
+ EXPECT_EQ(ProtocolFramePtrList(), multiframe_builder_.PopMultiframes())
<< "Unexpected frame: " << unexpected_frame;
}
}
TEST_F(MultiFrameBuilderTest, Add_FirstFrames_NoConnections) {
for (MultiFrameTestMap::iterator connection_it = test_data_map_.begin();
- connection_it != test_data_map_.end(); ++connection_it) {
+ connection_it != test_data_map_.end();
+ ++connection_it) {
SessionToMutiframeDataTestMap& session_map = connection_it->second;
- const ConnectionID connection_id = connection_it->first;
+ const ConnectionID connection_id = connection_it->first;
- for (SessionToMutiframeDataTestMap::iterator session_it = session_map.begin();
- session_it != session_map.end(); ++session_it) {
+ for (SessionToMutiframeDataTestMap::iterator session_it =
+ session_map.begin();
+ session_it != session_map.end();
+ ++session_it) {
MessageIDToMutiframeDataTestMap& messageId_map = session_it->second;
- for (MessageIDToMutiframeDataTestMap::iterator messageId_it = messageId_map.begin();
- messageId_it != messageId_map.end(); ++messageId_it) {
+ for (MessageIDToMutiframeDataTestMap::iterator messageId_it =
+ messageId_map.begin();
+ messageId_it != messageId_map.end();
+ ++messageId_it) {
const MutiframeData& multiframe_data = messageId_it->second;
const ProtocolFramePtrList& multiframes = multiframe_data.multiframes;
ASSERT_FALSE(multiframes.empty());
const ProtocolFramePtr first_frame = multiframes.front();
ASSERT_TRUE(first_frame);
- EXPECT_EQ(RESULT_FAIL,
- multiframe_builder_.AddFrame(first_frame))
+ EXPECT_EQ(RESULT_FAIL, multiframe_builder_.AddFrame(first_frame))
<< "Unexisting connection " << connection_id
<< "- to be skipped first frame: " << first_frame;
- EXPECT_EQ(ProtocolFramePtrList(),
- multiframe_builder_.PopMultiframes())
+ EXPECT_EQ(ProtocolFramePtrList(), multiframe_builder_.PopMultiframes())
<< "First frame: " << first_frame;
}
}
@@ -241,27 +349,30 @@ TEST_F(MultiFrameBuilderTest, Add_FirstFrames_NoConnections) {
TEST_F(MultiFrameBuilderTest, Add_FirstFrames_only) {
AddConnections();
for (MultiFrameTestMap::iterator connection_it = test_data_map_.begin();
- connection_it != test_data_map_.end(); ++connection_it) {
+ connection_it != test_data_map_.end();
+ ++connection_it) {
SessionToMutiframeDataTestMap& session_map = connection_it->second;
- for (SessionToMutiframeDataTestMap::iterator session_it = session_map.begin();
- session_it != session_map.end(); ++session_it) {
+ for (SessionToMutiframeDataTestMap::iterator session_it =
+ session_map.begin();
+ session_it != session_map.end();
+ ++session_it) {
MessageIDToMutiframeDataTestMap& messageId_map = session_it->second;
- for (MessageIDToMutiframeDataTestMap::iterator messageId_it = messageId_map.begin();
- messageId_it != messageId_map.end(); ++messageId_it) {
+ for (MessageIDToMutiframeDataTestMap::iterator messageId_it =
+ messageId_map.begin();
+ messageId_it != messageId_map.end();
+ ++messageId_it) {
const MutiframeData& multiframe_data = messageId_it->second;
const ProtocolFramePtrList& multiframes = multiframe_data.multiframes;
ASSERT_FALSE(multiframes.empty());
const ProtocolFramePtr first_frame = multiframes.front();
ASSERT_TRUE(first_frame);
- EXPECT_EQ(RESULT_OK,
- multiframe_builder_.AddFrame(first_frame))
+ EXPECT_EQ(RESULT_OK, multiframe_builder_.AddFrame(first_frame))
<< "First frame: " << first_frame;
- EXPECT_EQ(ProtocolFramePtrList(),
- multiframe_builder_.PopMultiframes())
+ EXPECT_EQ(ProtocolFramePtrList(), multiframe_builder_.PopMultiframes())
<< "First frame: " << first_frame;
}
}
@@ -270,11 +381,10 @@ TEST_F(MultiFrameBuilderTest, Add_FirstFrames_only) {
TEST_F(MultiFrameBuilderTest, Add_ConsecutiveFrame) {
ASSERT_FALSE(test_data_map_.empty());
- const ConnectionID& connection_id = test_data_map_.begin()->first;
+ const ConnectionID& connection_id = test_data_map_.begin()->first;
SessionToMutiframeDataTestMap& session_map = test_data_map_.begin()->second;
- AddConnection(connection_id);
-
+ ASSERT_TRUE(multiframe_builder_.AddConnection(connection_id));
ASSERT_FALSE(session_map.empty());
MessageIDToMutiframeDataTestMap& messageId_map = session_map.begin()->second;
@@ -287,15 +397,20 @@ TEST_F(MultiFrameBuilderTest, Add_ConsecutiveFrame) {
TEST_F(MultiFrameBuilderTest, Add_ConsecutiveFrames_OneByOne) {
AddConnections();
for (MultiFrameTestMap::iterator connection_it = test_data_map_.begin();
- connection_it != test_data_map_.end(); ++connection_it) {
+ connection_it != test_data_map_.end();
+ ++connection_it) {
SessionToMutiframeDataTestMap& session_map = connection_it->second;
- for (SessionToMutiframeDataTestMap::iterator session_it = session_map.begin();
- session_it != session_map.end(); ++session_it) {
+ for (SessionToMutiframeDataTestMap::iterator session_it =
+ session_map.begin();
+ session_it != session_map.end();
+ ++session_it) {
MessageIDToMutiframeDataTestMap& messageId_map = session_it->second;
- for (MessageIDToMutiframeDataTestMap::iterator messageId_it = messageId_map.begin();
- messageId_it != messageId_map.end(); ++messageId_it) {
+ for (MessageIDToMutiframeDataTestMap::iterator messageId_it =
+ messageId_map.begin();
+ messageId_it != messageId_map.end();
+ ++messageId_it) {
const MutiframeData& multiframe_data = messageId_it->second;
VerifyConsecutiveAdd(multiframe_data);
@@ -320,38 +435,39 @@ TEST_F(MultiFrameBuilderTest, Add_ConsecutiveFrames_per1) {
while (session_it != session_map.end()) {
MessageIDToMutiframeDataTestMap& messageId_map = session_it->second;
- MessageIDToMutiframeDataTestMap::iterator messageId_it = messageId_map.begin();
+ MessageIDToMutiframeDataTestMap::iterator messageId_it =
+ messageId_map.begin();
while (messageId_it != messageId_map.end()) {
-
- MutiframeData& multiframe_data = messageId_it->second;
+ MutiframeData& multiframe_data = messageId_it->second;
ProtocolFramePtrList& multiframes = multiframe_data.multiframes;
ASSERT_FALSE(multiframes.empty());
const ProtocolFramePtr frame = multiframes.front();
ASSERT_TRUE(frame);
- EXPECT_EQ(RESULT_OK,
- multiframe_builder_.AddFrame(frame)) << "Frame: " << frame;
+ EXPECT_EQ(RESULT_OK, multiframe_builder_.AddFrame(frame))
+ << "Frame: " << frame;
multiframes.pop_front();
// If all frames are assembled
if (multiframes.empty()) {
- const ProtocolFramePtrList& multiframe_list
- = multiframe_builder_.PopMultiframes();
- ASSERT_EQ(multiframe_list.size(), 1u);
+ const ProtocolFramePtrList& multiframe_list =
+ multiframe_builder_.PopMultiframes();
+ ASSERT_EQ(1u, multiframe_list.size());
const ProtocolFramePtr result_multiframe = multiframe_list.front();
- const UCharDataVector& binary_data = multiframe_data.binary_data;
+ const UCharDataVector& binary_data = multiframe_data.binary_data;
EXPECT_EQ(binary_data,
UCharDataVector(result_multiframe->data(),
result_multiframe->data() +
- result_multiframe->payload_size()));
+ result_multiframe->payload_size()));
messageId_map.erase(messageId_it++);
} else {
// Multiframe is not completed
EXPECT_EQ(ProtocolFramePtrList(),
- multiframe_builder_.PopMultiframes()) << "Frame: " << frame;
+ multiframe_builder_.PopMultiframes())
+ << "Frame: " << frame;
++messageId_it;
}
}
@@ -374,10 +490,10 @@ TEST_F(MultiFrameBuilderTest, FrameExpired_OneMSec) {
multiframe_builder_.set_waiting_timeout(1);
ASSERT_FALSE(test_data_map_.empty());
- const ConnectionID& connection_id = test_data_map_.begin()->first;
+ const ConnectionID& connection_id = test_data_map_.begin()->first;
SessionToMutiframeDataTestMap& session_map = test_data_map_.begin()->second;
- AddConnection(connection_id);
+ ASSERT_TRUE(multiframe_builder_.AddConnection(connection_id));
ASSERT_FALSE(session_map.empty());
MessageIDToMutiframeDataTestMap& messageId_map = session_map.begin()->second;
@@ -389,132 +505,37 @@ TEST_F(MultiFrameBuilderTest, FrameExpired_OneMSec) {
ASSERT_FALSE(multiframes.empty());
const ProtocolFramePtr first_frame = multiframes.front();
ASSERT_TRUE(first_frame);
- EXPECT_EQ(RESULT_OK,
- multiframe_builder_.AddFrame(first_frame))
+ EXPECT_EQ(RESULT_OK, multiframe_builder_.AddFrame(first_frame))
<< "First frame: " << first_frame;
// Wait frame expire
usleep(1000);
const ProtocolFramePtrList& list = multiframe_builder_.PopMultiframes();
ASSERT_FALSE(list.empty());
- EXPECT_EQ(first_frame,
- list.front());
+ EXPECT_EQ(first_frame, list.front());
}
-/*
- * Testing support methods
- */
-
-void MultiFrameBuilderTest::VerifyConsecutiveAdd(const MutiframeData& multiframe_data) {
- const ProtocolFramePtrList& multiframes = multiframe_data.multiframes;
- const UCharDataVector& binary_data = multiframe_data.binary_data;
- ASSERT_FALSE(multiframes.empty());
-
- // Frame of multiframe loop
- ProtocolFramePtrList::const_iterator it = multiframes.begin();
- // Skip last final frame
- const ProtocolFramePtrList::const_iterator it_last = --(multiframes.end());
- while (it != it_last) {
- const ProtocolFramePtr frame = *it;
- ASSERT_TRUE(frame);
- EXPECT_EQ(RESULT_OK,
- multiframe_builder_.AddFrame(frame))
- << "Non final CONSECUTIVE frame: " << frame;
- EXPECT_EQ(ProtocolFramePtrList(),
- multiframe_builder_.PopMultiframes())
- << "Non final CONSECUTIVE frame: " << frame;
- ++it;
- // Skip last final frame
- }
-
- const ProtocolFramePtr final_frame = multiframes.back();
-
- EXPECT_EQ(RESULT_OK,
- multiframe_builder_.AddFrame(final_frame))
- << "Final CONSECUTIVE frame: " << final_frame;
-
- const ProtocolFramePtrList& multiframe_list
- = multiframe_builder_.PopMultiframes();
- ASSERT_EQ(multiframe_list.size(), 1u);
-
- const ProtocolFramePtr result_multiframe = multiframe_list.front();
- EXPECT_EQ(binary_data,
- UCharDataVector(result_multiframe->data(),
- result_multiframe->data() + result_multiframe->payload_size()));
-}
-
-void MultiFrameBuilderTest::PrepareMultiFrames(const ConnectionID connection_id,
- const uint8_t protocol_version,
- const uint8_t service_type,
- const uint8_t session_id,
- const uint32_t message_id,
- const size_t max_payload_size,
- const UCharDataVector& data,
- ProtocolFramePtrList& out_frames) {
- ASSERT_GT(max_payload_size, FIRST_FRAME_DATA_SIZE);
- ASSERT_EQ(FIRST_FRAME_DATA_SIZE, 0x08);
-
- // TODO(EZamakhov): move to the separate class
- const size_t data_size = data.size();
- // remainder of last frame
- const size_t lastframe_remainder = data_size % max_payload_size;
- // size of last frame (full fill or not)
- const size_t lastframe_size =
- lastframe_remainder > 0 ? lastframe_remainder : max_payload_size;
-
- const size_t frames_count = data_size / max_payload_size +
- // add last frame if not empty
- (lastframe_remainder > 0 ? 1 : 0);
-
- uint8_t out_data[FIRST_FRAME_DATA_SIZE];
- out_data[0] = data_size >> 24;
- out_data[1] = data_size >> 16;
- out_data[2] = data_size >> 8;
- out_data[3] = data_size;
-
- out_data[4] = frames_count >> 24;
- out_data[5] = frames_count >> 16;
- out_data[6] = frames_count >> 8;
- out_data[7] = frames_count;
-
- ProtocolFramePtr first_frame(
- new ProtocolPacket(
- connection_id, protocol_version, PROTECTION_OFF, FRAME_TYPE_FIRST,
- service_type, FRAME_DATA_FIRST, session_id, FIRST_FRAME_DATA_SIZE,
- message_id, out_data));
- // Note: PHIMpl already prepare First frames the total_data_bytes on desirialization
- first_frame->set_total_data_bytes(data_size);
-
- out_frames.clear();
- out_frames.push_back(first_frame);
-
- for (size_t i = 0; i < frames_count; ++i) {
- const bool is_last_frame = (i == (frames_count - 1));
- const size_t frame_size = is_last_frame ? lastframe_size : max_payload_size;
- const uint8_t data_type =
- is_last_frame
- ? FRAME_DATA_LAST_CONSECUTIVE
- : (i % FRAME_DATA_MAX_CONSECUTIVE + 1);
-
- const ProtocolFramePtr consecutive_frame(
- new ProtocolPacket(
- connection_id, protocol_version, PROTECTION_OFF, FRAME_TYPE_CONSECUTIVE,
- service_type, data_type, session_id, frame_size, message_id,
- &data[max_payload_size * i]));
- out_frames.push_back(consecutive_frame);
- }
+TEST_F(MultiFrameBuilderTest, RemoveConnection_NoConnection_ResultFail) {
+ // Arrange
+ const ConnectionID& connection_id = test_data_map_.begin()->first;
+ // Act
+ const bool connection_result =
+ multiframe_builder_.RemoveConnection(connection_id);
+ // Assert
+ ASSERT_FALSE(connection_result);
}
-void MultiFrameBuilderTest::AddConnection(const ConnectionID connection_id) {
+TEST_F(MultiFrameBuilderTest, RemoveConnection_Successful) {
+ // Arrange
+ const ConnectionID& connection_id = test_data_map_.begin()->first;
+ // Variable test_data_map initially contains IDs of not existed in
+ // MultiframeBuilder connections
ASSERT_TRUE(multiframe_builder_.AddConnection(connection_id));
-}
-
-void MultiFrameBuilderTest::AddConnections() {
- for (MultiFrameTestMap::iterator connection_it = test_data_map_.begin();
- connection_it != test_data_map_.end(); ++connection_it) {
- const ConnectionID connection_id = connection_it->first;
- ASSERT_TRUE(multiframe_builder_.AddConnection(connection_id));
- }
+ // Act
+ const bool connection_result =
+ multiframe_builder_.RemoveConnection(connection_id);
+ // Assert
+ ASSERT_TRUE(connection_result);
}
} // namespace protocol_handler_test
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 f445e73405..1982fc7d48 100644
--- a/src/components/protocol_handler/test/protocol_handler_tm_test.cc
+++ b/src/components/protocol_handler/test/protocol_handler_tm_test.cc
@@ -43,7 +43,6 @@
#include "security_manager/mock_security_manager.h"
#include "security_manager/mock_ssl_context.h"
#include "transport_manager/mock_transport_manager.h"
-
#include "utils/make_shared.h"
namespace test {
@@ -53,24 +52,33 @@ namespace protocol_handler_test {
// Id passed as NULL for new session establishing
#define NEW_SESSION_ID 0u
#define SESSION_START_REJECT 0u
-
+// Protocol Handler Entities
+using protocol_handler::ProtocolHandlerImpl;
using protocol_handler::ServiceType;
using protocol_handler::RawMessage;
using protocol_handler::RawMessagePtr;
using protocol_handler::PROTECTION_ON;
using protocol_handler::PROTECTION_OFF;
using protocol_handler::PROTOCOL_VERSION_1;
+using protocol_handler::PROTOCOL_VERSION_2;
using protocol_handler::PROTOCOL_VERSION_3;
using protocol_handler::PROTOCOL_VERSION_MAX;
using protocol_handler::FRAME_TYPE_CONTROL;
using protocol_handler::FRAME_TYPE_SINGLE;
+using protocol_handler::FRAME_TYPE_FIRST;
+using protocol_handler::FRAME_TYPE_CONSECUTIVE;
using protocol_handler::FRAME_TYPE_MAX_VALUE;
+using protocol_handler::MAXIMUM_FRAME_DATA_V2_SIZE;
using protocol_handler::FRAME_DATA_START_SERVICE;
using protocol_handler::FRAME_DATA_START_SERVICE_ACK;
+using protocol_handler::FRAME_DATA_END_SERVICE_NACK;
+using protocol_handler::FRAME_DATA_END_SERVICE_ACK;
using protocol_handler::FRAME_DATA_END_SERVICE;
using protocol_handler::FRAME_DATA_HEART_BEAT;
using protocol_handler::FRAME_DATA_HEART_BEAT_ACK;
using protocol_handler::FRAME_DATA_SINGLE;
+using protocol_handler::FRAME_DATA_FIRST;
+using protocol_handler::FRAME_DATA_LAST_CONSECUTIVE;
using protocol_handler::kRpc;
using protocol_handler::kControl;
using protocol_handler::kAudio;
@@ -80,7 +88,10 @@ using protocol_handler::kInvalidServiceType;
// For TM states
using transport_manager::TransportManagerListener;
using transport_manager::E_SUCCESS;
-
+using transport_manager::DeviceInfo;
+// For CH entities
+using connection_handler::DeviceHandle;
+// Google Testing Framework Entities
using ::testing::Return;
using ::testing::ReturnRefOfCopy;
using ::testing::ReturnNull;