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>
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";
41 std::string format_test_path(const char *file)
43 return std::string(DB_TEST_DIR) + "/" + std::string(file);
46 bool startCallbackFlag = false;
47 XML::Parser::ElementHandlerPtr dummyStartCallback()
49 startCallbackFlag = true;
50 // return empty pointer
51 return XML::Parser::ElementHandlerPtr();
53 bool endCallbackFlag = false;
54 void dummyEndCallback(const XML::Parser::ElementHandlerPtr &)
56 endCallbackFlag = true;
60 BOOST_AUTO_TEST_SUITE(XML_PARSER_TEST)
62 BOOST_AUTO_TEST_CASE(XmlParserTest_wrong_argument)
64 std::string emptyPath;
65 XML::Parser parser(emptyPath);
66 BOOST_REQUIRE(Parser::ErrorCode::ERROR_INVALID_ARGUMENT == parser.Validate(
70 BOOST_REQUIRE(Parser::ErrorCode::ERROR_INVALID_ARGUMENT == parser.Parse());
72 BOOST_REQUIRE(Parser::ErrorCode::PARSE_SUCCESS ==
73 parser.RegisterElementCb("Key", dummyStartCallback, dummyEndCallback));
74 BOOST_REQUIRE(Parser::ErrorCode::ERROR_XML_PARSE_FAILED == parser.Parse());
77 BOOST_AUTO_TEST_CASE(XmlParserTest_no_XML_file)
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()));
84 BOOST_AUTO_TEST_CASE(XmlParserTest_XML1_correct_verify)
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()));
90 BOOST_AUTO_TEST_CASE(XmlParserTest_XML1_wrong_verify)
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()));
97 BOOST_AUTO_TEST_CASE(XmlParserTest_XML1_wrong_schema)
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()));
104 BOOST_AUTO_TEST_CASE(XmlParserTest_XML1_correct_parse_incorrect_callbacks)
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()));
109 BOOST_REQUIRE(Parser::ErrorCode::PARSE_SUCCESS ==
110 parser.RegisterElementCb("Data", NULL, NULL));
111 BOOST_REQUIRE(Parser::ErrorCode::PARSE_SUCCESS == parser.Parse());
114 BOOST_AUTO_TEST_CASE(XmlParserTest_XML1_correct_parse)
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()));
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);
130 class StructureTest {
132 class ExpectedSumHandler : public XML::Parser::ElementHandler {
134 ExpectedSumHandler() : m_value(0) {}
136 virtual void Start(const XML::Parser::Attributes &) {}
137 virtual void Characters(const std::string &data)
139 m_value = atoi(data.c_str());
141 virtual void End() {}
152 class MathHandler : public XML::Parser::ElementHandler {
154 MathHandler() : m_valueSet(false), m_value(0), m_powerFactor(1) {}
156 virtual void Start(const XML::Parser::Attributes &attr)
158 const auto &it = attr.find("powerFactor");
160 if (it != attr.end())
161 m_powerFactor = atoi(it->second.c_str());
163 virtual void Characters(const std::string &data)
165 m_value = pow(atoi(data.c_str()), m_powerFactor);
168 virtual void End() {}
170 virtual int compute(int prevVal) = 0;
177 class AddHandler : public MathHandler {
179 virtual int compute(int prevVal)
184 return prevVal + m_value;
188 class MultiplyHandler : public MathHandler {
190 virtual int compute(int prevVal)
195 return prevVal * m_value;
199 class DivHandler : public MathHandler {
201 virtual int compute(int prevVal)
209 return prevVal / m_value;
213 StructureTest(const char *filename) : m_parser(filename), m_sum(0),
216 m_parser.RegisterErrorCb(StructureTest::Error);
217 m_parser.RegisterElementCb("Add",
218 [this]() -> XML::Parser::ElementHandlerPtr {
219 return std::make_shared<AddHandler>();
221 [this](const XML::Parser::ElementHandlerPtr & element) {
224 MathHandler *mathElement = reinterpret_cast<MathHandler *>(element.get());
225 m_sum = mathElement->compute(m_sum);
229 m_parser.RegisterElementCb("Multiply",
230 [this]() -> XML::Parser::ElementHandlerPtr {
231 return std::make_shared<MultiplyHandler>();
233 [this](const XML::Parser::ElementHandlerPtr & element) {
234 // multiply computation
236 MathHandler *mathElement = reinterpret_cast<MathHandler *>(element.get());
237 m_sum = mathElement->compute(m_sum);
241 m_parser.RegisterElementCb("Div",
242 [this]() -> XML::Parser::ElementHandlerPtr {
243 return std::make_shared<DivHandler>();
245 [this](const XML::Parser::ElementHandlerPtr & element) {
246 // division computation
248 MathHandler *mathElement = reinterpret_cast<MathHandler *>(element.get());
249 m_sum = mathElement->compute(m_sum);
253 m_parser.RegisterElementCb("ExpectedSum",
254 [this]() -> XML::Parser::ElementHandlerPtr {
255 return std::make_shared<ExpectedSumHandler>();
257 [this](const XML::Parser::ElementHandlerPtr & element) {
259 ExpectedSumHandler *sumElement = reinterpret_cast<ExpectedSumHandler *>
261 m_expectedSum = sumElement->getSum();
266 static void Error(const Parser::ErrorType /*errorType*/,
267 const std::string &log_msg)
274 return m_parser.Parse();
282 int getExpectedSum() const
284 return m_expectedSum;
288 XML::Parser m_parser;
293 BOOST_AUTO_TEST_CASE(XmlParserTest_XML2_structure)
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());
302 BOOST_AUTO_TEST_CASE(XmlParserTest_XML3_encrypted_correct_parse)
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()));
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);
318 BOOST_AUTO_TEST_CASE(XmlParserTest_XML4_device_key_correct_parse)
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()));
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);
330 BOOST_AUTO_TEST_SUITE_END()