summaryrefslogtreecommitdiffstats
blob: e284ba9c39ae7092838a1cb9f2c65892aa33cf56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <vector>
#include <boost/bind.hpp>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>

#include "Swiften/Elements/ProtocolHeader.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));
			ProtocolHeader header;
			header.setTo("example.com");
			testling_->writeHeader(header);

			CPPUNIT_ASSERT_EQUAL(String("<?xml version=\"1.0\"?><stream:stream xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\" to=\"example.com\" 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);