Release 0.1.66
[platform/core/security/key-manager.git] / unit-tests / test_xml-parser.cpp
1         /*
2  *  Copyright (c) 2015-2020 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        test_xml-parser.cpp
18  * @author      Maciej Karpiuk (m.karpiuk2@samsung.com)
19  * @version     1.0
20  * @brief       XML parser tests.
21  */
22
23 #include <cmath>
24 #include <vector>
25 #include <boost_macros_wrapper.h>
26 #include <parser.h>
27
28 using namespace CKM;
29 using namespace XML;
30
31 namespace {
32 const char *XML_1_okay          = "XML_1_okay.xml";
33 const char *XSD_1_okay          = "XML_1_okay.xsd";
34 const char *XML_1_wrong         = "XML_1_wrong.xml";
35 const char *XSD_1_wrong         = "XML_1_wrong.xsd";
36 const char *XML_2_structure     = "XML_2_structure.xml";
37 const char *XML_3_encrypted     = "XML_3_encrypted.xml";
38 const char *XSD_3_encrypted     = "XML_3_encrypted.xsd";
39 const char *XML_4_device_key    = "XML_4_device_key.xml";
40 const char *XSD_4_device_key    = "XML_4_device_key.xsd";
41
42 std::string format_test_path(const char *file)
43 {
44         return std::string(DB_TEST_DIR) + "/" + std::string(file);
45 }
46
47 bool startCallbackFlag = false;
48 XML::Parser::ElementHandlerPtr dummyStartCallback()
49 {
50         startCallbackFlag = true;
51         // return empty pointer
52         return XML::Parser::ElementHandlerPtr();
53 }
54 bool endCallbackFlag = false;
55 void dummyEndCallback(const XML::Parser::ElementHandlerPtr &)
56 {
57         endCallbackFlag = true;
58 }
59 }
60
61 BOOST_AUTO_TEST_SUITE(XML_PARSER_TEST)
62
63 NEGATIVE_TEST_CASE(XmlParserTest_wrong_argument)
64 {
65         std::string emptyPath;
66         XML::Parser parser(emptyPath);
67         BOOST_REQUIRE(Parser::ErrorCode::ERROR_INVALID_ARGUMENT == parser.Validate(
68                                           emptyPath));
69
70         // no listeners
71         BOOST_REQUIRE(Parser::ErrorCode::ERROR_INVALID_ARGUMENT == parser.Parse());
72
73         BOOST_REQUIRE(Parser::ErrorCode::PARSE_SUCCESS ==
74                                   parser.RegisterElementCb("Key", dummyStartCallback, dummyEndCallback));
75         BOOST_REQUIRE(Parser::ErrorCode::ERROR_XML_PARSE_FAILED == parser.Parse());
76 }
77
78 NEGATIVE_TEST_CASE(XmlParserTest_no_XML_file)
79 {
80         XML::Parser parser(format_test_path("i-am-not-here").c_str());
81         BOOST_REQUIRE(Parser::ErrorCode::ERROR_XML_VALIDATION_FAILED == parser.Validate(
82                                           format_test_path(XSD_1_okay).c_str()));
83 }
84
85 POSITIVE_TEST_CASE(XmlParserTest_XML1_correct_verify)
86 {
87         XML::Parser parser(format_test_path(XML_1_okay).c_str());
88         BOOST_REQUIRE(0 == parser.Validate(format_test_path(XSD_1_okay).c_str()));
89 }
90
91 NEGATIVE_TEST_CASE(XmlParserTest_XML1_wrong_verify)
92 {
93         XML::Parser parser(format_test_path(XML_1_wrong).c_str());
94         BOOST_REQUIRE(Parser::ErrorCode::ERROR_XML_VALIDATION_FAILED == parser.Validate(
95                                           format_test_path(XSD_1_okay).c_str()));
96 }
97
98 NEGATIVE_TEST_CASE(XmlParserTest_XML1_wrong_schema)
99 {
100         XML::Parser parser(format_test_path(XML_1_okay).c_str());
101         BOOST_REQUIRE(Parser::ErrorCode::ERROR_XSD_PARSE_FAILED == parser.Validate(
102                                           format_test_path(XSD_1_wrong).c_str()));
103 }
104
105 NEGATIVE_TEST_CASE(XmlParserTest_XML1_correct_parse_incorrect_callbacks)
106 {
107         XML::Parser parser(format_test_path(XML_1_okay).c_str());
108         BOOST_REQUIRE(0 == parser.Validate(format_test_path(XSD_1_okay).c_str()));
109
110         BOOST_REQUIRE(Parser::ErrorCode::PARSE_SUCCESS ==
111                                   parser.RegisterElementCb("Data", NULL, NULL));
112         BOOST_REQUIRE(Parser::ErrorCode::PARSE_SUCCESS == parser.Parse());
113 }
114
115 POSITIVE_TEST_CASE(XmlParserTest_XML1_correct_parse)
116 {
117         XML::Parser parser(format_test_path(XML_1_okay).c_str());
118         BOOST_REQUIRE(0 == parser.Validate(format_test_path(XSD_1_okay).c_str()));
119
120         BOOST_REQUIRE(Parser::ErrorCode::PARSE_SUCCESS ==
121                                   parser.RegisterElementCb("Key", dummyStartCallback, NULL));
122         BOOST_REQUIRE(Parser::ErrorCode::PARSE_SUCCESS ==
123                                   parser.RegisterElementCb("Cert", NULL, dummyEndCallback));
124         startCallbackFlag = false;
125         endCallbackFlag = false;
126         BOOST_REQUIRE(Parser::ErrorCode::PARSE_SUCCESS == parser.Parse());
127         BOOST_REQUIRE(startCallbackFlag == true);
128         BOOST_REQUIRE(endCallbackFlag == true);
129 }
130
131 class StructureTest {
132 public:
133         class ExpectedSumHandler : public XML::Parser::ElementHandler {
134         public:
135                 ExpectedSumHandler() : m_value(0) {}
136
137                 virtual void Start(const XML::Parser::Attributes &) {}
138                 virtual void Characters(const std::string &data)
139                 {
140                         m_value = atoi(data.c_str());
141                 }
142                 virtual void End() {}
143
144                 int getSum() const
145                 {
146                         return m_value;
147                 }
148
149         protected:
150                 int m_value;
151         };
152
153         class MathHandler : public XML::Parser::ElementHandler {
154         public:
155                 MathHandler() : m_valueSet(false), m_value(0), m_powerFactor(1) {}
156
157                 virtual void Start(const XML::Parser::Attributes &attr)
158                 {
159                         const auto &it = attr.find("powerFactor");
160
161                         if (it != attr.end())
162                                 m_powerFactor = atoi(it->second.c_str());
163                 }
164                 virtual void Characters(const std::string &data)
165                 {
166                         m_value = pow(atoi(data.c_str()), m_powerFactor);
167                         m_valueSet = true;
168                 }
169                 virtual void End() {}
170
171                 virtual int compute(int prevVal) = 0;
172
173         protected:
174                 bool m_valueSet;
175                 int m_value;
176                 int m_powerFactor;
177         };
178         class AddHandler : public MathHandler {
179         public:
180                 virtual int compute(int prevVal)
181                 {
182                         if (!m_valueSet)
183                                 return prevVal;
184
185                         return prevVal + m_value;
186                 }
187         };
188
189         class MultiplyHandler : public MathHandler {
190         public:
191                 virtual int compute(int prevVal)
192                 {
193                         if (!m_valueSet)
194                                 return prevVal;
195
196                         return prevVal * m_value;
197                 }
198         };
199
200         class DivHandler : public MathHandler {
201         public:
202                 virtual int compute(int prevVal)
203                 {
204                         if (!m_valueSet)
205                                 return prevVal;
206
207                         if (m_value == 0)
208                                 return prevVal;
209
210                         return prevVal / m_value;
211                 }
212         };
213
214         explicit StructureTest(const char *filename) : m_parser(filename), m_sum(0),
215                 m_expectedSum(0)
216         {
217                 m_parser.RegisterErrorCb(StructureTest::Error);
218                 m_parser.RegisterElementCb("Add",
219                 [this]() -> XML::Parser::ElementHandlerPtr {
220                         return std::make_shared<AddHandler>();
221                 },
222                 [this](const XML::Parser::ElementHandlerPtr & element) {
223                         // add computation
224                         if (element) {
225                                 MathHandler *mathElement = reinterpret_cast<MathHandler *>(element.get());
226                                 m_sum = mathElement->compute(m_sum);
227                         }
228                 });
229
230                 m_parser.RegisterElementCb("Multiply",
231                 [this]() -> XML::Parser::ElementHandlerPtr {
232                         return std::make_shared<MultiplyHandler>();
233                 },
234                 [this](const XML::Parser::ElementHandlerPtr & element) {
235                         // multiply computation
236                         if (element) {
237                                 MathHandler *mathElement = reinterpret_cast<MathHandler *>(element.get());
238                                 m_sum = mathElement->compute(m_sum);
239                         }
240                 });
241
242                 m_parser.RegisterElementCb("Div",
243                 [this]() -> XML::Parser::ElementHandlerPtr {
244                         return std::make_shared<DivHandler>();
245                 },
246                 [this](const XML::Parser::ElementHandlerPtr & element) {
247                         // division computation
248                         if (element) {
249                                 MathHandler *mathElement = reinterpret_cast<MathHandler *>(element.get());
250                                 m_sum = mathElement->compute(m_sum);
251                         }
252                 });
253
254                 m_parser.RegisterElementCb("ExpectedSum",
255                 [this]() -> XML::Parser::ElementHandlerPtr {
256                         return std::make_shared<ExpectedSumHandler>();
257                 },
258                 [this](const XML::Parser::ElementHandlerPtr & element) {
259                         if (element) {
260                                 ExpectedSumHandler *sumElement = reinterpret_cast<ExpectedSumHandler *>
261                                                                                                  (element.get());
262                                 m_expectedSum = sumElement->getSum();
263                         }
264                 });
265         }
266
267         static void Error(const Parser::ErrorType /*errorType*/,
268                                           const std::string &log_msg)
269         {
270                 BOOST_FAIL(log_msg);
271         }
272
273         int Parse()
274         {
275                 return m_parser.Parse();
276         }
277
278         int getSum() const
279         {
280                 return m_sum;
281         }
282
283         int getExpectedSum() const
284         {
285                 return m_expectedSum;
286         }
287
288 private:
289         XML::Parser m_parser;
290         int m_sum;
291         int m_expectedSum;
292 };
293
294 POSITIVE_TEST_CASE(XmlParserTest_XML2_structure)
295 {
296         StructureTest parser(format_test_path(XML_2_structure).c_str());
297         BOOST_REQUIRE(0 == parser.Parse());
298         BOOST_REQUIRE_MESSAGE(parser.getSum() == parser.getExpectedSum(),
299                                                   "got sum: " << parser.getSum() << " while expected: " <<
300                                                   parser.getExpectedSum());
301 }
302
303 POSITIVE_TEST_CASE(XmlParserTest_XML3_encrypted_correct_parse)
304 {
305         XML::Parser parser(format_test_path(XML_3_encrypted).c_str());
306         BOOST_REQUIRE(0 == parser.Validate(format_test_path(XSD_3_encrypted).c_str()));
307
308         BOOST_REQUIRE(Parser::ErrorCode::PARSE_SUCCESS ==
309                                   parser.RegisterElementCb("Key", dummyStartCallback, NULL));
310         BOOST_REQUIRE(Parser::ErrorCode::PARSE_SUCCESS ==
311                                   parser.RegisterElementCb("Cert", NULL, dummyEndCallback));
312         startCallbackFlag = false;
313         endCallbackFlag = false;
314         BOOST_REQUIRE(Parser::ErrorCode::PARSE_SUCCESS == parser.Parse());
315         BOOST_REQUIRE(startCallbackFlag == true);
316         BOOST_REQUIRE(endCallbackFlag == true);
317 }
318
319 POSITIVE_TEST_CASE(XmlParserTest_XML4_device_key_correct_parse)
320 {
321         XML::Parser parser(format_test_path(XML_4_device_key).c_str());
322         BOOST_REQUIRE(0 == parser.Validate(format_test_path(XSD_4_device_key).c_str()));
323
324         BOOST_REQUIRE(Parser::ErrorCode::PARSE_SUCCESS ==
325                                   parser.RegisterElementCb("RSAPrivateKey", dummyStartCallback, NULL));
326         startCallbackFlag = false;
327         BOOST_REQUIRE(Parser::ErrorCode::PARSE_SUCCESS == parser.Parse());
328         BOOST_REQUIRE(startCallbackFlag == true);
329 }
330
331 BOOST_AUTO_TEST_SUITE_END()