diff options
Diffstat (limited to 'Swiften/Serializer')
-rw-r--r-- | Swiften/Serializer/ComponentHandshakeSerializer.cpp | 21 | ||||
-rw-r--r-- | Swiften/Serializer/ComponentHandshakeSerializer.h | 21 | ||||
-rw-r--r-- | Swiften/Serializer/GenericElementSerializer.h | 5 | ||||
-rw-r--r-- | Swiften/Serializer/UnitTest/XMPPSerializerTest.cpp | 75 | ||||
-rw-r--r-- | Swiften/Serializer/XMPPSerializer.cpp | 17 | ||||
-rw-r--r-- | Swiften/Serializer/XMPPSerializer.h | 7 |
6 files changed, 139 insertions, 7 deletions
diff --git a/Swiften/Serializer/ComponentHandshakeSerializer.cpp b/Swiften/Serializer/ComponentHandshakeSerializer.cpp new file mode 100644 index 0000000..de1958e --- /dev/null +++ b/Swiften/Serializer/ComponentHandshakeSerializer.cpp @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2010 Remko Tronçon + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#include "Swiften/Serializer/ComponentHandshakeSerializer.h" + +#include "Swiften/Elements/ComponentHandshake.h" + +namespace Swift { + +ComponentHandshakeSerializer::ComponentHandshakeSerializer() { +} + +String ComponentHandshakeSerializer::serialize(boost::shared_ptr<Element> element) const { + boost::shared_ptr<ComponentHandshake> handshake(boost::dynamic_pointer_cast<ComponentHandshake>(element)); + return "<handshake>" + handshake->getData() + "</challenge>"; +} + +} diff --git a/Swiften/Serializer/ComponentHandshakeSerializer.h b/Swiften/Serializer/ComponentHandshakeSerializer.h new file mode 100644 index 0000000..5423f08 --- /dev/null +++ b/Swiften/Serializer/ComponentHandshakeSerializer.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2010 Remko Tronçon + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#pragma once + +#include <boost/shared_ptr.hpp> + +#include "Swiften/Elements/ComponentHandshake.h" +#include "Swiften/Serializer/GenericElementSerializer.h" + +namespace Swift { + class ComponentHandshakeSerializer : public GenericElementSerializer<ComponentHandshake> { + public: + ComponentHandshakeSerializer(); + + virtual String serialize(boost::shared_ptr<Element> element) const; + }; +} diff --git a/Swiften/Serializer/GenericElementSerializer.h b/Swiften/Serializer/GenericElementSerializer.h index ffebe40..702e374 100644 --- a/Swiften/Serializer/GenericElementSerializer.h +++ b/Swiften/Serializer/GenericElementSerializer.h @@ -4,8 +4,7 @@ * See Documentation/Licenses/GPLv3.txt for more information. */ -#ifndef SWIFTEN_GenericElementSerializer_H -#define SWIFTEN_GenericElementSerializer_H +#pragma once #include "Swiften/Serializer/ElementSerializer.h" @@ -20,5 +19,3 @@ namespace Swift { } }; } - -#endif diff --git a/Swiften/Serializer/UnitTest/XMPPSerializerTest.cpp b/Swiften/Serializer/UnitTest/XMPPSerializerTest.cpp new file mode 100644 index 0000000..45ffe4b --- /dev/null +++ b/Swiften/Serializer/UnitTest/XMPPSerializerTest.cpp @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2010 Remko Tronçon + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/extensions/TestFactoryRegistry.h> + +#include "Swiften/Serializer/XMPPSerializer.h" +#include "Swiften/Elements/AuthChallenge.h" +#include "Swiften/Serializer/PayloadSerializerCollection.h" +#include "Swiften/Elements/ProtocolHeader.h" + +using namespace Swift; + +class XMPPSerializerTest : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(XMPPSerializerTest); + CPPUNIT_TEST(testSerializeHeader_Client); + CPPUNIT_TEST(testSerializeHeader_Component); + CPPUNIT_TEST(testSerializeHeader_Server); + CPPUNIT_TEST_SUITE_END(); + + public: + void setUp() { + payloadSerializerCollection = new PayloadSerializerCollection(); + } + + void tearDown() { + delete payloadSerializerCollection; + } + + void testSerializeHeader_Client() { + std::auto_ptr<XMPPSerializer> testling(createSerializer(ClientStreamType)); + ProtocolHeader protocolHeader; + protocolHeader.setFrom("bla@foo.com"); + protocolHeader.setTo("foo.com"); + protocolHeader.setID("myid"); + protocolHeader.setVersion("0.99"); + + CPPUNIT_ASSERT_EQUAL(String("<?xml version=\"1.0\"?><stream:stream xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\" from=\"bla@foo.com\" to=\"foo.com\" id=\"myid\" version=\"0.99\">"), testling->serializeHeader(protocolHeader)); + } + + void testSerializeHeader_Component() { + std::auto_ptr<XMPPSerializer> testling(createSerializer(ComponentStreamType)); + ProtocolHeader protocolHeader; + protocolHeader.setFrom("bla@foo.com"); + protocolHeader.setTo("foo.com"); + protocolHeader.setID("myid"); + protocolHeader.setVersion("0.99"); + + CPPUNIT_ASSERT_EQUAL(String("<?xml version=\"1.0\"?><stream:stream xmlns=\"jabber:component:accept\" xmlns:stream=\"http://etherx.jabber.org/streams\" from=\"bla@foo.com\" to=\"foo.com\" id=\"myid\" version=\"0.99\">"), testling->serializeHeader(protocolHeader)); + } + + void testSerializeHeader_Server() { + std::auto_ptr<XMPPSerializer> testling(createSerializer(ServerStreamType)); + ProtocolHeader protocolHeader; + protocolHeader.setFrom("bla@foo.com"); + protocolHeader.setTo("foo.com"); + protocolHeader.setID("myid"); + protocolHeader.setVersion("0.99"); + + CPPUNIT_ASSERT_EQUAL(String("<?xml version=\"1.0\"?><stream:stream xmlns=\"jabber:server\" xmlns:stream=\"http://etherx.jabber.org/streams\" from=\"bla@foo.com\" to=\"foo.com\" id=\"myid\" version=\"0.99\">"), testling->serializeHeader(protocolHeader)); + } + + private: + XMPPSerializer* createSerializer(StreamType type) { + return new XMPPSerializer(payloadSerializerCollection, type); + } + + private: + PayloadSerializerCollection* payloadSerializerCollection; +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(XMPPSerializerTest); diff --git a/Swiften/Serializer/XMPPSerializer.cpp b/Swiften/Serializer/XMPPSerializer.cpp index d2eb520..da4715c 100644 --- a/Swiften/Serializer/XMPPSerializer.cpp +++ b/Swiften/Serializer/XMPPSerializer.cpp @@ -8,6 +8,7 @@ #include <boost/bind.hpp> #include <iostream> +#include <cassert> #include "Swiften/Elements/ProtocolHeader.h" #include "Swiften/Base/foreach.h" @@ -30,10 +31,11 @@ #include "Swiften/Serializer/MessageSerializer.h" #include "Swiften/Serializer/PresenceSerializer.h" #include "Swiften/Serializer/IQSerializer.h" +#include "Swiften/Serializer/ComponentHandshakeSerializer.h" namespace Swift { -XMPPSerializer::XMPPSerializer(PayloadSerializerCollection* payloadSerializers) { +XMPPSerializer::XMPPSerializer(PayloadSerializerCollection* payloadSerializers, StreamType type) : type_(type) { serializers_.push_back(boost::shared_ptr<ElementSerializer>(new PresenceSerializer(payloadSerializers))); serializers_.push_back(boost::shared_ptr<ElementSerializer>(new IQSerializer(payloadSerializers))); serializers_.push_back(boost::shared_ptr<ElementSerializer>(new MessageSerializer(payloadSerializers))); @@ -53,10 +55,11 @@ XMPPSerializer::XMPPSerializer(PayloadSerializerCollection* payloadSerializers) serializers_.push_back(boost::shared_ptr<ElementSerializer>(new StreamManagementFailedSerializer())); serializers_.push_back(boost::shared_ptr<ElementSerializer>(new StanzaAckSerializer())); serializers_.push_back(boost::shared_ptr<ElementSerializer>(new StanzaAckRequestSerializer())); + serializers_.push_back(boost::shared_ptr<ElementSerializer>(new ComponentHandshakeSerializer())); } String XMPPSerializer::serializeHeader(const ProtocolHeader& header) const { - String result = "<?xml version=\"1.0\"?><stream:stream xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\""; + String result = "<?xml version=\"1.0\"?><stream:stream xmlns=\"" + getDefaultNamespace() + "\" xmlns:stream=\"http://etherx.jabber.org/streams\""; if (!header.getFrom().isEmpty()) { result += " from=\"" + header.getFrom() + "\""; } @@ -90,4 +93,14 @@ String XMPPSerializer::serializeFooter() const { return "</stream:stream>"; } +String XMPPSerializer::getDefaultNamespace() const { + switch (type_) { + case ClientStreamType: return "jabber:client"; + case ServerStreamType: return "jabber:server"; + case ComponentStreamType: return "jabber:component:accept"; + } + assert(false); + return ""; +} + } diff --git a/Swiften/Serializer/XMPPSerializer.h b/Swiften/Serializer/XMPPSerializer.h index fbb2cda..13c2cf7 100644 --- a/Swiften/Serializer/XMPPSerializer.h +++ b/Swiften/Serializer/XMPPSerializer.h @@ -10,6 +10,7 @@ #include <vector> #include "Swiften/Elements/Element.h" +#include "Swiften/Elements/StreamType.h" #include "Swiften/Base/String.h" #include "Swiften/Serializer/ElementSerializer.h" @@ -20,13 +21,17 @@ namespace Swift { class XMPPSerializer { public: - XMPPSerializer(PayloadSerializerCollection*); + XMPPSerializer(PayloadSerializerCollection*, StreamType type); String serializeHeader(const ProtocolHeader&) const; String serializeElement(boost::shared_ptr<Element> stanza) const; String serializeFooter() const; private: + String getDefaultNamespace() const; + + private: + StreamType type_; std::vector< boost::shared_ptr<ElementSerializer> > serializers_; }; } |