2 * Copyright (c) 2015-2020 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.
25 #include <boost_macros_wrapper.h>
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 NEGATIVE_TEST_CASE(XmlParserTest_wrong_argument)
65 std::string emptyPath;
66 XML::Parser parser(emptyPath);
67 BOOST_REQUIRE(Parser::ErrorCode::ERROR_INVALID_ARGUMENT == parser.Validate(
71 BOOST_REQUIRE(Parser::ErrorCode::ERROR_INVALID_ARGUMENT == parser.Parse());
73 BOOST_REQUIRE(Parser::ErrorCode::PARSE_SUCCESS ==
74 parser.RegisterElementCb("Key", dummyStartCallback, dummyEndCallback));
75 BOOST_REQUIRE(Parser::ErrorCode::ERROR_XML_PARSE_FAILED == parser.Parse());
78 NEGATIVE_TEST_CASE(XmlParserTest_no_XML_file)
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()));
85 POSITIVE_TEST_CASE(XmlParserTest_XML1_correct_verify)
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()));
91 NEGATIVE_TEST_CASE(XmlParserTest_XML1_wrong_verify)
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()));
98 NEGATIVE_TEST_CASE(XmlParserTest_XML1_wrong_schema)
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()));
105 NEGATIVE_TEST_CASE(XmlParserTest_XML1_correct_parse_incorrect_callbacks)
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()));
110 BOOST_REQUIRE(Parser::ErrorCode::PARSE_SUCCESS ==
111 parser.RegisterElementCb("Data", NULL, NULL));
112 BOOST_REQUIRE(Parser::ErrorCode::PARSE_SUCCESS == parser.Parse());
115 POSITIVE_TEST_CASE(XmlParserTest_XML1_correct_parse)
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()));
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);
131 class StructureTest {
133 class ExpectedSumHandler : public XML::Parser::ElementHandler {
135 ExpectedSumHandler() : m_value(0) {}
137 virtual void Start(const XML::Parser::Attributes &) {}
138 virtual void Characters(const std::string &data)
140 m_value = atoi(data.c_str());
142 virtual void End() {}
153 class MathHandler : public XML::Parser::ElementHandler {
155 MathHandler() : m_valueSet(false), m_value(0), m_powerFactor(1) {}
157 virtual void Start(const XML::Parser::Attributes &attr)
159 const auto &it = attr.find("powerFactor");
161 if (it != attr.end())
162 m_powerFactor = atoi(it->second.c_str());
164 virtual void Characters(const std::string &data)
166 m_value = pow(atoi(data.c_str()), m_powerFactor);
169 virtual void End() {}
171 virtual int compute(int prevVal) = 0;
178 class AddHandler : public MathHandler {
180 virtual int compute(int prevVal)
185 return prevVal + m_value;
189 class MultiplyHandler : public MathHandler {
191 virtual int compute(int prevVal)
196 return prevVal * m_value;
200 class DivHandler : public MathHandler {
202 virtual int compute(int prevVal)
210 return prevVal / m_value;
214 explicit StructureTest(const char *filename) : m_parser(filename), m_sum(0),
217 m_parser.RegisterErrorCb(StructureTest::Error);
218 m_parser.RegisterElementCb("Add",
219 [this]() -> XML::Parser::ElementHandlerPtr {
220 return std::make_shared<AddHandler>();
222 [this](const XML::Parser::ElementHandlerPtr & element) {
225 MathHandler *mathElement = reinterpret_cast<MathHandler *>(element.get());
226 m_sum = mathElement->compute(m_sum);
230 m_parser.RegisterElementCb("Multiply",
231 [this]() -> XML::Parser::ElementHandlerPtr {
232 return std::make_shared<MultiplyHandler>();
234 [this](const XML::Parser::ElementHandlerPtr & element) {
235 // multiply computation
237 MathHandler *mathElement = reinterpret_cast<MathHandler *>(element.get());
238 m_sum = mathElement->compute(m_sum);
242 m_parser.RegisterElementCb("Div",
243 [this]() -> XML::Parser::ElementHandlerPtr {
244 return std::make_shared<DivHandler>();
246 [this](const XML::Parser::ElementHandlerPtr & element) {
247 // division computation
249 MathHandler *mathElement = reinterpret_cast<MathHandler *>(element.get());
250 m_sum = mathElement->compute(m_sum);
254 m_parser.RegisterElementCb("ExpectedSum",
255 [this]() -> XML::Parser::ElementHandlerPtr {
256 return std::make_shared<ExpectedSumHandler>();
258 [this](const XML::Parser::ElementHandlerPtr & element) {
260 ExpectedSumHandler *sumElement = reinterpret_cast<ExpectedSumHandler *>
262 m_expectedSum = sumElement->getSum();
267 static void Error(const Parser::ErrorType /*errorType*/,
268 const std::string &log_msg)
275 return m_parser.Parse();
283 int getExpectedSum() const
285 return m_expectedSum;
289 XML::Parser m_parser;
294 POSITIVE_TEST_CASE(XmlParserTest_XML2_structure)
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());
303 POSITIVE_TEST_CASE(XmlParserTest_XML3_encrypted_correct_parse)
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()));
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);
319 POSITIVE_TEST_CASE(XmlParserTest_XML4_device_key_correct_parse)
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()));
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);
331 BOOST_AUTO_TEST_SUITE_END()