summaryrefslogtreecommitdiffstats
blob: ee27a67048510e24800c686c4cb317686f347130 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * 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 <boost/shared_ptr.hpp>
#include <boost/smart_ptr/make_shared.hpp>
#include <boost/bind.hpp>

#include <Swiften/Queries/IQHandler.h>
#include <Swiften/Queries/IQRouter.h>
#include <Swiften/Queries/DummyIQChannel.h>

using namespace Swift;

class IQRouterTest : public CppUnit::TestFixture {
		CPPUNIT_TEST_SUITE(IQRouterTest);
		CPPUNIT_TEST(testRemoveHandler);
		CPPUNIT_TEST(testRemoveHandler_AfterHandleIQ);
		CPPUNIT_TEST(testHandleIQ_SuccesfulHandlerFirst);
		CPPUNIT_TEST(testHandleIQ_SuccesfulHandlerLast);
		CPPUNIT_TEST(testHandleIQ_NoSuccesfulHandler);
		CPPUNIT_TEST(testHandleIQ_HandlerRemovedDuringHandle);
		CPPUNIT_TEST(testSendIQ_WithFrom);
		CPPUNIT_TEST(testSendIQ_WithoutFrom);
		CPPUNIT_TEST(testHandleIQ_WithFrom);
		CPPUNIT_TEST_SUITE_END();

	public:
		void setUp() {
			channel_ = new DummyIQChannel();
		}

		void tearDown() {
			delete channel_;
		}

		void testRemoveHandler() {
			IQRouter testling(channel_);
			DummyIQHandler handler1(true, &testling);
			DummyIQHandler handler2(true, &testling);
			testling.removeHandler(&handler1);

			channel_->onIQReceived(boost::make_shared<IQ>());

			CPPUNIT_ASSERT_EQUAL(0, handler1.called);
			CPPUNIT_ASSERT_EQUAL(1, handler2.called);
		}

		void testRemoveHandler_AfterHandleIQ() {
			IQRouter testling(channel_);
			DummyIQHandler handler2(true, &testling);
			DummyIQHandler handler1(true, &testling);

			channel_->onIQReceived(boost::make_shared<IQ>());
			testling.removeHandler(&handler1);
			channel_->onIQReceived(boost::make_shared<IQ>());

			CPPUNIT_ASSERT_EQUAL(1, handler1.called);
			CPPUNIT_ASSERT_EQUAL(1, handler2.called);
		}

		void testHandleIQ_SuccesfulHandlerFirst() {
			IQRouter testling(channel_);
			DummyIQHandler handler2(false, &testling);
			DummyIQHandler handler1(true, &testling);

			channel_->onIQReceived(boost::make_shared<IQ>());

			CPPUNIT_ASSERT_EQUAL(1, handler1.called);
			CPPUNIT_ASSERT_EQUAL(0, handler2.called);
			CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(channel_->iqs_.size()));
		}

		void testHandleIQ_SuccesfulHandlerLast() {
			IQRouter testling(channel_);
			DummyIQHandler handler2(true, &testling);
			DummyIQHandler handler1(false, &testling);

			channel_->onIQReceived(boost::make_shared<IQ>());

			CPPUNIT_ASSERT_EQUAL(1, handler1.called);
			CPPUNIT_ASSERT_EQUAL(1, handler2.called);
			CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(channel_->iqs_.size()));
		}

		void testHandleIQ_NoSuccesfulHandler() {
			IQRouter testling(channel_);
			DummyIQHandler handler(false, &testling);

			channel_->onIQReceived(boost::make_shared<IQ>());

			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size()));
			CPPUNIT_ASSERT(channel_->iqs_[0]->getPayload<ErrorPayload>());
		}


		void testHandleIQ_HandlerRemovedDuringHandle() {
			IQRouter testling(channel_);
			DummyIQHandler handler2(true, &testling);
			RemovingIQHandler handler1(&testling);

			channel_->onIQReceived(boost::make_shared<IQ>());
			channel_->onIQReceived(boost::make_shared<IQ>());

			CPPUNIT_ASSERT_EQUAL(1, handler1.called);
			CPPUNIT_ASSERT_EQUAL(2, handler2.called);
		}

		void testSendIQ_WithFrom() {
			IQRouter testling(channel_);
			testling.setFrom(JID("foo@bar.com/baz"));

			testling.sendIQ(boost::make_shared<IQ>());

			CPPUNIT_ASSERT_EQUAL(JID("foo@bar.com/baz"), channel_->iqs_[0]->getFrom());
		}

		void testSendIQ_WithoutFrom() {
			IQRouter testling(channel_);

			testling.sendIQ(boost::make_shared<IQ>());

			CPPUNIT_ASSERT_EQUAL(JID(), channel_->iqs_[0]->getFrom());
		}

		void testHandleIQ_WithFrom() {
			IQRouter testling(channel_);
			testling.setFrom(JID("foo@bar.com/baz"));
			DummyIQHandler handler(false, &testling);

			channel_->onIQReceived(boost::make_shared<IQ>());

			CPPUNIT_ASSERT_EQUAL(JID("foo@bar.com/baz"), channel_->iqs_[0]->getFrom());
		}

	private:
		struct DummyIQHandler : public IQHandler {
			DummyIQHandler(bool handle, IQRouter* router) : handle(handle), router(router), called(0) {
				router->addHandler(this);
			}

			~DummyIQHandler() {
				router->removeHandler(this);
			}

			virtual bool handleIQ(boost::shared_ptr<IQ>) {
				called++;
				return handle;
			}
			bool handle;
			IQRouter* router;
			int called;
		};

		struct RemovingIQHandler : public IQHandler {
			RemovingIQHandler(IQRouter* router) : router(router), called(0) {
				router->addHandler(this);
			}

			virtual bool handleIQ(boost::shared_ptr<IQ>) {
				called++;
				router->removeHandler(this);
				return false;
			}
			IQRouter* router;
			int called;
		};


		DummyIQChannel* channel_;
};

CPPUNIT_TEST_SUITE_REGISTRATION(IQRouterTest);