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/LinkLocal/UnitTest/LinkLocalConnectorTest.cpp
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/LinkLocal/UnitTest/LinkLocalConnectorTest.cpp')
-rw-r--r--Swiften/LinkLocal/UnitTest/LinkLocalConnectorTest.cpp8
1 files changed, 4 insertions, 4 deletions
diff --git a/Swiften/LinkLocal/UnitTest/LinkLocalConnectorTest.cpp b/Swiften/LinkLocal/UnitTest/LinkLocalConnectorTest.cpp
index 3c5e098..85ae537 100644
--- a/Swiften/LinkLocal/UnitTest/LinkLocalConnectorTest.cpp
+++ b/Swiften/LinkLocal/UnitTest/LinkLocalConnectorTest.cpp
@@ -17,118 +17,118 @@
#include <Swiften/LinkLocal/LinkLocalService.h>
#include <Swiften/Network/FakeConnection.h>
using namespace Swift;
class LinkLocalConnectorTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(LinkLocalConnectorTest);
CPPUNIT_TEST(testConnect);
CPPUNIT_TEST(testConnect_UnableToResolve);
CPPUNIT_TEST(testConnect_UnableToConnect);
CPPUNIT_TEST(testCancel_DuringResolve);
CPPUNIT_TEST(testCancel_DuringConnect);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {
eventLoop = new DummyEventLoop();
querier = std::make_shared<FakeDNSSDQuerier>("rabbithole.local", eventLoop);
connection = std::make_shared<FakeConnection>(eventLoop);
connectFinished = false;
}
void tearDown() {
querier->clearAllQueriesEverRun();
delete eventLoop;
}
void testConnect() {
std::shared_ptr<LinkLocalConnector>
testling(createConnector("rabbithole.local", 1234));
- querier->setAddress("rabbithole.local", HostAddress("192.168.1.1"));
+ querier->setAddress("rabbithole.local", HostAddress::fromString("192.168.1.1").get());
testling->connect();
eventLoop->processEvents();
CPPUNIT_ASSERT(connectFinished);
CPPUNIT_ASSERT(!connectError);
CPPUNIT_ASSERT(connection->connectedTo);
CPPUNIT_ASSERT_EQUAL(std::string(connection->connectedTo->getAddress().toString()), std::string("192.168.1.1"));
CPPUNIT_ASSERT_EQUAL(connection->connectedTo->getPort(), 1234);
}
void testConnect_UnableToResolve() {
std::shared_ptr<LinkLocalConnector>
testling(createConnector("rabbithole.local", 1234));
querier->setAddress("rabbithole.local", boost::optional<HostAddress>());
testling->connect();
eventLoop->processEvents();
CPPUNIT_ASSERT(connectFinished);
CPPUNIT_ASSERT(connectError);
CPPUNIT_ASSERT(!connection->connectedTo);
}
void testConnect_UnableToConnect() {
std::shared_ptr<LinkLocalConnector>
testling(createConnector("rabbithole.local", 1234));
- querier->setAddress("rabbithole.local", HostAddress("192.168.1.1"));
+ querier->setAddress("rabbithole.local", HostAddress::fromString("192.168.1.1").get());
connection->setError(Connection::ReadError);
testling->connect();
eventLoop->processEvents();
CPPUNIT_ASSERT(connectFinished);
CPPUNIT_ASSERT(connectError);
CPPUNIT_ASSERT(!connection->connectedTo);
}
void testCancel_DuringResolve() {
std::shared_ptr<LinkLocalConnector>
testling(createConnector("rabbithole.local", 1234));
testling->connect();
eventLoop->processEvents();
CPPUNIT_ASSERT(!connectFinished);
testling->cancel();
eventLoop->processEvents();
- querier->setAddress("rabbithole.local", HostAddress("192.168.1.1"));
+ querier->setAddress("rabbithole.local", HostAddress::fromString("192.168.1.1").get());
eventLoop->processEvents();
CPPUNIT_ASSERT(FakeConnection::Disconnected == connection->state);
}
void testCancel_DuringConnect() {
std::shared_ptr<LinkLocalConnector>
testling(createConnector("rabbithole.local", 1234));
- querier->setAddress("rabbithole.local", HostAddress("192.168.1.1"));
+ querier->setAddress("rabbithole.local", HostAddress::fromString("192.168.1.1").get());
connection->setDelayConnect();
testling->connect();
eventLoop->processEvents();
CPPUNIT_ASSERT(FakeConnection::Connecting == connection->state);
testling->cancel();
eventLoop->processEvents();
CPPUNIT_ASSERT(FakeConnection::Disconnected == connection->state);
}
private:
std::shared_ptr<LinkLocalConnector> createConnector(const std::string& hostname, int port) {
LinkLocalService service(
DNSSDServiceID("myname", "local."),
DNSSDResolveServiceQuery::Result(
"myname._presence._tcp.local", hostname, port,
LinkLocalServiceInfo().toTXTRecord()));
std::shared_ptr<LinkLocalConnector> result(
new LinkLocalConnector(service, querier, connection));
result->onConnectFinished.connect(
boost::bind(&LinkLocalConnectorTest::handleConnected, this, _1));
return result;
}
void handleConnected(bool e) {
connectFinished = true;
connectError = e;
}