summaryrefslogtreecommitdiffstats
blob: 349c2823e44f01f3b6249d955913d3d9a7cef2d7 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
 * Copyright (c) 2011-2015 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <boost/bind.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/optional.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/smart_ptr/make_shared.hpp>

#include <QA/Checker/IO.h>

#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>

#include <Swiften/Base/Algorithm.h>
#include <Swiften/EventLoop/DummyEventLoop.h>
#include <Swiften/Network/BOSHConnection.h>
#include <Swiften/Network/Connection.h>
#include <Swiften/Network/ConnectionFactory.h>
#include <Swiften/Network/DummyTimerFactory.h>
#include <Swiften/Network/HostAddressPort.h>
#include <Swiften/Network/StaticDomainNameResolver.h>
#include <Swiften/Parser/PlatformXMLParserFactory.h>
#include <Swiften/TLS/TLSOptions.h>

using namespace Swift;

class BOSHConnectionTest : public CppUnit::TestFixture {
	CPPUNIT_TEST_SUITE(BOSHConnectionTest);
	CPPUNIT_TEST(testHeader);
	CPPUNIT_TEST(testReadiness_ok);
	CPPUNIT_TEST(testReadiness_pending);
	CPPUNIT_TEST(testReadiness_disconnect);
	CPPUNIT_TEST(testReadiness_noSID);
	CPPUNIT_TEST(testWrite_Receive);
	CPPUNIT_TEST(testWrite_ReceiveTwice);
	CPPUNIT_TEST(testRead_Fragment);
	CPPUNIT_TEST(testHTTPRequest);
	CPPUNIT_TEST(testHTTPRequest_Empty);
	CPPUNIT_TEST_SUITE_END();

	public:
		void setUp() {
			eventLoop = new DummyEventLoop();
			connectionFactory = new MockConnectionFactory(eventLoop);
			resolver = new StaticDomainNameResolver(eventLoop);
			timerFactory = new DummyTimerFactory();
			tlsContextFactory = NULL;
			connectFinished = false;
			disconnected = false;
			disconnectedError = false;
			dataRead.clear();
		}

		void tearDown() {
			eventLoop->processEvents();
			delete connectionFactory;
			delete resolver;
			delete timerFactory;
			delete eventLoop;
		}

		void testHeader() {
			BOSHConnection::ref testling = createTestling();
			testling->connect();
			eventLoop->processEvents();
			testling->startStream("wonderland.lit", 1);
			std::string initial("<body wait='60' "
								"inactivity='30' "
								"polling='5' "
								"requests='2' "
								"hold='1' "
								"maxpause='120' "
								"sid='MyShinySID' "
								"ver='1.6' "
								"from='wonderland.lit' "
								"xmlns='http://jabber.org/protocol/httpbind'/>");
			readResponse(initial, connectionFactory->connections[0]);
			CPPUNIT_ASSERT_EQUAL(std::string("MyShinySID"), sid);
			CPPUNIT_ASSERT(testling->isReadyToSend());
		}

		void testReadiness_ok() {
			BOSHConnection::ref testling = createTestling();
			testling->connect();
			eventLoop->processEvents();
			testling->setSID("blahhhhh");
			CPPUNIT_ASSERT(testling->isReadyToSend());
		}

		void testReadiness_pending() {
			BOSHConnection::ref testling = createTestling();
			testling->connect();
			eventLoop->processEvents();
			testling->setSID("mySID");
			CPPUNIT_ASSERT(testling->isReadyToSend());
			testling->write(createSafeByteArray("<mypayload/>"));
			CPPUNIT_ASSERT(!testling->isReadyToSend());
			readResponse("<body><blah/></body>", connectionFactory->connections[0]);
			CPPUNIT_ASSERT(testling->isReadyToSend());
		}

		void testReadiness_disconnect() {
			BOSHConnection::ref testling = createTestling();
			testling->connect();
			eventLoop->processEvents();
			testling->setSID("mySID");
			CPPUNIT_ASSERT(testling->isReadyToSend());
			connectionFactory->connections[0]->onDisconnected(boost::optional<Connection::Error>());
			CPPUNIT_ASSERT(!testling->isReadyToSend());
		}


		void testReadiness_noSID() {
			BOSHConnection::ref testling = createTestling();
			testling->connect();
			eventLoop->processEvents();
			CPPUNIT_ASSERT(!testling->isReadyToSend());
		}

		void testWrite_Receive() {
			BOSHConnection::ref testling = createTestling();
			testling->connect();
			eventLoop->processEvents();
			testling->setSID("mySID");
			testling->write(createSafeByteArray("<mypayload/>"));
			readResponse("<body><blah/></body>", connectionFactory->connections[0]);
			CPPUNIT_ASSERT_EQUAL(std::string("<blah/>"), byteArrayToString(dataRead));

		}

		void testWrite_ReceiveTwice() {
			BOSHConnection::ref testling = createTestling();
			testling->connect();
			eventLoop->processEvents();
			testling->setSID("mySID");
			testling->write(createSafeByteArray("<mypayload/>"));
			readResponse("<body><blah/></body>", connectionFactory->connections[0]);
			CPPUNIT_ASSERT_EQUAL(std::string("<blah/>"), byteArrayToString(dataRead));
			dataRead.clear();
			testling->write(createSafeByteArray("<mypayload2/>"));
			readResponse("<body><bleh/></body>", connectionFactory->connections[0]);
			CPPUNIT_ASSERT_EQUAL(std::string("<bleh/>"), byteArrayToString(dataRead));
		}

		void testRead_Fragment() {
			BOSHConnection::ref testling = createTestling();
			testling->connect();
			eventLoop->processEvents();
			CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), connectionFactory->connections.size());
			boost::shared_ptr<MockConnection> connection = connectionFactory->connections[0];
			boost::shared_ptr<SafeByteArray> data1 = boost::make_shared<SafeByteArray>(createSafeByteArray(
				"HTTP/1.1 200 OK\r\n"
				"Content-Type: text/xml; charset=utf-8\r\n"
				"Access-Control-Allow-Origin: *\r\n"
				"Access-Control-Allow-Headers: Content-Type\r\n"
				"Content-Length: 64\r\n"));
			boost::shared_ptr<SafeByteArray> data2 = boost::make_shared<SafeByteArray>(createSafeByteArray(
				"\r\n<body xmlns='http://jabber.org/protocol/httpbind'>"
				"<bl"));
			boost::shared_ptr<SafeByteArray> data3 = boost::make_shared<SafeByteArray>(createSafeByteArray(
				"ah/>"
				"</body>"));
			connection->onDataRead(data1);
			connection->onDataRead(data2);
			CPPUNIT_ASSERT(dataRead.empty());
			connection->onDataRead(data3);
			CPPUNIT_ASSERT_EQUAL(std::string("<blah/>"), byteArrayToString(dataRead));
		}

		void testHTTPRequest() {
			std::string data = "<blah/>";
			std::string sid = "wigglebloom";
			std::string fullBody = "<body xmlns='http://jabber.org/protocol/httpbind' sid='" + sid + "' rid='20'>" + data + "</body>";
			std::pair<SafeByteArray, size_t> http = BOSHConnection::createHTTPRequest(createSafeByteArray(data), false, false, 20, sid, URL());
			CPPUNIT_ASSERT_EQUAL(fullBody.size(), http.second);
		}

		void testHTTPRequest_Empty() {
			std::string data = "";
			std::string sid = "wigglebloomsickle";
			std::string fullBody = "<body rid='42' sid='" + sid + "' xmlns='http://jabber.org/protocol/httpbind'>" + data + "</body>";
			std::pair<SafeByteArray, size_t> http = BOSHConnection::createHTTPRequest(createSafeByteArray(data), false, false, 42, sid, URL());
			CPPUNIT_ASSERT_EQUAL(fullBody.size(), http.second);
			std::string response = safeByteArrayToString(http.first);
			size_t bodyPosition = response.find("\r\n\r\n");
			CPPUNIT_ASSERT_EQUAL(fullBody, response.substr(bodyPosition+4));
		}

	private:

		BOSHConnection::ref createTestling() {
			resolver->addAddress("wonderland.lit", HostAddress("127.0.0.1"));
			Connector::ref connector = Connector::create("wonderland.lit", 5280, boost::optional<std::string>(), resolver, connectionFactory, timerFactory);
			BOSHConnection::ref c = BOSHConnection::create(URL("http", "wonderland.lit", 5280, "/http-bind"), connector, &parserFactory, tlsContextFactory, TLSOptions());
			c->onConnectFinished.connect(boost::bind(&BOSHConnectionTest::handleConnectFinished, this, _1));
			c->onDisconnected.connect(boost::bind(&BOSHConnectionTest::handleDisconnected, this, _1));
			c->onXMPPDataRead.connect(boost::bind(&BOSHConnectionTest::handleDataRead, this, _1));
			c->onSessionStarted.connect(boost::bind(&BOSHConnectionTest::handleSID, this, _1));
			c->setRID(42);
			return c;
		}

		void handleConnectFinished(bool error) {
			connectFinished = true;
			connectFinishedWithError = error;
		}

		void handleDisconnected(bool e) {
			disconnected = true;
			disconnectedError = e;
		}

		void handleDataRead(const SafeByteArray& d) {
			append(dataRead, d);
		}

		void handleSID(const std::string& s) {
			sid = s;
		}

		struct MockConnection : public Connection {
			public:
				MockConnection(const std::vector<HostAddressPort>& failingPorts, EventLoop* eventLoop) : eventLoop(eventLoop), failingPorts(failingPorts), disconnected(false) {
				}

				void listen() { assert(false); }

				void connect(const HostAddressPort& address) {
					hostAddressPort = address;
					bool fail = std::find(failingPorts.begin(), failingPorts.end(), address) != failingPorts.end();
					eventLoop->postEvent(boost::bind(boost::ref(onConnectFinished), fail));
				}

				HostAddressPort getLocalAddress() const { return HostAddressPort(); }

				void disconnect() { 
					disconnected = true;
					onDisconnected(boost::optional<Connection::Error>());
				}
				
				void write(const SafeByteArray& d) {
					append(dataWritten, d);
				}

				EventLoop* eventLoop;
				boost::optional<HostAddressPort> hostAddressPort;
				std::vector<HostAddressPort> failingPorts;
				ByteArray dataWritten;
				bool disconnected;
		};

		struct MockConnectionFactory : public ConnectionFactory {
			MockConnectionFactory(EventLoop* eventLoop) : eventLoop(eventLoop) {
			}

			boost::shared_ptr<Connection> createConnection() {
				boost::shared_ptr<MockConnection> connection = boost::make_shared<MockConnection>(failingPorts, eventLoop);
				connections.push_back(connection);
				return connection;
			}

			EventLoop* eventLoop;
			std::vector< boost::shared_ptr<MockConnection> > connections;
			std::vector<HostAddressPort> failingPorts;
		};

		void readResponse(const std::string& response, boost::shared_ptr<MockConnection> connection) {
			boost::shared_ptr<SafeByteArray> data1 = boost::make_shared<SafeByteArray>(createSafeByteArray(
				"HTTP/1.1 200 OK\r\n"
				"Content-Type: text/xml; charset=utf-8\r\n"
				"Access-Control-Allow-Origin: *\r\n"
				"Access-Control-Allow-Headers: Content-Type\r\n"
				"Content-Length: "));
			connection->onDataRead(data1);
			boost::shared_ptr<SafeByteArray> data2 = boost::make_shared<SafeByteArray>(createSafeByteArray(boost::lexical_cast<std::string>(response.size())));
			connection->onDataRead(data2);
			boost::shared_ptr<SafeByteArray> data3 = boost::make_shared<SafeByteArray>(createSafeByteArray("\r\n\r\n"));
			connection->onDataRead(data3);
			boost::shared_ptr<SafeByteArray> data4 = boost::make_shared<SafeByteArray>(createSafeByteArray(response));
			connection->onDataRead(data4);
		}


	private:
		DummyEventLoop* eventLoop;
		MockConnectionFactory* connectionFactory;
		bool connectFinished;
		bool connectFinishedWithError;
		bool disconnected;
		bool disconnectedError;
		ByteArray dataRead;
		PlatformXMLParserFactory parserFactory;
		StaticDomainNameResolver* resolver;
		TimerFactory* timerFactory;
		TLSContextFactory* tlsContextFactory;
		std::string sid;	

};


CPPUNIT_TEST_SUITE_REGISTRATION(BOSHConnectionTest);