From 8eac46d9c9e56b6164cc7b5c4427d5b5520b15bf Mon Sep 17 00:00:00 2001 From: Lukasz Wojciechowski Date: Fri, 9 Jan 2015 02:41:17 +0100 Subject: [PATCH] Add agent data wrapping functions Add functions for easy joining and spliting data strings used in communication between agent and plugins: * wrapAgentData; * unwrapAgentData. This functions are defined in new file plugins.cpp - a common file for both agent and plugins. Change-Id: I29494928d752832aef7c8e14204a7e4ce63a911e --- tests/cynara-tests/CMakeLists.txt | 1 + .../plugins/multiple-policy/CMakeLists.txt | 1 + tests/cynara-tests/plugins/plugins.cpp | 79 ++++++++++++++++++++++ tests/cynara-tests/plugins/plugins.h | 13 +++- .../plugins/single-policy/CMakeLists.txt | 1 + 5 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 tests/cynara-tests/plugins/plugins.cpp diff --git a/tests/cynara-tests/CMakeLists.txt b/tests/cynara-tests/CMakeLists.txt index 94aeb6e..f30b62a 100644 --- a/tests/cynara-tests/CMakeLists.txt +++ b/tests/cynara-tests/CMakeLists.txt @@ -39,6 +39,7 @@ SET(CYNARA_TARGET_TEST_SOURCES ${PROJECT_SOURCE_DIR}/tests/cynara-tests/common/cynara_test_cynara_mask.cpp ${PROJECT_SOURCE_DIR}/tests/cynara-tests/common/cynara_test_env.cpp ${PROJECT_SOURCE_DIR}/tests/cynara-tests/common/cynara_test_file_operations.cpp + ${PROJECT_SOURCE_DIR}/tests/cynara-tests/plugins/plugins.cpp ${PROJECT_SOURCE_DIR}/tests/cynara-tests/cynara-test.cpp ${PROJECT_SOURCE_DIR}/tests/cynara-tests/test_cases.cpp ${PROJECT_SOURCE_DIR}/tests/cynara-tests/test_cases_async.cpp diff --git a/tests/cynara-tests/plugins/multiple-policy/CMakeLists.txt b/tests/cynara-tests/plugins/multiple-policy/CMakeLists.txt index 7f9afb2..48ae2ae 100644 --- a/tests/cynara-tests/plugins/multiple-policy/CMakeLists.txt +++ b/tests/cynara-tests/plugins/multiple-policy/CMakeLists.txt @@ -34,6 +34,7 @@ INCLUDE_DIRECTORIES( SET(CYNARA_TARGET_TEST_PLUGIN_MULTIPLE_POLICY_SOURCES ${PROJECT_SOURCE_DIR}/tests/cynara-tests/plugins/multiple-policy/plugin.cpp + ${PROJECT_SOURCE_DIR}/tests/cynara-tests/plugins/plugins.cpp ) ADD_DEFINITIONS("-fvisibility=default") diff --git a/tests/cynara-tests/plugins/plugins.cpp b/tests/cynara-tests/plugins/plugins.cpp new file mode 100644 index 0000000..74bf7ae --- /dev/null +++ b/tests/cynara-tests/plugins/plugins.cpp @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @file plugins.cpp + * @author Lukasz Wojciechowski + * @brief Definition of types, constants and functions common for both tests and plugins + */ + +#include +#include +#include + +#include + +namespace CynaraTestPlugins { + +Cynara::PluginData wrapAgentData(const AgentDataVector &data) { + std::stringstream wrappedData; + wrappedData << AGENT_DATA_RECORD_SEPARATOR; + for (size_t i = 0; i < data.size(); ++i) { + wrappedData << AGENT_DATA_UNIT_SEPARATOR + << data[i] + << AGENT_DATA_UNIT_SEPARATOR; + } + wrappedData << AGENT_DATA_RECORD_SEPARATOR; + return wrappedData.str(); +} + +static bool unwrapAgentDataFromSeparator(const Cynara::PluginData &wrappedData, + const std::string &separator, + size_t &pos, std::string &unit) { +//check if wrapped data starts with separator + size_t separatorSize = separator.size(); + if (wrappedData.compare(pos, separatorSize, separator) != 0) + return false; + +//find ending separator + size_t unitStartIndex = pos + separatorSize; + size_t endingSeparatorIndex = wrappedData.find(separator, unitStartIndex); + if (endingSeparatorIndex == std::string::npos) + return false; + +//return found unit + pos = endingSeparatorIndex + separatorSize; + size_t unitSize = endingSeparatorIndex - unitStartIndex; + unit.assign(wrappedData, unitStartIndex, unitSize); + return true; +} + +bool unwrapAgentData(const Cynara::PluginData &wrappedData, AgentDataVector& data) { + std::string record; + size_t pos = 0; + if (!unwrapAgentDataFromSeparator(wrappedData, AGENT_DATA_RECORD_SEPARATOR, pos, record)) + return false; + + pos = 0; + while (pos < record.size()) { + std::string unit; + if (!unwrapAgentDataFromSeparator(record, AGENT_DATA_UNIT_SEPARATOR, pos, unit)) + return false; + data.push_back(unit); + } + return true; +} + +} // namespace CynaraTestPlugins diff --git a/tests/cynara-tests/plugins/plugins.h b/tests/cynara-tests/plugins/plugins.h index 9e4f122..edb1f5c 100644 --- a/tests/cynara-tests/plugins/plugins.h +++ b/tests/cynara-tests/plugins/plugins.h @@ -16,7 +16,7 @@ /** * @file plugins.h * @author Lukasz Wojciechowski - * @brief Definition of policy types and description used by test plugins + * @brief Definition of types, constants and functions common for both tests and plugins */ #ifndef CYNARA_TEST_PLUGINS_H @@ -27,6 +27,7 @@ #include #include +#include #include #include @@ -57,6 +58,16 @@ static const DescriptionsMap POLICY_DESCRIPTIONS = { }), }; +static const std::string AGENT_DATA_UNIT_SEPARATOR("\31"); +static const std::string AGENT_DATA_RECORD_SEPARATOR("\30"); +static const std::string AGENT_DATA_ALLOW("Allow"); +static const std::string AGENT_DATA_DENY("Deny"); + +typedef std::vector AgentDataVector; + +Cynara::PluginData wrapAgentData(const AgentDataVector &data); +bool unwrapAgentData(const Cynara::PluginData &wrappedData, AgentDataVector& data); + } // namespace CynaraTestPlugins #endif // CYNARA_TEST_PLUGINS_H diff --git a/tests/cynara-tests/plugins/single-policy/CMakeLists.txt b/tests/cynara-tests/plugins/single-policy/CMakeLists.txt index d82c9f4..818f733 100644 --- a/tests/cynara-tests/plugins/single-policy/CMakeLists.txt +++ b/tests/cynara-tests/plugins/single-policy/CMakeLists.txt @@ -34,6 +34,7 @@ INCLUDE_DIRECTORIES( SET(CYNARA_TARGET_TEST_PLUGIN_SINGLE_POLICY_SOURCES ${PROJECT_SOURCE_DIR}/tests/cynara-tests/plugins/single-policy/plugin.cpp + ${PROJECT_SOURCE_DIR}/tests/cynara-tests/plugins/plugins.cpp ) ADD_DEFINITIONS("-fvisibility=default") -- 2.7.4