diff options
-rw-r--r-- | Swiften/Network/HostAddress.h | 6 | ||||
-rw-r--r-- | Swiften/Network/HostAddressPort.h | 7 | ||||
-rw-r--r-- | Swiften/Network/UnitTest/HostAddressTest.cpp | 30 |
3 files changed, 41 insertions, 2 deletions
diff --git a/Swiften/Network/HostAddress.h b/Swiften/Network/HostAddress.h index e4ddffb..7a22cf4 100644 --- a/Swiften/Network/HostAddress.h +++ b/Swiften/Network/HostAddress.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -27,6 +27,10 @@ namespace Swift { return address_ == o.address_; } + bool operator<(const HostAddress& o) const { + return address_ < o.address_; + } + bool isValid() const; bool isLocalhost() const; diff --git a/Swiften/Network/HostAddressPort.h b/Swiften/Network/HostAddressPort.h index 14c7c66..759af01 100644 --- a/Swiften/Network/HostAddressPort.h +++ b/Swiften/Network/HostAddressPort.h @@ -29,6 +29,13 @@ namespace Swift { return address_ == o.address_ && port_ == o.port_; } + bool operator<(const HostAddressPort& o) const { + if (address_ < o.address_) { + return true; + } + return address_ == o.address_ && port_ < o.port_; + } + bool isValid() const { return address_.isValid() && port_ > 0; } diff --git a/Swiften/Network/UnitTest/HostAddressTest.cpp b/Swiften/Network/UnitTest/HostAddressTest.cpp index 226346b..bd345a7 100644 --- a/Swiften/Network/UnitTest/HostAddressTest.cpp +++ b/Swiften/Network/UnitTest/HostAddressTest.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -10,6 +10,7 @@ #include <cppunit/extensions/TestFactoryRegistry.h> #include <Swiften/Network/HostAddress.h> +#include <Swiften/Network/HostAddressPort.h> using namespace Swift; @@ -21,6 +22,7 @@ class HostAddressTest : public CppUnit::TestFixture { CPPUNIT_TEST(testToString); CPPUNIT_TEST(testToString_IPv6); CPPUNIT_TEST(testToString_Invalid); + CPPUNIT_TEST(testComparison); CPPUNIT_TEST_SUITE_END(); public: @@ -62,6 +64,32 @@ class HostAddressTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(std::string("0.0.0.0"), testling.toString()); } + + void testComparison() { + auto ha127_0_0_1 = *HostAddress::fromString("127.0.0.1"); + auto ha127_0_0_2 = *HostAddress::fromString("127.0.0.2"); + auto ha127_0_1_0 = *HostAddress::fromString("127.0.1.0"); + + CPPUNIT_ASSERT(ha127_0_0_1 < ha127_0_0_2); + CPPUNIT_ASSERT(ha127_0_0_2 < ha127_0_1_0); + CPPUNIT_ASSERT(!(ha127_0_0_1 < ha127_0_0_1)); + CPPUNIT_ASSERT(!(ha127_0_0_2 < ha127_0_0_1)); + CPPUNIT_ASSERT(!(ha127_0_0_2 == ha127_0_0_1)); + CPPUNIT_ASSERT(ha127_0_0_1 == ha127_0_0_1); + + auto hap_127_0_0_1__1 = HostAddressPort(ha127_0_0_1, 1); + auto hap_127_0_0_1__2 = HostAddressPort(ha127_0_0_1, 2); + auto hap_127_0_0_2__1 = HostAddressPort(ha127_0_0_2, 1); + auto hap_127_0_0_2__2 = HostAddressPort(ha127_0_0_2, 2); + + CPPUNIT_ASSERT(hap_127_0_0_1__1 < hap_127_0_0_1__2); + CPPUNIT_ASSERT(!(hap_127_0_0_1__1 < hap_127_0_0_1__1)); + CPPUNIT_ASSERT(!(hap_127_0_0_1__1 == hap_127_0_0_1__2)); + CPPUNIT_ASSERT(hap_127_0_0_1__1 == hap_127_0_0_1__1); + CPPUNIT_ASSERT(!(hap_127_0_0_1__2 == hap_127_0_0_1__1)); + CPPUNIT_ASSERT(hap_127_0_0_1__2 < hap_127_0_0_2__1); + CPPUNIT_ASSERT(hap_127_0_0_2__1 < hap_127_0_0_2__2); + } }; CPPUNIT_TEST_SUITE_REGISTRATION(HostAddressTest); |