summaryrefslogtreecommitdiffstats
blob: d24d1f8817f79d1efd194ee9e9875d0b141b40bf (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
/*
 * 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/Elements/Message.h"
#include "Swiften/Server/ServerStanzaRouter.h"
#include "Swiften/Server/ServerSession.h"

using namespace Swift;

class ServerStanzaRouterTest : public CppUnit::TestFixture {
		CPPUNIT_TEST_SUITE(ServerStanzaRouterTest);
		CPPUNIT_TEST(testRouteStanza_FullJID);
		CPPUNIT_TEST(testRouteStanza_FullJIDWithNegativePriority);
		CPPUNIT_TEST(testRouteStanza_FullJIDWithOnlyBareJIDMatchingSession);
		CPPUNIT_TEST(testRouteStanza_BareJIDWithoutMatchingSession);
		CPPUNIT_TEST(testRouteStanza_BareJIDWithMultipleSessions);
		CPPUNIT_TEST(testRouteStanza_BareJIDWithOnlyNegativePriorities);
		CPPUNIT_TEST(testRouteStanza_BareJIDWithChangingPresence);
		CPPUNIT_TEST_SUITE_END();

	public:
		ServerStanzaRouterTest() {}

		void setUp() {
		}

		void tearDown() {
		}

		void testRouteStanza_FullJID() {
			ServerStanzaRouter testling;
			MockServerSession session1(JID("foo@bar.com/Bla"), 0);
			testling.addClientSession(&session1);
			MockServerSession session2(JID("foo@bar.com/Baz"), 0);
			testling.addClientSession(&session2);

			bool result = testling.routeStanza(createMessageTo("foo@bar.com/Baz"));

			CPPUNIT_ASSERT(result);
			CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(session1.sentStanzas.size()));
			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(session2.sentStanzas.size()));
		}

		void testRouteStanza_FullJIDWithNegativePriority() {
			ServerStanzaRouter testling;
			MockServerSession session1(JID("foo@bar.com/Bla"), -1);
			testling.addClientSession(&session1);
			MockServerSession session2(JID("foo@bar.com/Baz"), 0);
			testling.addClientSession(&session2);

			bool result = testling.routeStanza(createMessageTo("foo@bar.com/Bla"));

			CPPUNIT_ASSERT(result);
			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(session1.sentStanzas.size()));
			CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(session2.sentStanzas.size()));
		}

		void testRouteStanza_FullJIDWithOnlyBareJIDMatchingSession() {
			ServerStanzaRouter testling;
			MockServerSession session(JID("foo@bar.com/Bla"), 0);
			testling.addClientSession(&session);

			bool result = testling.routeStanza(createMessageTo("foo@bar.com/Baz"));

			CPPUNIT_ASSERT(result);
			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(session.sentStanzas.size()));
		}

		void testRouteStanza_BareJIDWithoutMatchingSession() {
			ServerStanzaRouter testling;

			bool result = testling.routeStanza(createMessageTo("foo@bar.com"));

			CPPUNIT_ASSERT(!result);
		}

		void testRouteStanza_BareJIDWithMultipleSessions() {
			ServerStanzaRouter testling;
			MockServerSession session1(JID("foo@bar.com/Bla"), 1);
			testling.addClientSession(&session1);
			MockServerSession session2(JID("foo@bar.com/Baz"), 8);
			testling.addClientSession(&session2);
			MockServerSession session3(JID("foo@bar.com/Bar"), 5);
			testling.addClientSession(&session3);

			bool result = testling.routeStanza(createMessageTo("foo@bar.com"));

			CPPUNIT_ASSERT(result);
			CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(session1.sentStanzas.size()));
			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(session2.sentStanzas.size()));
			CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(session3.sentStanzas.size()));
		}

		void testRouteStanza_BareJIDWithOnlyNegativePriorities() {
			ServerStanzaRouter testling;
			MockServerSession session(JID("foo@bar.com/Bla"), -1);
			testling.addClientSession(&session);

			bool result = testling.routeStanza(createMessageTo("foo@bar.com"));

			CPPUNIT_ASSERT(!result);
		}

		void testRouteStanza_BareJIDWithChangingPresence() {
			ServerStanzaRouter testling;
			MockServerSession session1(JID("foo@bar.com/Baz"), 8);
			testling.addClientSession(&session1);
			MockServerSession session2(JID("foo@bar.com/Bar"), 5);
			testling.addClientSession(&session2);

			session1.priority = 3;
			session2.priority = 4;
			bool result = testling.routeStanza(createMessageTo("foo@bar.com"));

			CPPUNIT_ASSERT(result);
			CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(session1.sentStanzas.size()));
			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(session2.sentStanzas.size()));
		}

	private:
		boost::shared_ptr<Message> createMessageTo(const String& recipient) {
			boost::shared_ptr<Message> message(new Message());
			message->setTo(JID(recipient));
			return message;
		}

		class MockServerSession : public ServerSession {
			public:
				MockServerSession(const JID& jid, int priority) : jid(jid), priority(priority) {}

				virtual const JID& getJID() const { return jid; }
				virtual int getPriority() const { return priority; }

				virtual void sendStanza(boost::shared_ptr<Stanza> stanza) {
					sentStanzas.push_back(stanza);
				}

				JID jid;
				int priority;
				std::vector< boost::shared_ptr<Stanza> > sentStanzas;
		};
};

CPPUNIT_TEST_SUITE_REGISTRATION(ServerStanzaRouterTest);