diff options
-rw-r--r-- | src/ServerAddress.cpp | 42 | ||||
-rw-r--r-- | src/ServerAddress.h | 48 | ||||
-rw-r--r-- | src/mumble.pri | 4 | ||||
-rw-r--r-- | src/tests/TestServerAddress/TestServerAddress.cpp | 101 | ||||
-rw-r--r-- | src/tests/TestServerAddress/TestServerAddress.pro | 12 | ||||
-rw-r--r-- | src/tests/tests.pro | 3 |
6 files changed, 207 insertions, 3 deletions
diff --git a/src/ServerAddress.cpp b/src/ServerAddress.cpp new file mode 100644 index 000000000..98a6d5365 --- /dev/null +++ b/src/ServerAddress.cpp @@ -0,0 +1,42 @@ +// Copyright 2005-2017 The Mumble Developers. All rights reserved. +// Use of this source code is governed by a BSD-style license +// that can be found in the LICENSE file at the root of the +// Mumble source tree or at <https://www.mumble.info/LICENSE>. + +#include "murmur_pch.h" + +#include "ServerAddress.h" + +ServerAddress::ServerAddress() + : port(0) {} + +ServerAddress::ServerAddress(HostAddress host_, unsigned short port_) + : host(host_) + , port(port_) {} + +bool ServerAddress::isValid() const { + return host.isValid() && port != 0; +} + +bool operator==(const ServerAddress &lhs, const ServerAddress &rhs) { + return lhs.host == rhs.host && lhs.port == rhs.port; +} + +bool operator!=(const ServerAddress &lhs, const ServerAddress &rhs) { + return !operator==(lhs, rhs); +} + +bool operator<(const ServerAddress &lhs, const ServerAddress &rhs) { + if (lhs.host < rhs.host) { + return true; + } else if (lhs.host == rhs.host) { + if (lhs.port < lhs.port) { + return true; + } + } + return false; +} + +uint qHash(const ServerAddress &key) { + return qHash(key.host) ^ uint(key.port); +} diff --git a/src/ServerAddress.h b/src/ServerAddress.h new file mode 100644 index 000000000..a504494c1 --- /dev/null +++ b/src/ServerAddress.h @@ -0,0 +1,48 @@ +// Copyright 2005-2017 The Mumble Developers. All rights reserved. +// Use of this source code is governed by a BSD-style license +// that can be found in the LICENSE file at the root of the +// Mumble source tree or at <https://www.mumble.info/LICENSE>. + +#ifndef MUMBLE_SERVERADDRESS_H_ +#define MUMBLE_SERVERADDRESS_H_ + +#include <QtCore/QString> + +#include "HostAddress.h" + +/// ServerAddress represents a server +/// address consisting of a HostAddress +/// and a port. +struct ServerAddress { + HostAddress host; + unsigned short port; + + /// Construct a default ServerAddress. + /// The default ServerAddress value is considered + /// invalid per the |isValid| method. + ServerAddress(); + + /// Construct a ServerAddress pointing to |host_| and |port_|. + ServerAddress(HostAddress host_, unsigned short port_); + + /// Check whether the ServerAddress is valid. + /// A ServerAddress is valid if it has a valid |host| + /// and if its |port| > 0. + bool isValid() const; +}; + +/// Check whether the ServerAddresses |lhs| and |rhs| are equal. +bool operator==(const ServerAddress &lhs, const ServerAddress &rhs); + +/// Check whether the ServerAddresses |lhs| and |rhs| are not equal. +bool operator!=(const ServerAddress &lhs, const ServerAddress &rhs); + +/// Check whether ServerAddress |lhs| should be sorted before |rhs|. +/// This is implemented such that ServerAddress can be used in QMap. +bool operator<(const ServerAddress &lhs, const ServerAddress &rhs); + +/// Implementation of qHash for ServerAddress, such that ServerAddress +/// can be used as a key in QHash, QMap, etc. +uint qHash(const ServerAddress &key); + +#endif diff --git a/src/mumble.pri b/src/mumble.pri index efb7a2df4..2093c6699 100644 --- a/src/mumble.pri +++ b/src/mumble.pri @@ -14,8 +14,8 @@ CONFIG += qt thread debug_and_release warn_on DEFINES *= MUMBLE_VERSION_STRING=$$VERSION INCLUDEPATH += $$PWD . ../mumble_proto VPATH += $$PWD -HEADERS *= ACL.h Channel.h CryptState.h Connection.h Group.h HTMLFilter.h User.h Net.h OSInfo.h Timer.h SSL.h Version.h SSLCipherInfo.h SSLCipherInfoTable.h licenses.h License.h LogEmitter.h CryptographicHash.h CryptographicRandom.h PasswordGenerator.h ByteSwap.h HostAddress.cpp Ban.h EnvUtils.h UnresolvedServerAddress.h -SOURCES *= ACL.cpp Group.cpp Channel.cpp Connection.cpp HTMLFilter.cpp User.cpp Timer.cpp CryptState.cpp OSInfo.cpp SSL.cpp Version.cpp SSLCipherInfo.cpp License.cpp LogEmitter.cpp CryptographicHash.cpp CryptographicRandom.cpp PasswordGenerator.cpp HostAddress.cpp Ban.cpp EnvUtils.cpp UnresolvedServerAddress.cpp +HEADERS *= ACL.h Channel.h CryptState.h Connection.h Group.h HTMLFilter.h User.h Net.h OSInfo.h Timer.h SSL.h Version.h SSLCipherInfo.h SSLCipherInfoTable.h licenses.h License.h LogEmitter.h CryptographicHash.h CryptographicRandom.h PasswordGenerator.h ByteSwap.h HostAddress.cpp Ban.h EnvUtils.h UnresolvedServerAddress.h ServerAddress.h +SOURCES *= ACL.cpp Group.cpp Channel.cpp Connection.cpp HTMLFilter.cpp User.cpp Timer.cpp CryptState.cpp OSInfo.cpp SSL.cpp Version.cpp SSLCipherInfo.cpp License.cpp LogEmitter.cpp CryptographicHash.cpp CryptographicRandom.cpp PasswordGenerator.cpp HostAddress.cpp Ban.cpp EnvUtils.cpp UnresolvedServerAddress.cpp ServerAddress.cpp LIBS *= -lmumble_proto # Add arc4random_uniform diff --git a/src/tests/TestServerAddress/TestServerAddress.cpp b/src/tests/TestServerAddress/TestServerAddress.cpp new file mode 100644 index 000000000..3c785173d --- /dev/null +++ b/src/tests/TestServerAddress/TestServerAddress.cpp @@ -0,0 +1,101 @@ +// Copyright 2005-2017 The Mumble Developers. All rights reserved. +// Use of this source code is governed by a BSD-style license +// that can be found in the LICENSE file at the root of the +// Mumble source tree or at <https://www.mumble.info/LICENSE>. + +#include <QtCore> +#include <QtTest> + +#include <algorithm> + +#include "HostAddress.h" +#include "ServerAddress.h" + +class TestServerAddress : public QObject { + Q_OBJECT + private slots: + void defaultCtor(); + void isValid(); + void ctor(); + void equals(); + void lessThan(); + void qhash(); +}; + +void TestServerAddress::defaultCtor() { + ServerAddress sa; + QVERIFY(sa.host == HostAddress()); + QVERIFY(sa.port == 0); + QVERIFY(!sa.isValid()); +} + +void TestServerAddress::isValid() { + ServerAddress invalid1; + QVERIFY(!invalid1.isValid()); + + ServerAddress invalid2(HostAddress(), 0); + QVERIFY(!invalid2.isValid()); + + ServerAddress invalid3(HostAddress(), 64738); + QVERIFY(!invalid3.isValid()); + + ServerAddress invalid4(HostAddress(QHostAddress("127.0.0.1")), 0); + QVERIFY(!invalid4.isValid()); + + ServerAddress valid(HostAddress(QHostAddress("127.0.0.1")), 443); + QVERIFY(valid.isValid()); +} + +void TestServerAddress::ctor() { + ServerAddress sa(HostAddress(QHostAddress("127.0.0.1")), 443); + QCOMPARE(sa.host, HostAddress(QHostAddress("127.0.0.1"))); + QCOMPARE(sa.port, static_cast<unsigned short>(443)); +} + +void TestServerAddress::equals() { + ServerAddress a1(HostAddress(QHostAddress("127.0.0.1")), 443); + ServerAddress a2(HostAddress(QHostAddress("127.0.0.1")), 443); + ServerAddress b(HostAddress(QHostAddress("127.0.0.1")), 64738); + ServerAddress c(HostAddress(QHostAddress("10.0.0.1")), 80); + + QVERIFY(a1 == a2); + QVERIFY(a1 != b); + QVERIFY(a1 != c); + QVERIFY(b != c); +} + +void TestServerAddress::lessThan() { + QList<ServerAddress> testdata; + + testdata << ServerAddress(); + testdata << ServerAddress(HostAddress(), 1); + testdata << ServerAddress(HostAddress(), 999); + testdata << ServerAddress(HostAddress(QHostAddress("0.0.0.1")), 0); + testdata << ServerAddress(HostAddress(QHostAddress("0.0.0.2")), 0); + testdata << ServerAddress(HostAddress(QHostAddress("0.0.0.2")), 1); + testdata << ServerAddress(HostAddress(QHostAddress("80.0.0.1")), 0); + testdata << ServerAddress(HostAddress(QHostAddress("80.0.0.1")), 100); + testdata << ServerAddress(HostAddress(QHostAddress("80.0.0.1")), 64738); + testdata << ServerAddress(HostAddress(QHostAddress("80.0.0.2")), 64738); + testdata << ServerAddress(HostAddress(QHostAddress("255.255.255.255")), 0); + testdata << ServerAddress(HostAddress(QHostAddress("255.255.255.255")), 65535); + + QList<ServerAddress> sorted(testdata); + std::sort(sorted.begin(), sorted.end()); + QVERIFY(testdata == sorted); +} + +void TestServerAddress::qhash() { + ServerAddress a1(HostAddress(QHostAddress("127.0.0.1")), 443); + ServerAddress a2(HostAddress(QHostAddress("127.0.0.1")), 443); + ServerAddress b(HostAddress(QHostAddress("127.0.0.1")), 64738); + ServerAddress c(HostAddress(QHostAddress("10.0.0.1")), 80); + + QVERIFY(qHash(a1) == qHash(a2)); + QVERIFY(qHash(a1) != qHash(b)); + QVERIFY(qHash(a1) != qHash(c)); + QVERIFY(qHash(b) != qHash(c)); +} + +QTEST_MAIN(TestServerAddress) +#include "TestServerAddress.moc" diff --git a/src/tests/TestServerAddress/TestServerAddress.pro b/src/tests/TestServerAddress/TestServerAddress.pro new file mode 100644 index 000000000..6beb9b575 --- /dev/null +++ b/src/tests/TestServerAddress/TestServerAddress.pro @@ -0,0 +1,12 @@ +# Copyright 2005-2017 The Mumble Developers. All rights reserved. +# Use of this source code is governed by a BSD-style license +# that can be found in the LICENSE file at the root of the +# Mumble source tree or at <https://www.mumble.info/LICENSE>. + +include(../test.pri) + +QT += network + +TARGET = TestServerAddress +SOURCES = TestServerAddress.cpp ServerAddress.cpp HostAddress.cpp +HEADERS = ServerAddress.h HostAddresss.h diff --git a/src/tests/tests.pro b/src/tests/tests.pro index c40114527..55e699713 100644 --- a/src/tests/tests.pro +++ b/src/tests/tests.pro @@ -13,4 +13,5 @@ SUBDIRS += \ TestPasswordGenerator \ TestTimer \ TestXMLTools \ - TestUnresolvedServerAddress + TestUnresolvedServerAddress \ + TestServerAddress |