diff options
author | Mili Verma <mili.verma@isode.com> | 2015-06-23 09:08:56 (GMT) |
---|---|---|
committer | Kevin Smith <kevin.smith@isode.com> | 2015-06-29 14:51:46 (GMT) |
commit | ea41bd07a0e014c12cce144b421abac9f21d1269 (patch) | |
tree | f6c5b44863d8cb7949bfe5ce29da90f07878efe9 | |
parent | 595fdfa75d4757d5654bf3189c23c85a66ec6836 (diff) | |
download | swift-ea41bd07a0e014c12cce144b421abac9f21d1269.zip swift-ea41bd07a0e014c12cce144b421abac9f21d1269.tar.bz2 |
Parse hostname for xep-0233
Test-information:
Verified with M-Link.
Unit tests pass.
Change-Id: Ic675c8d7cd70e01be61c51c0280e1d7208b364ba
-rw-r--r-- | Swiften/Elements/StreamFeatures.h | 12 | ||||
-rw-r--r-- | Swiften/Parser/StreamFeaturesParser.cpp | 13 | ||||
-rw-r--r-- | Swiften/Parser/StreamFeaturesParser.h | 3 | ||||
-rw-r--r-- | Swiften/Parser/UnitTest/StreamFeaturesParserTest.cpp | 40 |
4 files changed, 63 insertions, 5 deletions
diff --git a/Swiften/Elements/StreamFeatures.h b/Swiften/Elements/StreamFeatures.h index 26dc1ba..10563a9 100644 --- a/Swiften/Elements/StreamFeatures.h +++ b/Swiften/Elements/StreamFeatures.h @@ -1,16 +1,17 @@ /* - * Copyright (c) 2010-2014 Isode Limited. + * Copyright (c) 2010-2015 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once #include <vector> #include <string> +#include <boost/optional.hpp> #include <boost/shared_ptr.hpp> #include <Swiften/Base/API.h> #include <Swiften/Elements/ToplevelElement.h> namespace Swift { @@ -65,12 +66,20 @@ namespace Swift { bool hasAuthenticationMechanism(const std::string& mechanism) const; bool hasAuthenticationMechanisms() const { return !authenticationMechanisms_.empty(); } + const boost::optional<std::string> getAuthenticationHostname() const { + return authenticationHostname_; + } + + void setAuthenticationHostname(const boost::optional<std::string> authenticationHostname) { + authenticationHostname_ = authenticationHostname; + } + bool hasStreamManagement() const { return hasStreamManagement_; } void setHasStreamManagement() { hasStreamManagement_ = true; @@ -89,8 +98,9 @@ namespace Swift { std::vector<std::string> compressionMethods_; std::vector<std::string> authenticationMechanisms_; bool hasResourceBind_; bool hasSession_; bool hasStreamManagement_; bool hasRosterVersioning_; + boost::optional<std::string> authenticationHostname_; }; } diff --git a/Swiften/Parser/StreamFeaturesParser.cpp b/Swiften/Parser/StreamFeaturesParser.cpp index f0b8c9a..913c50d 100644 --- a/Swiften/Parser/StreamFeaturesParser.cpp +++ b/Swiften/Parser/StreamFeaturesParser.cpp @@ -1,17 +1,17 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2015 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Swiften/Parser/StreamFeaturesParser.h> namespace Swift { -StreamFeaturesParser::StreamFeaturesParser() : GenericElementParser<StreamFeatures>(), currentDepth_(0), inMechanisms_(false), inMechanism_(false), inCompression_(false), inCompressionMethod_(false) { +StreamFeaturesParser::StreamFeaturesParser() : GenericElementParser<StreamFeatures>(), currentDepth_(0), inMechanisms_(false), inMechanism_(false), inAuthenticationHostname_(false), inCompression_(false), inCompressionMethod_(false) { } void StreamFeaturesParser::handleStartElement(const std::string& element, const std::string& ns, const AttributeMap&) { if (currentDepth_ == 1) { if (element == "starttls" && ns == "urn:ietf:params:xml:ns:xmpp-tls") { getElementGeneric()->setHasStartTLS(); @@ -41,12 +41,17 @@ void StreamFeaturesParser::handleStartElement(const std::string& element, const currentText_ = ""; } else if (inMechanisms_ && element == "mechanism") { inMechanism_ = true; currentText_ = ""; } + else if (inMechanisms_ && element == "hostname" && ns == "urn:xmpp:domain-based-name:1") { + inAuthenticationHostname_ = true; + currentText_ = ""; + } + } ++currentDepth_; } void StreamFeaturesParser::handleEndElement(const std::string&, const std::string&) { --currentDepth_; @@ -60,12 +65,16 @@ void StreamFeaturesParser::handleEndElement(const std::string&, const std::strin inCompressionMethod_ = false; } else if (inMechanism_) { getElementGeneric()->addAuthenticationMechanism(currentText_); inMechanism_ = false; } + else if (inAuthenticationHostname_) { + getElementGeneric()->setAuthenticationHostname(currentText_); + inAuthenticationHostname_ = false; + } } } void StreamFeaturesParser::handleCharacterData(const std::string& data) { currentText_ += data; } diff --git a/Swiften/Parser/StreamFeaturesParser.h b/Swiften/Parser/StreamFeaturesParser.h index 20eabdc..9ae5fd8 100644 --- a/Swiften/Parser/StreamFeaturesParser.h +++ b/Swiften/Parser/StreamFeaturesParser.h @@ -1,8 +1,8 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2015 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once @@ -23,10 +23,11 @@ namespace Swift { private: int currentDepth_; std::string currentText_; bool inMechanisms_; bool inMechanism_; + bool inAuthenticationHostname_; bool inCompression_; bool inCompressionMethod_; }; } diff --git a/Swiften/Parser/UnitTest/StreamFeaturesParserTest.cpp b/Swiften/Parser/UnitTest/StreamFeaturesParserTest.cpp index 8dd3a22..f6c9336 100644 --- a/Swiften/Parser/UnitTest/StreamFeaturesParserTest.cpp +++ b/Swiften/Parser/UnitTest/StreamFeaturesParserTest.cpp @@ -1,8 +1,8 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2015 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> @@ -13,12 +13,14 @@ using namespace Swift; class StreamFeaturesParserTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(StreamFeaturesParserTest); CPPUNIT_TEST(testParse); CPPUNIT_TEST(testParse_Empty); + CPPUNIT_TEST(testParse_AuthenticationHostname); + CPPUNIT_TEST(testParse_AuthenticationHostnameEmpty); CPPUNIT_TEST_SUITE_END(); public: void testParse() { StreamFeaturesParser testling; ElementParserTester parser(&testling); @@ -46,12 +48,13 @@ class StreamFeaturesParserTest : public CppUnit::TestFixture { CPPUNIT_ASSERT(element->hasResourceBind()); CPPUNIT_ASSERT(element->hasCompressionMethod("zlib")); CPPUNIT_ASSERT(element->hasCompressionMethod("lzw")); CPPUNIT_ASSERT(element->hasAuthenticationMechanisms()); CPPUNIT_ASSERT(element->hasAuthenticationMechanism("DIGEST-MD5")); CPPUNIT_ASSERT(element->hasAuthenticationMechanism("PLAIN")); + CPPUNIT_ASSERT(!element->getAuthenticationHostname()); CPPUNIT_ASSERT(element->hasStreamManagement()); CPPUNIT_ASSERT(element->hasRosterVersioning()); } void testParse_Empty() { StreamFeaturesParser testling; @@ -62,9 +65,44 @@ class StreamFeaturesParserTest : public CppUnit::TestFixture { StreamFeatures::ref element = boost::dynamic_pointer_cast<StreamFeatures>(testling.getElement()); CPPUNIT_ASSERT(!element->hasStartTLS()); CPPUNIT_ASSERT(!element->hasSession()); CPPUNIT_ASSERT(!element->hasResourceBind()); CPPUNIT_ASSERT(!element->hasAuthenticationMechanisms()); } + + void testParse_AuthenticationHostname() { + StreamFeaturesParser testling; + ElementParserTester parser(&testling); + std::string hostname("auth42.us.example.com"); + + CPPUNIT_ASSERT(parser.parse( + "<stream:features xmlns:stream='http://etherx.jabber.org/streams'>" + "<mechanisms xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">" + "<mechanism>GSSAPI</mechanism>" + "<hostname xmlns=\"urn:xmpp:domain-based-name:1\">auth42.us.example.com</hostname>" + "</mechanisms>" + "</stream:features>")); + + StreamFeatures::ref element = boost::dynamic_pointer_cast<StreamFeatures>(testling.getElement()); + CPPUNIT_ASSERT(element->hasAuthenticationMechanism("GSSAPI")); + CPPUNIT_ASSERT_EQUAL(*element->getAuthenticationHostname(), hostname); + } + + void testParse_AuthenticationHostnameEmpty() { + StreamFeaturesParser testling; + ElementParserTester parser(&testling); + + CPPUNIT_ASSERT(parser.parse( + "<stream:features xmlns:stream='http://etherx.jabber.org/streams'>" + "<mechanisms xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">" + "<mechanism>GSSAPI</mechanism>" + "<hostname xmlns=\"urn:xmpp:domain-based-name:1\"></hostname>" + "</mechanisms>" + "</stream:features>")); + + StreamFeatures::ref element = boost::dynamic_pointer_cast<StreamFeatures>(testling.getElement()); + CPPUNIT_ASSERT(element->hasAuthenticationMechanism("GSSAPI")); + CPPUNIT_ASSERT(element->getAuthenticationHostname()->empty()); + } }; CPPUNIT_TEST_SUITE_REGISTRATION(StreamFeaturesParserTest); |