diff options
| author | Joanna Hulboj <joanna.hulboj@isode.com> | 2019-09-19 19:48:44 (GMT) |
|---|---|---|
| committer | Joanna Hulboj <joanna.hulboj@isode.com> | 2019-09-24 10:08:15 (GMT) |
| commit | 23e2766dab6d4a3f6158eca7649cd36b644634d3 (patch) | |
| tree | 1faa4b741b03704e7a70918196310a4453e699a7 /Swiften/Parser/UnitTest/XMLParserTest.cpp | |
| parent | e58cf7d5d7d3bab330bccf6a098dd476fbf4dc86 (diff) | |
| download | swift-23e2766dab6d4a3f6158eca7649cd36b644634d3.zip swift-23e2766dab6d4a3f6158eca7649cd36b644634d3.tar.bz2 | |
Process attribute and element prefixes
XML (Expat/LibXML) parsing modified to process prefix information.
Prefixes for attributes stored within attributes.
Prefixes for elements passed in additional callback
(only if prefix present).
Test-information:
Unit tests pass on Windows 10 and Ubuntu 18.04.1 LTS.
Change-Id: Ib6b5087feed758c31895f426df6a3c7ea975f248
Diffstat (limited to 'Swiften/Parser/UnitTest/XMLParserTest.cpp')
| -rw-r--r-- | Swiften/Parser/UnitTest/XMLParserTest.cpp | 59 |
1 files changed, 56 insertions, 3 deletions
diff --git a/Swiften/Parser/UnitTest/XMLParserTest.cpp b/Swiften/Parser/UnitTest/XMLParserTest.cpp index 63d30ea..4db694e 100644 --- a/Swiften/Parser/UnitTest/XMLParserTest.cpp +++ b/Swiften/Parser/UnitTest/XMLParserTest.cpp | |||
| @@ -35,6 +35,7 @@ class XMLParserTest : public CppUnit::TestFixture { | |||
| 35 | CPPUNIT_TEST(testParse_WhitespaceInAttribute); | 35 | CPPUNIT_TEST(testParse_WhitespaceInAttribute); |
| 36 | CPPUNIT_TEST(testParse_AttributeWithoutNamespace); | 36 | CPPUNIT_TEST(testParse_AttributeWithoutNamespace); |
| 37 | CPPUNIT_TEST(testParse_AttributeWithNamespace); | 37 | CPPUNIT_TEST(testParse_AttributeWithNamespace); |
| 38 | CPPUNIT_TEST(testParse_AttributeWithNamespaceNoPrefix); | ||
| 38 | CPPUNIT_TEST(testParse_BillionLaughs); | 39 | CPPUNIT_TEST(testParse_BillionLaughs); |
| 39 | CPPUNIT_TEST(testParse_InternalEntity); | 40 | CPPUNIT_TEST(testParse_InternalEntity); |
| 40 | //CPPUNIT_TEST(testParse_UndefinedPrefix); | 41 | //CPPUNIT_TEST(testParse_UndefinedPrefix); |
| @@ -43,6 +44,7 @@ class XMLParserTest : public CppUnit::TestFixture { | |||
| 43 | CPPUNIT_TEST(testParse_DisallowCommentsInXML); | 44 | CPPUNIT_TEST(testParse_DisallowCommentsInXML); |
| 44 | CPPUNIT_TEST(testParse_Doctype); | 45 | CPPUNIT_TEST(testParse_Doctype); |
| 45 | CPPUNIT_TEST(testParse_ProcessingInstructions); | 46 | CPPUNIT_TEST(testParse_ProcessingInstructions); |
| 47 | CPPUNIT_TEST(testParse_ProcessingPrefixedElement); | ||
| 46 | CPPUNIT_TEST_SUITE_END(); | 48 | CPPUNIT_TEST_SUITE_END(); |
| 47 | 49 | ||
| 48 | public: | 50 | public: |
| @@ -264,6 +266,7 @@ class XMLParserTest : public CppUnit::TestFixture { | |||
| 264 | CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), client_.events[0].attributes.getEntries().size()); | 266 | CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), client_.events[0].attributes.getEntries().size()); |
| 265 | CPPUNIT_ASSERT_EQUAL(std::string("attr"), client_.events[0].attributes.getEntries()[0].getAttribute().getName()); | 267 | CPPUNIT_ASSERT_EQUAL(std::string("attr"), client_.events[0].attributes.getEntries()[0].getAttribute().getName()); |
| 266 | CPPUNIT_ASSERT_EQUAL(std::string(""), client_.events[0].attributes.getEntries()[0].getAttribute().getNamespace()); | 268 | CPPUNIT_ASSERT_EQUAL(std::string(""), client_.events[0].attributes.getEntries()[0].getAttribute().getNamespace()); |
| 269 | CPPUNIT_ASSERT_EQUAL(std::string(""), client_.events[0].attributes.getEntries()[0].getAttribute().getPrefix()); | ||
| 267 | } | 270 | } |
| 268 | 271 | ||
| 269 | void testParse_AttributeWithNamespace() { | 272 | void testParse_AttributeWithNamespace() { |
| @@ -275,6 +278,22 @@ class XMLParserTest : public CppUnit::TestFixture { | |||
| 275 | CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), client_.events[0].attributes.getEntries().size()); | 278 | CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), client_.events[0].attributes.getEntries().size()); |
| 276 | CPPUNIT_ASSERT_EQUAL(std::string("attr"), client_.events[0].attributes.getEntries()[0].getAttribute().getName()); | 279 | CPPUNIT_ASSERT_EQUAL(std::string("attr"), client_.events[0].attributes.getEntries()[0].getAttribute().getName()); |
| 277 | CPPUNIT_ASSERT_EQUAL(std::string("http://swift.im/f"), client_.events[0].attributes.getEntries()[0].getAttribute().getNamespace()); | 280 | CPPUNIT_ASSERT_EQUAL(std::string("http://swift.im/f"), client_.events[0].attributes.getEntries()[0].getAttribute().getNamespace()); |
| 281 | CPPUNIT_ASSERT_EQUAL(std::string("f"), client_.events[0].attributes.getEntries()[0].getAttribute().getPrefix()); | ||
| 282 | CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), client_.events[0].namespaces.size()); | ||
| 283 | CPPUNIT_ASSERT_EQUAL(std::string("http://swift.im"), client_.events[0].namespaces[""]); | ||
| 284 | CPPUNIT_ASSERT_EQUAL(std::string("http://swift.im/f"), client_.events[0].namespaces["f"]); | ||
| 285 | } | ||
| 286 | |||
| 287 | void testParse_AttributeWithNamespaceNoPrefix() { | ||
| 288 | ParserType testling(&client_); | ||
| 289 | |||
| 290 | CPPUNIT_ASSERT(testling.parse( | ||
| 291 | "<query xmlns='http://swift.im' xmlns:f='http://swift.im/f' attr='3'/>")); | ||
| 292 | |||
| 293 | CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), client_.events[0].attributes.getEntries().size()); | ||
| 294 | CPPUNIT_ASSERT_EQUAL(std::string("attr"), client_.events[0].attributes.getEntries()[0].getAttribute().getName()); | ||
| 295 | CPPUNIT_ASSERT_EQUAL(std::string(""), client_.events[0].attributes.getEntries()[0].getAttribute().getNamespace()); | ||
| 296 | CPPUNIT_ASSERT_EQUAL(std::string(""), client_.events[0].attributes.getEntries()[0].getAttribute().getPrefix()); | ||
| 278 | CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), client_.events[0].namespaces.size()); | 297 | CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), client_.events[0].namespaces.size()); |
| 279 | CPPUNIT_ASSERT_EQUAL(std::string("http://swift.im"), client_.events[0].namespaces[""]); | 298 | CPPUNIT_ASSERT_EQUAL(std::string("http://swift.im"), client_.events[0].namespaces[""]); |
| 280 | CPPUNIT_ASSERT_EQUAL(std::string("http://swift.im/f"), client_.events[0].namespaces["f"]); | 299 | CPPUNIT_ASSERT_EQUAL(std::string("http://swift.im/f"), client_.events[0].namespaces["f"]); |
| @@ -373,25 +392,52 @@ class XMLParserTest : public CppUnit::TestFixture { | |||
| 373 | CPPUNIT_ASSERT(!testling.parse("<?xml-stylesheet type=\"text/xsl\" href=\"Sample.xsl\"?>")); | 392 | CPPUNIT_ASSERT(!testling.parse("<?xml-stylesheet type=\"text/xsl\" href=\"Sample.xsl\"?>")); |
| 374 | } | 393 | } |
| 375 | 394 | ||
| 395 | void testParse_ProcessingPrefixedElement() { | ||
| 396 | client_.testingStartElementPrefix = true; | ||
| 397 | ParserType testling(&client_); | ||
| 398 | |||
| 399 | CPPUNIT_ASSERT(testling.parse("<prefix:message xmlns='uri' xmlns:prefix='uriPrefix'/>")); | ||
| 400 | |||
| 401 | CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), client_.events.size()); | ||
| 402 | |||
| 403 | CPPUNIT_ASSERT_EQUAL(Client::StartElementPrefix, client_.events[0].type); | ||
| 404 | CPPUNIT_ASSERT_EQUAL(std::string("message"), client_.events[0].data); | ||
| 405 | CPPUNIT_ASSERT_EQUAL(std::string("uriPrefix"), client_.events[0].ns); | ||
| 406 | CPPUNIT_ASSERT_EQUAL(std::string("prefix"), client_.events[0].prefix); | ||
| 407 | |||
| 408 | CPPUNIT_ASSERT_EQUAL(Client::EndElement, client_.events[1].type); | ||
| 409 | CPPUNIT_ASSERT_EQUAL(std::string("message"), client_.events[1].data); | ||
| 410 | CPPUNIT_ASSERT_EQUAL(std::string("uriPrefix"), client_.events[1].ns); | ||
| 411 | } | ||
| 412 | |||
| 376 | private: | 413 | private: |
| 377 | class Client : public XMLParserClient { | 414 | class Client : public XMLParserClient { |
| 378 | public: | 415 | public: |
| 379 | using NamespaceMap = std::unordered_map<std::string /* prefix */, std::string /* uri */>; | 416 | using NamespaceMap = std::unordered_map<std::string /* prefix */, std::string /* uri */>; |
| 380 | enum Type { StartElement, EndElement, CharacterData, NamespaceDefined }; | 417 | enum Type { StartElement, StartElementPrefix, EndElement, CharacterData, NamespaceDefined }; |
| 381 | struct Event { | 418 | struct Event { |
| 382 | Event( | 419 | Event( |
| 383 | Type type, | 420 | Type type, |
| 384 | const std::string& data, | 421 | const std::string& data, |
| 385 | const std::string& ns, | 422 | const std::string& ns, |
| 423 | const std::string& prefix, | ||
| 424 | const AttributeMap& attributes, | ||
| 425 | NamespaceMap namespaces) | ||
| 426 | : type(type), data(data), ns(ns), prefix(prefix), attributes(attributes), namespaces(std::move(namespaces)) {} | ||
| 427 | Event( | ||
| 428 | Type type, | ||
| 429 | const std::string& data, | ||
| 430 | const std::string& ns, | ||
| 386 | const AttributeMap& attributes, | 431 | const AttributeMap& attributes, |
| 387 | NamespaceMap namespaces = {}) | 432 | NamespaceMap namespaces = {}) |
| 388 | : type(type), data(data), ns(ns), attributes(attributes), namespaces(std::move(namespaces)) {} | 433 | : Event(type, data, ns, {}, attributes, std::move(namespaces)) {} |
| 389 | Event(Type type, const std::string& data, const std::string& ns = std::string()) | 434 | Event(Type type, const std::string& data, const std::string& ns = std::string()) |
| 390 | : type(type), data(data), ns(ns) {} | 435 | : Event(type, data, ns, "", AttributeMap(), NamespaceMap()) {} |
| 391 | 436 | ||
| 392 | Type type; | 437 | Type type; |
| 393 | std::string data; | 438 | std::string data; |
| 394 | std::string ns; | 439 | std::string ns; |
| 440 | std::string prefix; | ||
| 395 | AttributeMap attributes; | 441 | AttributeMap attributes; |
| 396 | NamespaceMap namespaces; | 442 | NamespaceMap namespaces; |
| 397 | }; | 443 | }; |
| @@ -399,9 +445,15 @@ class XMLParserTest : public CppUnit::TestFixture { | |||
| 399 | Client() {} | 445 | Client() {} |
| 400 | 446 | ||
| 401 | void handleStartElement(const std::string& element, const std::string& ns, const AttributeMap& attributes) override { | 447 | void handleStartElement(const std::string& element, const std::string& ns, const AttributeMap& attributes) override { |
| 448 | if (testingStartElementPrefix) return; | ||
| 402 | events.push_back(Event(StartElement, element, ns, attributes, std::move(namespaces_))); | 449 | events.push_back(Event(StartElement, element, ns, attributes, std::move(namespaces_))); |
| 403 | } | 450 | } |
| 404 | 451 | ||
| 452 | void handleStartElementPrefix(const std::string& prefix, const std::string& uri, const std::string& name, const std::string&, const std::string&, const AttributeMap&) override { | ||
| 453 | if (!testingStartElementPrefix) return; | ||
| 454 | events.push_back(Event(StartElementPrefix, name, uri, prefix, AttributeMap(), NamespaceMap())); | ||
| 455 | } | ||
| 456 | |||
| 405 | void handleEndElement(const std::string& element, const std::string& ns) override { | 457 | void handleEndElement(const std::string& element, const std::string& ns) override { |
| 406 | events.push_back(Event(EndElement, element, ns)); | 458 | events.push_back(Event(EndElement, element, ns)); |
| 407 | } | 459 | } |
| @@ -415,6 +467,7 @@ class XMLParserTest : public CppUnit::TestFixture { | |||
| 415 | } | 467 | } |
| 416 | 468 | ||
| 417 | std::vector<Event> events; | 469 | std::vector<Event> events; |
| 470 | bool testingStartElementPrefix = false; | ||
| 418 | private: | 471 | private: |
| 419 | NamespaceMap namespaces_; | 472 | NamespaceMap namespaces_; |
| 420 | } client_; | 473 | } client_; |
Swift