summaryrefslogtreecommitdiffstats
blob: 484c5e336b71d333bc91530e1a38f6c09d725398 (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
/*
 * Copyright (c) 2010-2015 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <string>

#include <boost/shared_ptr.hpp>
#include <boost/smart_ptr/make_shared.hpp>

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

#include <Swiften/Base/Algorithm.h>
#include <Swiften/Base/sleep.h>
#include <Swiften/EventLoop/DummyEventLoop.h>
#include <Swiften/Network/BoostConnection.h>
#include <Swiften/Network/BoostIOServiceThread.h>
#include <Swiften/Network/HostAddress.h>
#include <Swiften/Network/HostAddressPort.h>

using namespace Swift;

class BoostConnectionTest : public CppUnit::TestFixture {
		CPPUNIT_TEST_SUITE(BoostConnectionTest);
		CPPUNIT_TEST(testDestructor);
		CPPUNIT_TEST(testDestructor_PendingEvents);
		CPPUNIT_TEST(testWrite);
		CPPUNIT_TEST(testWriteMultipleSimultaniouslyQueuesWrites);
#ifdef TEST_IPV6
		CPPUNIT_TEST(testWrite_IPv6);
#endif
		CPPUNIT_TEST_SUITE_END();

	public:
		void setUp() {
			eventLoop_ = new DummyEventLoop();
			boostIOServiceThread_ = new BoostIOServiceThread();
			boostIOService_ = boost::make_shared<boost::asio::io_service>();
			disconnected_ = false;
			connectFinished_ = false;
		}

		void tearDown() {
			delete boostIOServiceThread_;
			while (eventLoop_->hasEvents()) {
				eventLoop_->processEvents();
			}
			delete eventLoop_;
		}

		void testDestructor() {
			{
				BoostConnection::ref testling(BoostConnection::create(boostIOServiceThread_->getIOService(), eventLoop_));
				testling->connect(HostAddressPort(HostAddress(getenv("SWIFT_NETWORK_TEST_IPV4")), 5222));
			}
		}

		void testDestructor_PendingEvents() {
			{
				BoostConnection::ref testling(BoostConnection::create(boostIOServiceThread_->getIOService(), eventLoop_));
				testling->connect(HostAddressPort(HostAddress(getenv("SWIFT_NETWORK_TEST_IPV4")), 5222));
				while (!eventLoop_->hasEvents()) {
					Swift::sleep(10);
				}
			}
			eventLoop_->processEvents();
		}

		void testWrite() {
			BoostConnection::ref testling(BoostConnection::create(boostIOServiceThread_->getIOService(), eventLoop_));
			testling->onConnectFinished.connect(boost::bind(&BoostConnectionTest::doWrite, this, testling.get()));
			testling->onDataRead.connect(boost::bind(&BoostConnectionTest::handleDataRead, this, _1));
			testling->onDisconnected.connect(boost::bind(&BoostConnectionTest::handleDisconnected, this));
			testling->connect(HostAddressPort(HostAddress(getenv("SWIFT_NETWORK_TEST_IPV4")), 5222));
			while (receivedData_.empty()) {
				Swift::sleep(10);
				eventLoop_->processEvents();
			}
			testling->disconnect();
		}

		void testWrite_IPv6() {
			BoostConnection::ref testling(BoostConnection::create(boostIOServiceThread_->getIOService(), eventLoop_));
			testling->onConnectFinished.connect(boost::bind(&BoostConnectionTest::doWrite, this, testling.get()));
			testling->onDataRead.connect(boost::bind(&BoostConnectionTest::handleDataRead, this, _1));
			testling->onDisconnected.connect(boost::bind(&BoostConnectionTest::handleDisconnected, this));
			testling->connect(HostAddressPort(HostAddress(getenv("SWIFT_NETWORK_TEST_IPV6")), 80));
			while (receivedData_.empty()) {
				Swift::sleep(10);
				eventLoop_->processEvents();
			}
			testling->disconnect();
		}


		void testWriteMultipleSimultaniouslyQueuesWrites() {
			BoostConnection::ref testling(BoostConnection::create(boostIOService_, eventLoop_));
			testling->onConnectFinished.connect(boost::bind(&BoostConnectionTest::handleConnectFinished, this));
			testling->onDataRead.connect(boost::bind(&BoostConnectionTest::handleDataRead, this, _1));
			testling->onDisconnected.connect(boost::bind(&BoostConnectionTest::handleDisconnected, this));
			testling->connect(HostAddressPort(HostAddress(getenv("SWIFT_NETWORK_TEST_IPV4")), 5222));
			while (!connectFinished_) {
				boostIOService_->run_one();
				eventLoop_->processEvents();
			}

			testling->write(createSafeByteArray("<stream:strea"));
			testling->write(createSafeByteArray("m"));
			testling->write(createSafeByteArray(">"));

			 // Check that we only did one write event, the others are queued
			/*int runHandlers = */boostIOService_->poll();
			// Disabling this test, because poll runns all handlers that are added during poll() as well, so
			// this test doesn't really work any more. We'll have to trust that things are queued.
			//CPPUNIT_ASSERT_EQUAL(1, runHandlers);
			// Process the other events
			while (receivedData_.empty()) {
				boostIOService_->run_one();
				eventLoop_->processEvents();
			}

			// Disconnect & clean up
			testling->disconnect();
			while (!disconnected_) {
				boostIOService_->run_one();
				eventLoop_->processEvents();
			}
		}

		void doWrite(BoostConnection* connection) {
			connection->write(createSafeByteArray("<stream:stream>"));
			connection->write(createSafeByteArray("\r\n\r\n")); // Temporarily, while we don't have an xmpp server running on ipv6
		}

		void handleDataRead(boost::shared_ptr<SafeByteArray> data) {
			append(receivedData_, *data);
		}

		void handleDisconnected() {
			disconnected_ = true;
		}

		void handleConnectFinished() {
			connectFinished_ = true;
		}

	private:
		BoostIOServiceThread* boostIOServiceThread_;
		boost::shared_ptr<boost::asio::io_service> boostIOService_;
		DummyEventLoop* eventLoop_;
		ByteArray receivedData_;
		bool disconnected_;
		bool connectFinished_;
};

CPPUNIT_TEST_SUITE_REGISTRATION(BoostConnectionTest);