2 * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 * @file test_xml-parser.cpp
18 * @author Maciej Karpiuk (m.karpiuk2@samsung.com)
20 * @brief XML parser tests.
24 #include <boost/test/unit_test.hpp>
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";
42 std::string format_test_path(const char *file)
44 return std::string(DB_TEST_DIR) + "/" + std::string(file);
47 bool startCallbackFlag = false;
48 XML::Parser::ElementHandlerPtr dummyStartCallback()
50 startCallbackFlag = true;
51 // return empty pointer
52 return XML::Parser::ElementHandlerPtr();
54 bool endCallbackFlag = false;
55 void dummyEndCallback(const XML::Parser::ElementHandlerPtr &)
57 endCallbackFlag = true;
61 BOOST_AUTO_TEST_SUITE(XML_PARSER_TEST)
63 BOOST_AUTO_TEST_CASE(XmlParserTest_wrong_argument)
65 std::string emptyPath;
66 XML::Parser parser(emptyPath);
67 BOOST_REQUIRE(Parser::ErrorCode::ERROR_INVALID_ARGUMENT == parser.Validate(emptyPath));
70 BOOST_REQUIRE(Parser::ErrorCode::ERROR_INVALID_ARGUMENT == parser.Parse());
72 BOOST_REQUIRE(Parser::ErrorCode::PARSE_SUCCESS == parser.RegisterElementCb("Key", dummyStartCallback, dummyEndCallback));
73 BOOST_REQUIRE(Parser::ErrorCode::ERROR_XML_PARSE_FAILED == parser.Parse());
76 BOOST_AUTO_TEST_CASE(XmlParserTest_no_XML_file)
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()));
82 BOOST_AUTO_TEST_CASE(XmlParserTest_XML1_correct_verify)
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()));
88 BOOST_AUTO_TEST_CASE(XmlParserTest_XML1_wrong_verify)
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()));
94 BOOST_AUTO_TEST_CASE(XmlParserTest_XML1_wrong_schema)
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()));
100 BOOST_AUTO_TEST_CASE(XmlParserTest_XML1_correct_parse_incorrect_callbacks)
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()));
105 BOOST_REQUIRE(Parser::ErrorCode::PARSE_SUCCESS == parser.RegisterElementCb("Data", NULL, NULL));
106 BOOST_REQUIRE(Parser::ErrorCode::PARSE_SUCCESS == parser.Parse());
109 BOOST_AUTO_TEST_CASE(XmlParserTest_XML1_correct_parse)
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()));
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);
126 class ExpectedSumHandler : public XML::Parser::ElementHandler
129 ExpectedSumHandler() : m_value(0) {}
131 virtual void Start(const XML::Parser::Attributes &) {}
132 virtual void Characters(const std::string &data) {
133 m_value = atoi(data.c_str());
135 virtual void End() {}
145 class MathHandler : public XML::Parser::ElementHandler
148 MathHandler() : m_valueSet(false), m_value(0), m_powerFactor(1) {}
150 virtual void Start(const XML::Parser::Attributes &attr) {
151 const auto & it = attr.find("powerFactor");
153 m_powerFactor = atoi(it->second.c_str());
155 virtual void Characters(const std::string &data) {
156 m_value = pow(atoi(data.c_str()), m_powerFactor);
159 virtual void End() {}
161 virtual int compute(int prevVal) = 0;
168 class AddHandler : public MathHandler
171 virtual int compute(int prevVal) {
175 return prevVal + m_value;
179 class MultiplyHandler : public MathHandler
182 virtual int compute(int prevVal) {
186 return prevVal * m_value;
190 class DivHandler : public MathHandler
193 virtual int compute(int prevVal) {
199 return prevVal / m_value;
203 StructureTest(const char *filename) : m_parser(filename), m_sum(0), m_expectedSum(0)
205 m_parser.RegisterErrorCb(StructureTest::Error);
206 m_parser.RegisterElementCb("Add",
207 [this]() -> XML::Parser::ElementHandlerPtr
209 return std::make_shared<AddHandler>();
211 [this](const XML::Parser::ElementHandlerPtr & element)
216 MathHandler *mathElement = reinterpret_cast<MathHandler*>(element.get());
217 m_sum = mathElement->compute(m_sum);
220 m_parser.RegisterElementCb("Multiply",
221 [this]() -> XML::Parser::ElementHandlerPtr
223 return std::make_shared<MultiplyHandler>();
225 [this](const XML::Parser::ElementHandlerPtr &element)
227 // multiply computation
230 MathHandler *mathElement = reinterpret_cast<MathHandler*>(element.get());
231 m_sum = mathElement->compute(m_sum);
234 m_parser.RegisterElementCb("Div",
235 [this]() -> XML::Parser::ElementHandlerPtr
237 return std::make_shared<DivHandler>();
239 [this](const XML::Parser::ElementHandlerPtr &element)
241 // division computation
244 MathHandler *mathElement = reinterpret_cast<MathHandler*>(element.get());
245 m_sum = mathElement->compute(m_sum);
248 m_parser.RegisterElementCb("ExpectedSum",
249 [this]() -> XML::Parser::ElementHandlerPtr
251 return std::make_shared<ExpectedSumHandler>();
253 [this](const XML::Parser::ElementHandlerPtr &element)
257 ExpectedSumHandler *sumElement = reinterpret_cast<ExpectedSumHandler*>(element.get());
258 m_expectedSum = sumElement->getSum();
263 static void Error(const Parser::ErrorType /*errorType*/,
264 const std::string & log_msg)
271 return m_parser.Parse();
277 int getExpectedSum() const {
278 return m_expectedSum;
281 XML::Parser m_parser;
286 BOOST_AUTO_TEST_CASE(XmlParserTest_XML2_structure)
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());
294 BOOST_AUTO_TEST_CASE(XmlParserTest_XML3_encrypted_correct_parse)
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()));
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);
308 BOOST_AUTO_TEST_CASE(XmlParserTest_XML4_device_key_correct_parse)
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()));
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);
319 BOOST_AUTO_TEST_SUITE_END()