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 @@ | |||
| 1 | /* | 1 | /* |
| 2 | * Copyright (c) 2010-2016 Isode Limited. | 2 | * Copyright (c) 2010-2018 Isode Limited. |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * See the COPYING file for more information. | 4 | * See the COPYING file for more information. |
| 5 | */ | 5 | */ |
| @@ -27,6 +27,10 @@ namespace Swift { | |||
| 27 | return address_ == o.address_; | 27 | return address_ == o.address_; |
| 28 | } | 28 | } |
| 29 | 29 | ||
| 30 | bool operator<(const HostAddress& o) const { | ||
| 31 | return address_ < o.address_; | ||
| 32 | } | ||
| 33 | |||
| 30 | bool isValid() const; | 34 | bool isValid() const; |
| 31 | bool isLocalhost() const; | 35 | bool isLocalhost() const; |
| 32 | 36 | ||
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 { | |||
| 29 | return address_ == o.address_ && port_ == o.port_; | 29 | return address_ == o.address_ && port_ == o.port_; |
| 30 | } | 30 | } |
| 31 | 31 | ||
| 32 | bool operator<(const HostAddressPort& o) const { | ||
| 33 | if (address_ < o.address_) { | ||
| 34 | return true; | ||
| 35 | } | ||
| 36 | return address_ == o.address_ && port_ < o.port_; | ||
| 37 | } | ||
| 38 | |||
| 32 | bool isValid() const { | 39 | bool isValid() const { |
| 33 | return address_.isValid() && port_ > 0; | 40 | return address_.isValid() && port_ > 0; |
| 34 | } | 41 | } |
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 @@ | |||
| 1 | /* | 1 | /* |
| 2 | * Copyright (c) 2010-2016 Isode Limited. | 2 | * Copyright (c) 2010-2018 Isode Limited. |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * See the COPYING file for more information. | 4 | * See the COPYING file for more information. |
| 5 | */ | 5 | */ |
| @@ -10,6 +10,7 @@ | |||
| 10 | #include <cppunit/extensions/TestFactoryRegistry.h> | 10 | #include <cppunit/extensions/TestFactoryRegistry.h> |
| 11 | 11 | ||
| 12 | #include <Swiften/Network/HostAddress.h> | 12 | #include <Swiften/Network/HostAddress.h> |
| 13 | #include <Swiften/Network/HostAddressPort.h> | ||
| 13 | 14 | ||
| 14 | using namespace Swift; | 15 | using namespace Swift; |
| 15 | 16 | ||
| @@ -21,6 +22,7 @@ class HostAddressTest : public CppUnit::TestFixture { | |||
| 21 | CPPUNIT_TEST(testToString); | 22 | CPPUNIT_TEST(testToString); |
| 22 | CPPUNIT_TEST(testToString_IPv6); | 23 | CPPUNIT_TEST(testToString_IPv6); |
| 23 | CPPUNIT_TEST(testToString_Invalid); | 24 | CPPUNIT_TEST(testToString_Invalid); |
| 25 | CPPUNIT_TEST(testComparison); | ||
| 24 | CPPUNIT_TEST_SUITE_END(); | 26 | CPPUNIT_TEST_SUITE_END(); |
| 25 | 27 | ||
| 26 | public: | 28 | public: |
| @@ -62,6 +64,32 @@ class HostAddressTest : public CppUnit::TestFixture { | |||
| 62 | 64 | ||
| 63 | CPPUNIT_ASSERT_EQUAL(std::string("0.0.0.0"), testling.toString()); | 65 | CPPUNIT_ASSERT_EQUAL(std::string("0.0.0.0"), testling.toString()); |
| 64 | } | 66 | } |
| 67 | |||
| 68 | void testComparison() { | ||
| 69 | auto ha127_0_0_1 = *HostAddress::fromString("127.0.0.1"); | ||
| 70 | auto ha127_0_0_2 = *HostAddress::fromString("127.0.0.2"); | ||
| 71 | auto ha127_0_1_0 = *HostAddress::fromString("127.0.1.0"); | ||
| 72 | |||
| 73 | CPPUNIT_ASSERT(ha127_0_0_1 < ha127_0_0_2); | ||
| 74 | CPPUNIT_ASSERT(ha127_0_0_2 < ha127_0_1_0); | ||
| 75 | CPPUNIT_ASSERT(!(ha127_0_0_1 < ha127_0_0_1)); | ||
| 76 | CPPUNIT_ASSERT(!(ha127_0_0_2 < ha127_0_0_1)); | ||
| 77 | CPPUNIT_ASSERT(!(ha127_0_0_2 == ha127_0_0_1)); | ||
| 78 | CPPUNIT_ASSERT(ha127_0_0_1 == ha127_0_0_1); | ||
| 79 | |||
| 80 | auto hap_127_0_0_1__1 = HostAddressPort(ha127_0_0_1, 1); | ||
| 81 | auto hap_127_0_0_1__2 = HostAddressPort(ha127_0_0_1, 2); | ||
| 82 | auto hap_127_0_0_2__1 = HostAddressPort(ha127_0_0_2, 1); | ||
| 83 | auto hap_127_0_0_2__2 = HostAddressPort(ha127_0_0_2, 2); | ||
| 84 | |||
| 85 | CPPUNIT_ASSERT(hap_127_0_0_1__1 < hap_127_0_0_1__2); | ||
| 86 | CPPUNIT_ASSERT(!(hap_127_0_0_1__1 < hap_127_0_0_1__1)); | ||
| 87 | CPPUNIT_ASSERT(!(hap_127_0_0_1__1 == hap_127_0_0_1__2)); | ||
| 88 | CPPUNIT_ASSERT(hap_127_0_0_1__1 == hap_127_0_0_1__1); | ||
| 89 | CPPUNIT_ASSERT(!(hap_127_0_0_1__2 == hap_127_0_0_1__1)); | ||
| 90 | CPPUNIT_ASSERT(hap_127_0_0_1__2 < hap_127_0_0_2__1); | ||
| 91 | CPPUNIT_ASSERT(hap_127_0_0_2__1 < hap_127_0_0_2__2); | ||
| 92 | } | ||
| 65 | }; | 93 | }; |
| 66 | 94 | ||
| 67 | CPPUNIT_TEST_SUITE_REGISTRATION(HostAddressTest); | 95 | CPPUNIT_TEST_SUITE_REGISTRATION(HostAddressTest); |
Swift