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