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

#include <string>

#include <boost/shared_ptr.hpp>

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

#include <Swiften/Base/sleep.h>
#include <Swiften/EventLoop/DummyEventLoop.h>
#include <Swiften/Network/BoostConnectionServer.h>
#include <Swiften/Network/BoostIOServiceThread.h>

using namespace Swift;

class BoostConnectionServerTest : public CppUnit::TestFixture {
		CPPUNIT_TEST_SUITE(BoostConnectionServerTest);
		CPPUNIT_TEST(testConstructor_TwoServersOnSamePort);
		CPPUNIT_TEST(testStart_Conflict);
		CPPUNIT_TEST(testStop);
		CPPUNIT_TEST(testIPv4Server);
		CPPUNIT_TEST(testIPv6Server);
		CPPUNIT_TEST(testIPv4IPv6DualStackServer);
		CPPUNIT_TEST_SUITE_END();

	public:
		void setUp() {
			eventLoop_ = new DummyEventLoop();
			boostIOServiceThread_ = new BoostIOServiceThread();
			stopped_ = false;
			stoppedError_.reset();
			receivedNewConnection_ = false;
			connectFinished_ = false;
		}

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

		void testConstructor_TwoServersOnSamePort() {
			BoostConnectionServer::ref testling(BoostConnectionServer::create(9999, boostIOServiceThread_->getIOService(), eventLoop_));
			BoostConnectionServer::ref testling2(BoostConnectionServer::create(9999, boostIOServiceThread_->getIOService(), eventLoop_));
		}

		void testStart_Conflict() {
			BoostConnectionServer::ref testling(BoostConnectionServer::create(9999, boostIOServiceThread_->getIOService(), eventLoop_));
			testling->start();

			BoostConnectionServer::ref testling2(BoostConnectionServer::create(9999, boostIOServiceThread_->getIOService(), eventLoop_));
			testling2->onStopped.connect(
					boost::bind(&BoostConnectionServerTest::handleStopped_, this, _1));

			testling->stop();
		}

		void testStop() {
			BoostConnectionServer::ref testling(BoostConnectionServer::create(9999, boostIOServiceThread_->getIOService(), eventLoop_));
			testling->start();

			testling->stop();

			BoostConnectionServer::ref testling2(BoostConnectionServer::create(9999, boostIOServiceThread_->getIOService(), eventLoop_));
			testling2->start();

			testling2->stop();
		}

		void testIPv4Server() {
			BoostConnectionServer::ref testling = BoostConnectionServer::create(HostAddress("127.0.0.1"), 9999, boostIOServiceThread_->getIOService(), eventLoop_);
			testling->onNewConnection.connect(boost::bind(&BoostConnectionServerTest::handleNewConnection, this, _1));
			testling->start();

			BoostConnection::ref clientTestling = BoostConnection::create(boostIOServiceThread_->getIOService(), eventLoop_);
			clientTestling->onConnectFinished.connect(boost::bind(&BoostConnectionServerTest::handleConnectFinished, this, _1));
			clientTestling->connect(HostAddressPort(HostAddress("127.0.0.1"), 9999));

			while (!connectFinished_) {
				Swift::sleep(10);
				eventLoop_->processEvents();
			}

			CPPUNIT_ASSERT_EQUAL(true, receivedNewConnection_);

			testling->stop();
		}

		void testIPv6Server() {
			BoostConnectionServer::ref testling = BoostConnectionServer::create(HostAddress("::1"), 9999, boostIOServiceThread_->getIOService(), eventLoop_);
			testling->onNewConnection.connect(boost::bind(&BoostConnectionServerTest::handleNewConnection, this, _1));
			testling->start();

			BoostConnection::ref clientTestling = BoostConnection::create(boostIOServiceThread_->getIOService(), eventLoop_);
			clientTestling->onConnectFinished.connect(boost::bind(&BoostConnectionServerTest::handleConnectFinished, this, _1));
			clientTestling->connect(HostAddressPort(HostAddress("::1"), 9999));

			while (!connectFinished_) {
				Swift::sleep(10);
				eventLoop_->processEvents();
			}

			CPPUNIT_ASSERT_EQUAL(true, receivedNewConnection_);

			testling->stop();
		}

		void testIPv4IPv6DualStackServer() {
			BoostConnectionServer::ref testling = BoostConnectionServer::create(HostAddress("::"), 9999, boostIOServiceThread_->getIOService(), eventLoop_);
			testling->onNewConnection.connect(boost::bind(&BoostConnectionServerTest::handleNewConnection, this, _1));
			testling->start();

			// Test IPv4.
			BoostConnection::ref clientTestling = BoostConnection::create(boostIOServiceThread_->getIOService(), eventLoop_);
			clientTestling->onConnectFinished.connect(boost::bind(&BoostConnectionServerTest::handleConnectFinished, this, _1));
			clientTestling->connect(HostAddressPort(HostAddress("127.0.0.1"), 9999));

			while (!connectFinished_) {
				Swift::sleep(10);
				eventLoop_->processEvents();
			}

			CPPUNIT_ASSERT_EQUAL(true, receivedNewConnection_);

			receivedNewConnection_ = false;
			connectFinished_ = false;

			// Test IPv6.
			clientTestling = BoostConnection::create(boostIOServiceThread_->getIOService(), eventLoop_);
			clientTestling->onConnectFinished.connect(boost::bind(&BoostConnectionServerTest::handleConnectFinished, this, _1));
			clientTestling->connect(HostAddressPort(HostAddress("::1"), 9999));

			while (!connectFinished_) {
				Swift::sleep(10);
				eventLoop_->processEvents();
			}

			CPPUNIT_ASSERT_EQUAL(true, receivedNewConnection_);

			testling->stop();
		}

		void handleStopped_(boost::optional<BoostConnectionServer::Error> e) {
			stopped_ = true;
			stoppedError_ = e;
		}

		void handleNewConnection(boost::shared_ptr<Connection> /*connection*/) {
			receivedNewConnection_ = true;
		}

		void handleConnectFinished(bool /*error*/) {
			connectFinished_ = true;
		}

	private:
		BoostIOServiceThread* boostIOServiceThread_;
		DummyEventLoop* eventLoop_;
		bool stopped_;
		bool receivedNewConnection_;
		bool connectFinished_;
		boost::optional<BoostConnectionServer::Error> stoppedError_;
};

CPPUNIT_TEST_SUITE_REGISTRATION(BoostConnectionServerTest);