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