Rename /tests to /ckm to align with tizen branch
[platform/core/test/security-tests.git] / src / cynara-tests / plugins / plugins.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file        plugins.cpp
18  * @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
19  * @brief       Definition of types, constants and functions common for both tests and plugins
20  */
21
22 #include <vector>
23 #include <sstream>
24 #include <string>
25
26 #include <plugins.h>
27
28 namespace CynaraTestPlugins {
29
30 Cynara::PluginData wrapAgentData(const AgentDataVector &data) {
31     std::stringstream wrappedData;
32     wrappedData << AGENT_DATA_RECORD_SEPARATOR;
33     for (size_t i = 0; i < data.size(); ++i) {
34         wrappedData << AGENT_DATA_UNIT_SEPARATOR
35                     << data[i]
36                     << AGENT_DATA_UNIT_SEPARATOR;
37     }
38     wrappedData << AGENT_DATA_RECORD_SEPARATOR;
39     return wrappedData.str();
40 }
41
42 static bool unwrapAgentDataFromSeparator(const Cynara::PluginData &wrappedData,
43                                          const std::string &separator,
44                                          size_t &pos, std::string &unit) {
45 //check if wrapped data starts with separator
46     size_t separatorSize = separator.size();
47     if (wrappedData.compare(pos, separatorSize, separator) != 0)
48         return false;
49
50 //find ending separator
51     size_t unitStartIndex = pos + separatorSize;
52     size_t endingSeparatorIndex = wrappedData.find(separator, unitStartIndex);
53     if (endingSeparatorIndex == std::string::npos)
54         return false;
55
56 //return found unit
57     pos = endingSeparatorIndex + separatorSize;
58     size_t unitSize = endingSeparatorIndex - unitStartIndex;
59     unit.assign(wrappedData, unitStartIndex, unitSize);
60     return true;
61 }
62
63 bool unwrapAgentData(const Cynara::PluginData &wrappedData, AgentDataVector& data) {
64     std::string record;
65     size_t pos = 0;
66     if (!unwrapAgentDataFromSeparator(wrappedData, AGENT_DATA_RECORD_SEPARATOR, pos, record))
67         return false;
68
69     pos = 0;
70     while (pos < record.size()) {
71         std::string unit;
72         if (!unwrapAgentDataFromSeparator(record, AGENT_DATA_UNIT_SEPARATOR, pos, unit))
73             return false;
74         data.push_back(unit);
75     }
76     return true;
77 }
78
79 } // namespace CynaraTestPlugins