diff options
author | Remko Tronçon <git@el-tramo.be> | 2009-06-01 08:48:42 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2009-06-01 09:24:28 (GMT) |
commit | 2812bddd81f8a1b804c7460f4e14cd0aa393d129 (patch) | |
tree | d46294f35150c4f0f43deaf2d31fceaf945ae715 /Swiften/StreamStack/UnitTest | |
download | swift-contrib-2812bddd81f8a1b804c7460f4e14cd0aa393d129.zip swift-contrib-2812bddd81f8a1b804c7460f4e14cd0aa393d129.tar.bz2 |
Import.
Diffstat (limited to 'Swiften/StreamStack/UnitTest')
-rw-r--r-- | Swiften/StreamStack/UnitTest/Makefile.inc | 3 | ||||
-rw-r--r-- | Swiften/StreamStack/UnitTest/StreamStackTest.cpp | 167 | ||||
-rw-r--r-- | Swiften/StreamStack/UnitTest/XMPPLayerTest.cpp | 116 |
3 files changed, 286 insertions, 0 deletions
diff --git a/Swiften/StreamStack/UnitTest/Makefile.inc b/Swiften/StreamStack/UnitTest/Makefile.inc new file mode 100644 index 0000000..dba5f5a --- /dev/null +++ b/Swiften/StreamStack/UnitTest/Makefile.inc @@ -0,0 +1,3 @@ +UNITTEST_SOURCES += \ + Swiften/StreamStack/UnitTest/XMPPLayerTest.cpp \ + Swiften/StreamStack/UnitTest/StreamStackTest.cpp diff --git a/Swiften/StreamStack/UnitTest/StreamStackTest.cpp b/Swiften/StreamStack/UnitTest/StreamStackTest.cpp new file mode 100644 index 0000000..1f6aad7 --- /dev/null +++ b/Swiften/StreamStack/UnitTest/StreamStackTest.cpp @@ -0,0 +1,167 @@ +#include <vector> +#include <boost/bind.hpp> +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/extensions/TestFactoryRegistry.h> + +#include "Swiften/Base/ByteArray.h" +#include "Swiften/StreamStack/StreamStack.h" +#include "Swiften/StreamStack/LowLayer.h" +#include "Swiften/StreamStack/XMPPLayer.h" +#include "Swiften/StreamStack/StreamLayer.h" +#include "Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.h" +#include "Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.h" + +using namespace Swift; + +class StreamStackTest : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE(StreamStackTest); + CPPUNIT_TEST(testWriteData_NoIntermediateStreamStack); + CPPUNIT_TEST(testWriteData_OneIntermediateStream); + CPPUNIT_TEST(testWriteData_TwoIntermediateStreamStack); + CPPUNIT_TEST(testReadData_NoIntermediateStreamStack); + CPPUNIT_TEST(testReadData_OneIntermediateStream); + CPPUNIT_TEST(testReadData_TwoIntermediateStreamStack); + CPPUNIT_TEST(testAddLayer_ExistingOnWriteDataSlot); + CPPUNIT_TEST_SUITE_END(); + + public: + StreamStackTest() {} + + void setUp() { + physicalStream_ = new TestLowLayer(); + xmppStream_ = new XMPPLayer(&parserFactories_, &serializers_); + elementsReceived_ = 0; + dataWriteReceived_ = 0; + } + + void tearDown() { + delete physicalStream_; + delete xmppStream_; + } + + void testWriteData_NoIntermediateStreamStack() { + StreamStack testling(xmppStream_, physicalStream_); + + xmppStream_->writeData("foo"); + + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), physicalStream_->data_.size()); + CPPUNIT_ASSERT_EQUAL(ByteArray("foo"), physicalStream_->data_[0]); + } + + void testWriteData_OneIntermediateStream() { + StreamStack testling(xmppStream_, physicalStream_); + std::auto_ptr<MyStreamLayer> xStream(new MyStreamLayer("X")); + testling.addLayer(xStream.get()); + + xmppStream_->writeData("foo"); + + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), physicalStream_->data_.size()); + CPPUNIT_ASSERT_EQUAL(ByteArray("Xfoo"), physicalStream_->data_[0]); + } + + void testWriteData_TwoIntermediateStreamStack() { + StreamStack testling(xmppStream_, physicalStream_); + std::auto_ptr<MyStreamLayer> xStream(new MyStreamLayer("X")); + std::auto_ptr<MyStreamLayer> yStream(new MyStreamLayer("Y")); + testling.addLayer(xStream.get()); + testling.addLayer(yStream.get()); + + xmppStream_->writeData("foo"); + + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), physicalStream_->data_.size()); + CPPUNIT_ASSERT_EQUAL(ByteArray("XYfoo"), physicalStream_->data_[0]); + } + + void testReadData_NoIntermediateStreamStack() { + StreamStack testling(xmppStream_, physicalStream_); + xmppStream_->onElement.connect(boost::bind(&StreamStackTest::handleElement, this, _1)); + + physicalStream_->onDataRead(ByteArray("<stream:stream xmlns:stream='http://etherx.jabber.org/streams'><presence/>")); + + CPPUNIT_ASSERT_EQUAL(1, elementsReceived_); + } + + void testReadData_OneIntermediateStream() { + StreamStack testling(xmppStream_, physicalStream_); + xmppStream_->onElement.connect(boost::bind(&StreamStackTest::handleElement, this, _1)); + std::auto_ptr<MyStreamLayer> xStream(new MyStreamLayer("<")); + testling.addLayer(xStream.get()); + + physicalStream_->onDataRead(ByteArray("stream:stream xmlns:stream='http://etherx.jabber.org/streams'><presence/>")); + + CPPUNIT_ASSERT_EQUAL(1, elementsReceived_); + } + + void testReadData_TwoIntermediateStreamStack() { + StreamStack testling(xmppStream_, physicalStream_); + xmppStream_->onElement.connect(boost::bind(&StreamStackTest::handleElement, this, _1)); + std::auto_ptr<MyStreamLayer> xStream(new MyStreamLayer("s")); + std::auto_ptr<MyStreamLayer> yStream(new MyStreamLayer("<")); + testling.addLayer(xStream.get()); + testling.addLayer(yStream.get()); + + physicalStream_->onDataRead(ByteArray("tream:stream xmlns:stream='http://etherx.jabber.org/streams'><presence/>")); + + CPPUNIT_ASSERT_EQUAL(1, elementsReceived_); + } + + void testAddLayer_ExistingOnWriteDataSlot() { + StreamStack testling(xmppStream_, physicalStream_); + xmppStream_->onWriteData.connect(boost::bind(&StreamStackTest::handleWriteData, this, _1)); + std::auto_ptr<MyStreamLayer> xStream(new MyStreamLayer("X")); + testling.addLayer(xStream.get()); + + xmppStream_->writeData("foo"); + + CPPUNIT_ASSERT_EQUAL(1, dataWriteReceived_); + } + + void handleElement(boost::shared_ptr<Element>) { + ++elementsReceived_; + } + + void handleWriteData(ByteArray) { + ++dataWriteReceived_; + } + + private: + class MyStreamLayer : public StreamLayer { + public: + MyStreamLayer(const String& prepend) : prepend_(prepend) { + } + + virtual void writeData(const ByteArray& data) { + onWriteData(ByteArray(prepend_) + data); + } + + virtual void handleDataRead(const ByteArray& data) { + onDataRead(ByteArray(prepend_) + data); + } + + private: + String prepend_; + }; + + class TestLowLayer : public LowLayer { + public: + TestLowLayer() { + } + + virtual void writeData(const ByteArray& data) { + data_.push_back(data); + } + + std::vector<ByteArray> data_; + }; + + private: + FullPayloadParserFactoryCollection parserFactories_; + FullPayloadSerializerCollection serializers_; + TestLowLayer* physicalStream_; + XMPPLayer* xmppStream_; + int elementsReceived_; + int dataWriteReceived_; +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(StreamStackTest); diff --git a/Swiften/StreamStack/UnitTest/XMPPLayerTest.cpp b/Swiften/StreamStack/UnitTest/XMPPLayerTest.cpp new file mode 100644 index 0000000..2150f4d --- /dev/null +++ b/Swiften/StreamStack/UnitTest/XMPPLayerTest.cpp @@ -0,0 +1,116 @@ +#include <vector> +#include <boost/bind.hpp> +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/extensions/TestFactoryRegistry.h> + +#include "Swiften/Elements/Presence.h" +#include "Swiften/Base/ByteArray.h" +#include "Swiften/StreamStack/XMPPLayer.h" +#include "Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.h" +#include "Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.h" + +using namespace Swift; + +class XMPPLayerTest : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE(XMPPLayerTest); + CPPUNIT_TEST(testParseData_Error); + CPPUNIT_TEST(testResetParser); + CPPUNIT_TEST(testResetParser_FromSlot); + CPPUNIT_TEST(testWriteHeader); + CPPUNIT_TEST(testWriteElement); + CPPUNIT_TEST(testWriteFooter); + CPPUNIT_TEST_SUITE_END(); + + public: + XMPPLayerTest() {} + + void setUp() { + testling_ = new XMPPLayer(&parserFactories_, &serializers_); + elementsReceived_ = 0; + dataReceived_ = ""; + errorReceived_ = 0; + } + + void tearDown() { + delete testling_; + } + + void testParseData_Error() { + testling_->onError.connect(boost::bind(&XMPPLayerTest::handleError, this)); + + testling_->parseData("<iq>"); + + CPPUNIT_ASSERT_EQUAL(1, errorReceived_); + } + + void testResetParser() { + testling_->onElement.connect(boost::bind(&XMPPLayerTest::handleElement, this, _1)); + testling_->onError.connect(boost::bind(&XMPPLayerTest::handleError, this)); + + testling_->parseData("<stream:stream to='example.com' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' >"); + testling_->resetParser(); + testling_->parseData("<stream:stream to='example.com' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' >"); + testling_->parseData("<presence/>"); + + CPPUNIT_ASSERT_EQUAL(1, elementsReceived_); + CPPUNIT_ASSERT_EQUAL(0, errorReceived_); + } + + void testResetParser_FromSlot() { + testling_->onElement.connect(boost::bind(&XMPPLayerTest::handleElementAndReset, this, _1)); + testling_->parseData("<stream:stream to='example.com' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' ><presence/>"); + testling_->parseData("<stream:stream to='example.com' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' ><presence/>"); + + CPPUNIT_ASSERT_EQUAL(2, elementsReceived_); + CPPUNIT_ASSERT_EQUAL(0, errorReceived_); + } + + void testWriteHeader() { + testling_->onWriteData.connect(boost::bind(&XMPPLayerTest::handleWriteData, this, _1)); + testling_->writeHeader("example.com"); + + CPPUNIT_ASSERT_EQUAL(String("<?xml version='1.0'?><stream:stream to='example.com' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' >"), dataReceived_); + } + + void testWriteElement() { + testling_->onWriteData.connect(boost::bind(&XMPPLayerTest::handleWriteData, this, _1)); + testling_->writeElement(boost::shared_ptr<Presence>(new Presence())); + + CPPUNIT_ASSERT_EQUAL(String("<presence/>"), dataReceived_); + } + + void testWriteFooter() { + testling_->onWriteData.connect(boost::bind(&XMPPLayerTest::handleWriteData, this, _1)); + testling_->writeFooter(); + + CPPUNIT_ASSERT_EQUAL(String("</stream:stream>"), dataReceived_); + } + + void handleElement(boost::shared_ptr<Element>) { + ++elementsReceived_; + } + + void handleElementAndReset(boost::shared_ptr<Element>) { + ++elementsReceived_; + testling_->resetParser(); + } + + void handleWriteData(ByteArray ba) { + dataReceived_ += std::string(ba.getData(), ba.getSize()); + } + + void handleError() { + ++errorReceived_; + } + + private: + FullPayloadParserFactoryCollection parserFactories_; + FullPayloadSerializerCollection serializers_; + XMPPLayer* testling_; + int elementsReceived_; + String dataReceived_; + int errorReceived_; +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(XMPPLayerTest); |