Add agent data wrapping functions 85/33385/5
authorLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Fri, 9 Jan 2015 01:41:17 +0000 (02:41 +0100)
committerLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Thu, 15 Jan 2015 18:27:00 +0000 (19:27 +0100)
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
tests/cynara-tests/plugins/multiple-policy/CMakeLists.txt
tests/cynara-tests/plugins/plugins.cpp [new file with mode: 0644]
tests/cynara-tests/plugins/plugins.h
tests/cynara-tests/plugins/single-policy/CMakeLists.txt

index 94aeb6e..f30b62a 100644 (file)
@@ -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
index 7f9afb2..48ae2ae 100644 (file)
@@ -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 (file)
index 0000000..74bf7ae
--- /dev/null
@@ -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 <l.wojciechow@partner.samsung.com>
+ * @brief       Definition of types, constants and functions common for both tests and plugins
+ */
+
+#include <vector>
+#include <sstream>
+#include <string>
+
+#include <plugins.h>
+
+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
index 9e4f122..edb1f5c 100644 (file)
@@ -16,7 +16,7 @@
 /**
  * @file        plugins.h
  * @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
- * @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 <map>
 #include <string>
 
+#include <cynara-plugin.h>
 #include <cynara/types/PolicyDescription.h>
 #include <cynara/types/PolicyType.h>
 
@@ -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<std::string> AgentDataVector;
+
+Cynara::PluginData wrapAgentData(const AgentDataVector &data);
+bool unwrapAgentData(const Cynara::PluginData &wrappedData, AgentDataVector& data);
+
 } // namespace CynaraTestPlugins
 
 #endif // CYNARA_TEST_PLUGINS_H
index d82c9f4..818f733 100644 (file)
@@ -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")