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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* TCPConnectorTest.cpp
* Test fixture for the TCPConnector class
* Copyright (C) 2012 Simon Newton
*/
#include <cppunit/extensions/HelperMacros.h>
#include <stdint.h>
#include <string.h>
#include <string>
#include "ola/Callback.h"
#include "ola/Clock.h"
#include "ola/Logging.h"
#include "ola/io/SelectServer.h"
#include "ola/network/IPV4Address.h"
#include "ola/network/NetworkUtils.h"
#include "ola/network/SocketAddress.h"
#include "ola/network/Socket.h"
#include "ola/network/TCPConnector.h"
#include "ola/network/TCPSocketFactory.h"
#include "ola/testing/TestUtils.h"
using ola::TimeInterval;
using ola::io::ConnectedDescriptor;
using ola::network::GenericSocketAddress;
using ola::network::IPV4Address;
using ola::network::IPV4SocketAddress;
using ola::io::SelectServer;
using ola::network::TCPConnector;
using ola::network::TCPAcceptingSocket;
using ola::network::TCPSocket;
using std::auto_ptr;
using std::string;
// used to set a timeout which aborts the tests
static const int CONNECT_TIMEOUT_IN_MS = 500;
static const int ABORT_TIMEOUT_IN_MS = 1000;
class TCPConnectorTest: public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(TCPConnectorTest);
CPPUNIT_TEST(testNonBlockingConnect);
CPPUNIT_TEST(testNonBlockingConnectFailure);
CPPUNIT_TEST(testNonBlockingConnectError);
CPPUNIT_TEST(testNonBlockingCancel);
CPPUNIT_TEST(testEarlyDestruction);
CPPUNIT_TEST_SUITE_END();
public:
TCPConnectorTest()
: CppUnit::TestFixture(),
m_localhost(IPV4Address::Loopback()) {
}
void setUp();
void tearDown();
void testNonBlockingConnect();
void testNonBlockingConnectFailure();
void testNonBlockingConnectError();
void testNonBlockingCancel();
void testEarlyDestruction();
// timing out indicates something went wrong
void Timeout() {
m_timeout_closure = NULL;
OLA_FAIL("timeout");
}
// Socket close actions
void TerminateOnClose() {
m_ss->Terminate();
}
private:
SelectServer *m_ss;
IPV4Address m_localhost;
ola::SingleUseCallback0<void> *m_timeout_closure;
unsigned int m_successful_calls;
unsigned int m_failure_calls;
void AcceptedConnection(TCPSocket *socket);
void OnConnect(int fd, int error);
void OnConnectFailure(int fd, int error);
uint16_t ReservePort();
};
CPPUNIT_TEST_SUITE_REGISTRATION(TCPConnectorTest);
/*
* Setup the select server
*/
void TCPConnectorTest::setUp() {
m_ss = new SelectServer();
m_timeout_closure = ola::NewSingleCallback(this, &TCPConnectorTest::Timeout);
m_successful_calls = 0;
m_failure_calls = 0;
OLA_ASSERT_TRUE(m_ss->RegisterSingleTimeout(ABORT_TIMEOUT_IN_MS,
m_timeout_closure));
#if _WIN32
WSADATA wsa_data;
int result = WSAStartup(MAKEWORD(2, 0), &wsa_data);
OLA_ASSERT_EQ(result, 0);
#endif // _WIN32
}
/*
* Cleanup the select server
*/
void TCPConnectorTest::tearDown() {
delete m_ss;
#ifdef _WIN32
WSACleanup();
#endif // _WIN32
}
/*
* Test non-blocking TCP connects work correctly.
*/
void TCPConnectorTest::testNonBlockingConnect() {
ola::network::TCPSocketFactory socket_factory(
ola::NewCallback(this, &TCPConnectorTest::AcceptedConnection));
TCPAcceptingSocket listening_socket(&socket_factory);
IPV4SocketAddress listen_address(m_localhost, 0);
OLA_ASSERT_TRUE_MSG(listening_socket.Listen(listen_address),
"Failed to listen");
GenericSocketAddress addr = listening_socket.GetLocalAddress();
OLA_ASSERT_TRUE(addr.IsValid());
// calling listen a second time should fail
OLA_ASSERT_FALSE(listening_socket.Listen(listen_address));
OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&listening_socket));
// Attempt a non-blocking connect
TCPConnector connector(m_ss);
TimeInterval connect_timeout(0, CONNECT_TIMEOUT_IN_MS * 1000);
TCPConnector::TCPConnectionID id = connector.Connect(
addr.V4Addr(),
connect_timeout,
ola::NewSingleCallback(this, &TCPConnectorTest::OnConnect));
if (id) {
OLA_ASSERT_EQ(1u, connector.ConnectionsPending());
m_ss->Run();
OLA_ASSERT_EQ(0u, connector.ConnectionsPending());
}
OLA_ASSERT_EQ(1u, m_successful_calls);
OLA_ASSERT_EQ(0u, m_failure_calls);
m_ss->RemoveReadDescriptor(&listening_socket);
}
/*
* Test a non-blocking TCP connect fails.
*/
void TCPConnectorTest::testNonBlockingConnectFailure() {
uint16_t port = ReservePort();
OLA_ASSERT_NE(0, port);
IPV4SocketAddress target(m_localhost, port);
// attempt a non-blocking connect, hopefully nothing is running on port 9010
TCPConnector connector(m_ss);
TimeInterval connect_timeout(0, CONNECT_TIMEOUT_IN_MS * 1000);
TCPConnector::TCPConnectionID id = connector.Connect(
target,
connect_timeout,
ola::NewSingleCallback(this, &TCPConnectorTest::OnConnectFailure));
// On platforms where connect() doesn't return EINPROGRESS, it's hard to
// actually test this without knowing a non-local address.
if (id) {
m_ss->Run();
OLA_ASSERT_EQ(0u, connector.ConnectionsPending());
}
OLA_ASSERT_EQ(0u, m_successful_calls);
OLA_ASSERT_EQ(1u, m_failure_calls);
}
/*
* Test a non-blocking TCP connect for an invalid port.
*/
void TCPConnectorTest::testNonBlockingConnectError() {
IPV4Address bcast_address;
OLA_ASSERT_TRUE(IPV4Address::FromString("255.255.255.255", &bcast_address));
TCPConnector connector(m_ss);
TimeInterval connect_timeout(0, CONNECT_TIMEOUT_IN_MS * 1000);
TCPConnector::TCPConnectionID id = connector.Connect(
IPV4SocketAddress(bcast_address, 0),
connect_timeout,
ola::NewSingleCallback(this, &TCPConnectorTest::OnConnectFailure));
OLA_ASSERT_FALSE(id);
OLA_ASSERT_EQ(0u, connector.ConnectionsPending());
OLA_ASSERT_EQ(0u, m_successful_calls);
OLA_ASSERT_EQ(1u, m_failure_calls);
}
/*
* Test that cancelling a connect works.
*/
void TCPConnectorTest::testNonBlockingCancel() {
uint16_t port = ReservePort();
OLA_ASSERT_NE(0, port);
IPV4SocketAddress target(m_localhost, port);
TCPConnector connector(m_ss);
TimeInterval connect_timeout(0, CONNECT_TIMEOUT_IN_MS * 1000);
TCPConnector::TCPConnectionID id = connector.Connect(
target,
connect_timeout,
ola::NewSingleCallback(this, &TCPConnectorTest::OnConnectFailure));
// On platforms where connect() doesn't return EINPROGRESS, it's hard to
// actually test this without knowing a non-local address.
if (id) {
OLA_ASSERT_EQ(1u, connector.ConnectionsPending());
OLA_ASSERT_TRUE(connector.Cancel(id));
OLA_ASSERT_EQ(0u, connector.ConnectionsPending());
}
OLA_ASSERT_EQ(0u, m_successful_calls);
OLA_ASSERT_EQ(1u, m_failure_calls);
}
/*
* Test that we can destroy the Connector and everything will work.
*/
void TCPConnectorTest::testEarlyDestruction() {
uint16_t port = ReservePort();
OLA_ASSERT_NE(0, port);
IPV4SocketAddress target(m_localhost, port);
// attempt a non-blocking connect.
TimeInterval connect_timeout(0, CONNECT_TIMEOUT_IN_MS * 1000);
{
TCPConnector connector(m_ss);
TCPConnector::TCPConnectionID id = connector.Connect(
target,
connect_timeout,
ola::NewSingleCallback(this, &TCPConnectorTest::OnConnectFailure));
if (id != 0) {
// The callback hasn't run yet.
OLA_ASSERT_EQ(1u, connector.ConnectionsPending());
m_ss->RunOnce(TimeInterval(1, 0));
}
OLA_ASSERT_EQ(0u, m_successful_calls);
OLA_ASSERT_EQ(1u, m_failure_calls);
}
}
/*
* Accept a new TCP connection.
*/
void TCPConnectorTest::AcceptedConnection(TCPSocket *new_socket) {
OLA_ASSERT_NOT_NULL(new_socket);
GenericSocketAddress address = new_socket->GetPeerAddress();
OLA_ASSERT_TRUE(address.Family() == AF_INET);
OLA_INFO << "Connection from " << address;
// terminate the ss when this connection is closed
new_socket->SetOnClose(
ola::NewSingleCallback(this, &TCPConnectorTest::TerminateOnClose));
m_ss->AddReadDescriptor(new_socket, true);
}
/**
* Called when a connection completes or times out.
*/
void TCPConnectorTest::OnConnect(int fd, int error) {
if (error) {
std::ostringstream str;
str << "Failed to connect: " << strerror(error);
OLA_ASSERT_EQ_MSG(0, error, str.str());
m_ss->Terminate();
} else {
OLA_ASSERT_TRUE(fd >= 0);
#ifdef _WIN32
closesocket(fd);
#else
close(fd);
#endif // _WIN32
}
m_successful_calls++;
}
/**
* Called when a connection completes or times out.
*/
void TCPConnectorTest::OnConnectFailure(int fd, int error) {
// The error could be one of many things, right now we just check it's non-0
OLA_ASSERT_NE(0, error);
OLA_ASSERT_EQ(-1, fd);
m_ss->Terminate();
m_failure_calls++;
}
/**
* For certain tests we need to ensure there isn't something listening on a TCP
* port. The best way I've come up with doing this is to bind to port 0, then
* close the socket. REUSE_ADDR means that the port shouldn't be allocated
* again for a while.
*/
uint16_t TCPConnectorTest::ReservePort() {
TCPAcceptingSocket listening_socket(NULL);
IPV4SocketAddress listen_address(m_localhost, 0);
OLA_ASSERT_TRUE_MSG(listening_socket.Listen(listen_address),
"Failed to listen");
GenericSocketAddress addr = listening_socket.GetLocalAddress();
OLA_ASSERT_TRUE(addr.IsValid());
return addr.V4Addr().Port();
}
|