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

#include <memory>
#include <string>

#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(testIPv6DualStackServerPeerAddress);
        CPPUNIT_TEST_SUITE_END();

    public:
        void setUp() {
            eventLoop_ = new DummyEventLoop();
            boostIOServiceThread_ = new BoostIOServiceThread();
            stopped_ = false;
            stoppedError_.reset();
            receivedNewConnection_ = false;
            connectFinished_ = false;
            remoteAddress_ = boost::optional<HostAddressPort>();
        }

        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::fromString("127.0.0.1").get(), 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::fromString("127.0.0.1").get(), 9999));

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

            CPPUNIT_ASSERT_EQUAL(true, receivedNewConnection_);

            testling->stop();
        }

        void testIPv6Server() {
            BoostConnectionServer::ref testling = BoostConnectionServer::create(HostAddress::fromString("::1").get(), 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::fromString("::1").get(), 9999));

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

            CPPUNIT_ASSERT_EQUAL(true, receivedNewConnection_);

            testling->stop();
        }

        void testIPv4IPv6DualStackServer() {
            BoostConnectionServer::ref testling = BoostConnectionServer::create(HostAddress::fromString("::").get(), 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::fromString("127.0.0.1").get(), 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::fromString("::1").get(), 9999));

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

            CPPUNIT_ASSERT_EQUAL(true, receivedNewConnection_);

            testling->stop();
        }

        void testIPv6DualStackServerPeerAddress() {
            BoostConnectionServer::ref testling = BoostConnectionServer::create(HostAddress::fromString("::").get(), 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::fromString("127.0.0.1").get(), 9999));

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

            CPPUNIT_ASSERT_EQUAL(true, receivedNewConnection_);
            // The IPv4 localhost mapped to a IPv6 address is expected here.
            CPPUNIT_ASSERT(HostAddress::fromString("::ffff:127.0.0.1").get() == remoteAddress_.get().getAddress());

            receivedNewConnection_ = false;
            connectFinished_ = false;
            remoteAddress_ = boost::optional<HostAddressPort>();

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

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

            CPPUNIT_ASSERT_EQUAL(true, receivedNewConnection_);
            // The IPv6 local host is expected here.
            CPPUNIT_ASSERT(HostAddress::fromString("::1").get() == remoteAddress_.get().getAddress());

            testling->stop();
        }

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

        void handleNewConnection(std::shared_ptr<Connection> connection) {
            receivedNewConnection_ = true;
            remoteAddress_ = connection->getRemoteAddress();
        }

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

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

CPPUNIT_TEST_SUITE_REGISTRATION(BoostConnectionServerTest);