summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobias Markmann <tm@ayena.de>2016-11-08 14:29:17 (GMT)
committerTobias Markmann <tm@ayena.de>2016-11-18 08:49:39 (GMT)
commit43479ef719ea8fc6abbf654730b47c4583140508 (patch)
treec0a05a837b8988c0875fedb6161c08f3dcb2ffb0 /Swiften/Network/UnitTest
parentc82f95fd431e702137d5f2e3dda4cf0ae424e837 (diff)
downloadswift-43479ef719ea8fc6abbf654730b47c4583140508.zip
swift-43479ef719ea8fc6abbf654730b47c4583140508.tar.bz2
Improve string to HostAddress conversion API
Previously HostAddress had a constructor which allowed initialisation via a std::string. This initialisation can fail and this is heavily used for checking whether a string is a valid IP address. This constructor is removed in this commit and replaced by a static method HostAddress::fromString, taking a string and returning an optional HostAddress. This clearly communicates that the conversion can fail. Test-Information: ./scons test=all passes on macOS 10.12.1. Change-Id: Idaafee6f84010ce541c55f267ac77ad6ac8f02b4
Diffstat (limited to 'Swiften/Network/UnitTest')
-rw-r--r--Swiften/Network/UnitTest/BOSHConnectionPoolTest.cpp4
-rw-r--r--Swiften/Network/UnitTest/BOSHConnectionTest.cpp2
-rw-r--r--Swiften/Network/UnitTest/ChainedConnectorTest.cpp2
-rw-r--r--Swiften/Network/UnitTest/ConnectorTest.cpp16
-rw-r--r--Swiften/Network/UnitTest/HTTPConnectProxiedConnectionTest.cpp30
-rw-r--r--Swiften/Network/UnitTest/HostAddressTest.cpp10
6 files changed, 31 insertions, 33 deletions
diff --git a/Swiften/Network/UnitTest/BOSHConnectionPoolTest.cpp b/Swiften/Network/UnitTest/BOSHConnectionPoolTest.cpp
index 8dbd09e..5d6fedd 100644
--- a/Swiften/Network/UnitTest/BOSHConnectionPoolTest.cpp
+++ b/Swiften/Network/UnitTest/BOSHConnectionPoolTest.cpp
@@ -69,7 +69,7 @@ class BOSHConnectionPoolTest : public CppUnit::TestFixture {
boshDataRead.clear();
boshDataWritten.clear();
resolver = new StaticDomainNameResolver(eventLoop);
- resolver->addAddress(to, HostAddress("127.0.0.1"));
+ resolver->addAddress(to, HostAddress::fromString("127.0.0.1").get());
timerFactory = new DummyTimerFactory();
}
@@ -234,7 +234,7 @@ class BOSHConnectionPoolTest : public CppUnit::TestFixture {
void testSession() {
to = "prosody.doomsong.co.uk";
- resolver->addAddress("prosody.doomsong.co.uk", HostAddress("127.0.0.1"));
+ resolver->addAddress("prosody.doomsong.co.uk", HostAddress::fromString("127.0.0.1").get());
path = "/http-bind/";
boshURL = URL("http", to, 5280, path);
diff --git a/Swiften/Network/UnitTest/BOSHConnectionTest.cpp b/Swiften/Network/UnitTest/BOSHConnectionTest.cpp
index a791e96..99dd462 100644
--- a/Swiften/Network/UnitTest/BOSHConnectionTest.cpp
+++ b/Swiften/Network/UnitTest/BOSHConnectionTest.cpp
@@ -193,7 +193,7 @@ class BOSHConnectionTest : public CppUnit::TestFixture {
private:
BOSHConnection::ref createTestling() {
- resolver->addAddress("wonderland.lit", HostAddress("127.0.0.1"));
+ resolver->addAddress("wonderland.lit", HostAddress::fromString("127.0.0.1").get());
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));
diff --git a/Swiften/Network/UnitTest/ChainedConnectorTest.cpp b/Swiften/Network/UnitTest/ChainedConnectorTest.cpp
index 3fad433..2d78cd7 100644
--- a/Swiften/Network/UnitTest/ChainedConnectorTest.cpp
+++ b/Swiften/Network/UnitTest/ChainedConnectorTest.cpp
@@ -34,7 +34,7 @@ class ChainedConnectorTest : public CppUnit::TestFixture {
public:
void setUp() {
error.reset();
- host = HostAddressPort(HostAddress("1.1.1.1"), 1234);
+ host = HostAddressPort(HostAddress::fromString("1.1.1.1").get(), 1234);
eventLoop = new DummyEventLoop();
resolver = new StaticDomainNameResolver(eventLoop);
resolver->addXMPPClientService("foo.com", host);
diff --git a/Swiften/Network/UnitTest/ConnectorTest.cpp b/Swiften/Network/UnitTest/ConnectorTest.cpp
index 20ad68d..8524439 100644
--- a/Swiften/Network/UnitTest/ConnectorTest.cpp
+++ b/Swiften/Network/UnitTest/ConnectorTest.cpp
@@ -43,9 +43,9 @@ class ConnectorTest : public CppUnit::TestFixture {
public:
void setUp() {
- host1 = HostAddressPort(HostAddress("1.1.1.1"), 1234);
- host2 = HostAddressPort(HostAddress("2.2.2.2"), 2345);
- host3 = HostAddressPort(HostAddress("3.3.3.3"), 5222);
+ host1 = HostAddressPort(HostAddress::fromString("1.1.1.1").get(), 1234);
+ host2 = HostAddressPort(HostAddress::fromString("2.2.2.2").get(), 2345);
+ host3 = HostAddressPort(HostAddress::fromString("3.3.3.3").get(), 5222);
eventLoop = new DummyEventLoop();
resolver = new StaticDomainNameResolver(eventLoop);
connectionFactory = new MockConnectionFactory(eventLoop);
@@ -122,8 +122,8 @@ class ConnectorTest : public CppUnit::TestFixture {
void testConnect_FirstAddressHostFails() {
Connector::ref testling(createConnector());
- HostAddress address1("1.1.1.1");
- HostAddress address2("2.2.2.2");
+ auto address1 = HostAddress::fromString("1.1.1.1").get();
+ auto address2 = HostAddress::fromString("2.2.2.2").get();
resolver->addXMPPClientService("foo.com", "host-foo.com", 1234);
resolver->addAddress("host-foo.com", address1);
resolver->addAddress("host-foo.com", address2);
@@ -245,10 +245,10 @@ class ConnectorTest : public CppUnit::TestFixture {
Connector::ref testling(createConnector());
testling->setTimeoutMilliseconds(10);
+ auto address2 = HostAddress::fromString("2.2.2.2").get();
+
resolver->addXMPPClientService("foo.com", "host-foo.com", 1234);
- HostAddress address1("1.1.1.1");
- resolver->addAddress("host-foo.com", address1);
- HostAddress address2("2.2.2.2");
+ resolver->addAddress("host-foo.com", HostAddress::fromString("1.1.1.1").get());
resolver->addAddress("host-foo.com", address2);
connectionFactory->isResponsive = false;
diff --git a/Swiften/Network/UnitTest/HTTPConnectProxiedConnectionTest.cpp b/Swiften/Network/UnitTest/HTTPConnectProxiedConnectionTest.cpp
index 232847b..1a160b7 100644
--- a/Swiften/Network/UnitTest/HTTPConnectProxiedConnectionTest.cpp
+++ b/Swiften/Network/UnitTest/HTTPConnectProxiedConnectionTest.cpp
@@ -18,7 +18,6 @@
#include <Swiften/Base/Algorithm.h>
#include <Swiften/Base/Log.h>
-#include <Swiften/Base/foreach.h>
#include <Swiften/EventLoop/DummyEventLoop.h>
#include <Swiften/Network/Connection.h>
#include <Swiften/Network/ConnectionFactory.h>
@@ -65,8 +64,7 @@ namespace {
int statusCode = boost::lexical_cast<int>(statusLineFields[1]);
if (statusCode == 407) {
- typedef std::pair<std::string, std::string> StrPair;
- foreach (const StrPair& field, response) {
+ for (const auto& field : response) {
if (to_lower(field.first) == to_lower("Proxy-Authenticate")) {
if (field.second.size() >= 6 && field.second.substr(0, 6) == " NTLM ") {
filterResponseReturn.push_back(std::pair<std::string, std::string>("Proxy-Authorization", "NTLM TlRMTVNTUAADAAAAGAAYAHIAAAAYABgAigAAABIAEgBIAAAABgAGAFoAAAASABIVNTUAADAAYAAAABAAEACiAAAANYKI4gUBKAoAAAAPTABBAEIAUwBNAE8ASwBFADMAXwBxAGEATABBAEIAUwBNAE8ASwBFADMA0NKq8HYYhj8AAAAAAAAAAAAAAAAAAAAAOIiih3mR+AkyM4r99sy1mdFonCu2ILODro1WTTrJ4b4JcXEzUBA2Ig=="));
@@ -108,8 +106,8 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture {
void setUp() {
proxyHost = "doo.bah";
proxyPort = 1234;
- proxyHostAddress = HostAddressPort(HostAddress("1.1.1.1"), proxyPort);
- host = HostAddressPort(HostAddress("2.2.2.2"), 2345);
+ proxyHostAddress = HostAddressPort(HostAddress::fromString("1.1.1.1").get(), proxyPort);
+ host = HostAddressPort(HostAddress::fromString("2.2.2.2").get(), 2345);
eventLoop = new DummyEventLoop();
resolver = new StaticDomainNameResolver(eventLoop);
resolver->addAddress(proxyHost, proxyHostAddress.getAddress());
@@ -148,14 +146,14 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture {
void testConnect_SendsConnectRequest() {
HTTPConnectProxiedConnection::ref testling(createTestling());
- connect(testling, HostAddressPort(HostAddress("2.2.2.2"), 2345));
+ connect(testling, HostAddressPort(HostAddress::fromString("2.2.2.2").get(), 2345));
CPPUNIT_ASSERT_EQUAL(createByteArray("CONNECT 2.2.2.2:2345 HTTP/1.1\r\n\r\n"), connectionFactory->connections[0]->dataWritten);
}
void testConnect_ReceiveConnectResponse() {
HTTPConnectProxiedConnection::ref testling(createTestling());
- connect(testling, HostAddressPort(HostAddress("2.2.2.2"), 2345));
+ connect(testling, HostAddressPort(HostAddress::fromString("2.2.2.2").get(), 2345));
connectionFactory->connections[0]->onDataRead(createSafeByteArrayRef("HTTP/1.0 200 Connection established\r\n\r\n"));
eventLoop->processEvents();
@@ -167,7 +165,7 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture {
void testConnect_ReceiveConnectChunkedResponse() {
HTTPConnectProxiedConnection::ref testling(createTestling());
- connect(testling, HostAddressPort(HostAddress("2.2.2.2"), 2345));
+ connect(testling, HostAddressPort(HostAddress::fromString("2.2.2.2").get(), 2345));
connectionFactory->connections[0]->onDataRead(createSafeByteArrayRef("HTTP/1.0 "));
eventLoop->processEvents();
@@ -182,7 +180,7 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture {
void testConnect_ReceiveMalformedConnectResponse() {
HTTPConnectProxiedConnection::ref testling(createTestling());
- connect(testling, HostAddressPort(HostAddress("2.2.2.2"), 2345));
+ connect(testling, HostAddressPort(HostAddress::fromString("2.2.2.2").get(), 2345));
connectionFactory->connections[0]->onDataRead(createSafeByteArrayRef("FLOOP"));
eventLoop->processEvents();
@@ -194,7 +192,7 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture {
void testConnect_ReceiveErrorConnectResponse() {
HTTPConnectProxiedConnection::ref testling(createTestling());
- connect(testling, HostAddressPort(HostAddress("2.2.2.2"), 2345));
+ connect(testling, HostAddressPort(HostAddress::fromString("2.2.2.2").get(), 2345));
connectionFactory->connections[0]->onDataRead(createSafeByteArrayRef("HTTP/1.0 401 Unauthorized\r\n\r\n"));
eventLoop->processEvents();
@@ -206,7 +204,7 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture {
void testConnect_ReceiveDataAfterConnect() {
HTTPConnectProxiedConnection::ref testling(createTestling());
- connect(testling, HostAddressPort(HostAddress("2.2.2.2"), 2345));
+ connect(testling, HostAddressPort(HostAddress::fromString("2.2.2.2").get(), 2345));
connectionFactory->connections[0]->onDataRead(createSafeByteArrayRef("HTTP/1.0 200 Connection established\r\n\r\n"));
eventLoop->processEvents();
@@ -217,7 +215,7 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture {
void testWrite_AfterConnect() {
HTTPConnectProxiedConnection::ref testling(createTestling());
- connect(testling, HostAddressPort(HostAddress("2.2.2.2"), 2345));
+ connect(testling, HostAddressPort(HostAddress::fromString("2.2.2.2").get(), 2345));
connectionFactory->connections[0]->onDataRead(createSafeByteArrayRef("HTTP/1.0 200 Connection established\r\n\r\n"));
eventLoop->processEvents();
connectionFactory->connections[0]->dataWritten.clear();
@@ -229,7 +227,7 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture {
void testDisconnect_AfterConnectRequest() {
HTTPConnectProxiedConnection::ref testling(createTestling());
- connect(testling, HostAddressPort(HostAddress("2.2.2.2"), 2345));
+ connect(testling, HostAddressPort(HostAddress::fromString("2.2.2.2").get(), 2345));
testling->disconnect();
@@ -240,7 +238,7 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture {
void testDisconnect_AfterConnect() {
HTTPConnectProxiedConnection::ref testling(createTestling());
- connect(testling, HostAddressPort(HostAddress("2.2.2.2"), 2345));
+ connect(testling, HostAddressPort(HostAddress::fromString("2.2.2.2").get(), 2345));
connectionFactory->connections[0]->onDataRead(createSafeByteArrayRef("HTTP/1.0 200 Connection established\r\n\r\n"));
eventLoop->processEvents();
@@ -257,7 +255,7 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture {
std::shared_ptr<ExampleHTTPTrafficFilter> httpTrafficFilter = std::make_shared<ExampleHTTPTrafficFilter>();
testling->setHTTPTrafficFilter(httpTrafficFilter);
- connect(testling, HostAddressPort(HostAddress("2.2.2.2"), 2345));
+ connect(testling, HostAddressPort(HostAddress::fromString("2.2.2.2").get(), 2345));
// set a default response so the server response is answered by the traffic filter
httpTrafficFilter->filterResponseReturn.clear();
@@ -298,7 +296,7 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture {
std::shared_ptr<ProxyAuthenticationHTTPTrafficFilter> httpTrafficFilter = std::make_shared<ProxyAuthenticationHTTPTrafficFilter>();
testling->setHTTPTrafficFilter(httpTrafficFilter);
- connect(testling, HostAddressPort(HostAddress("2.2.2.2"), 2345));
+ connect(testling, HostAddressPort(HostAddress::fromString("2.2.2.2").get(), 2345));
// First HTTP CONNECT request assumes the proxy will work.
CPPUNIT_ASSERT_EQUAL(createByteArray("CONNECT 2.2.2.2:2345 HTTP/1.1\r\n"
diff --git a/Swiften/Network/UnitTest/HostAddressTest.cpp b/Swiften/Network/UnitTest/HostAddressTest.cpp
index aceb9be..226346b 100644
--- a/Swiften/Network/UnitTest/HostAddressTest.cpp
+++ b/Swiften/Network/UnitTest/HostAddressTest.cpp
@@ -25,10 +25,10 @@ class HostAddressTest : public CppUnit::TestFixture {
public:
void testConstructor() {
- HostAddress testling("192.168.1.254");
+ auto testling = HostAddress::fromString("192.168.1.254");
- CPPUNIT_ASSERT_EQUAL(std::string("192.168.1.254"), testling.toString());
- CPPUNIT_ASSERT(testling.isValid());
+ CPPUNIT_ASSERT_EQUAL(std::string("192.168.1.254"), testling->toString());
+ CPPUNIT_ASSERT(testling->isValid());
}
void testConstructor_Invalid() {
@@ -38,9 +38,9 @@ class HostAddressTest : public CppUnit::TestFixture {
}
void testConstructor_InvalidString() {
- HostAddress testling("invalid");
+ auto testling = HostAddress::fromString("invalid");
- CPPUNIT_ASSERT(!testling.isValid());
+ CPPUNIT_ASSERT(!testling);
}
void testToString() {