1 // Copyright 2005-2019 The Mumble Developers. All rights reserved.
2 // Use of this source code is governed by a BSD-style license
3 // that can be found in the LICENSE file at the root of the
4 // Mumble source tree or at <https://www.mumble.info/LICENSE>.
5 
6 #include <QtCore>
7 #include <QtTest>
8 
9 #include <algorithm>
10 
11 #include "HostAddress.h"
12 #include "ServerAddress.h"
13 
14 class TestServerAddress : public QObject {
15 		Q_OBJECT
16 	private slots:
17 		void defaultCtor();
18 		void isValid();
19 		void ctor();
20 		void equals();
21 		void lessThan();
22 		void qhash();
23 };
24 
defaultCtor()25 void TestServerAddress::defaultCtor() {
26 	ServerAddress sa;
27 	QVERIFY(sa.host == HostAddress());
28 	QVERIFY(sa.port == 0);
29 	QVERIFY(!sa.isValid());
30 }
31 
isValid()32 void TestServerAddress::isValid() {
33 	ServerAddress invalid1;
34 	QVERIFY(!invalid1.isValid());
35 
36 	ServerAddress invalid2(HostAddress(), 0);
37 	QVERIFY(!invalid2.isValid());
38 
39 	ServerAddress invalid3(HostAddress(), 64738);
40 	QVERIFY(!invalid3.isValid());
41 
42 	ServerAddress invalid4(HostAddress(QHostAddress("127.0.0.1")), 0);
43 	QVERIFY(!invalid4.isValid());
44 
45 	ServerAddress valid(HostAddress(QHostAddress("127.0.0.1")), 443);
46 	QVERIFY(valid.isValid());
47 }
48 
ctor()49 void TestServerAddress::ctor() {
50 	ServerAddress sa(HostAddress(QHostAddress("127.0.0.1")), 443);
51 	QCOMPARE(sa.host, HostAddress(QHostAddress("127.0.0.1")));
52 	QCOMPARE(sa.port, static_cast<unsigned short>(443));
53 }
54 
equals()55 void TestServerAddress::equals() {
56 	ServerAddress a1(HostAddress(QHostAddress("127.0.0.1")), 443);
57 	ServerAddress a2(HostAddress(QHostAddress("127.0.0.1")), 443);
58 	ServerAddress b(HostAddress(QHostAddress("127.0.0.1")), 64738);
59 	ServerAddress c(HostAddress(QHostAddress("10.0.0.1")), 80);
60 
61 	QVERIFY(a1 == a2);
62 	QVERIFY(a1 != b);
63 	QVERIFY(a1 != c);
64 	QVERIFY(b != c);
65 }
66 
lessThan()67 void TestServerAddress::lessThan() {
68 	QList<ServerAddress> testdata;
69 
70 	testdata << ServerAddress();
71 	testdata << ServerAddress(HostAddress(), 1);
72 	testdata << ServerAddress(HostAddress(), 999);
73 	testdata << ServerAddress(HostAddress(QHostAddress("0.0.0.1")), 0);
74 	testdata << ServerAddress(HostAddress(QHostAddress("0.0.0.2")), 0);
75 	testdata << ServerAddress(HostAddress(QHostAddress("0.0.0.2")), 1);
76 	testdata << ServerAddress(HostAddress(QHostAddress("80.0.0.1")), 0);
77 	testdata << ServerAddress(HostAddress(QHostAddress("80.0.0.1")), 100);
78 	testdata << ServerAddress(HostAddress(QHostAddress("80.0.0.1")), 64738);
79 	testdata << ServerAddress(HostAddress(QHostAddress("80.0.0.2")), 64738);
80 	testdata << ServerAddress(HostAddress(QHostAddress("255.255.255.255")), 0);
81 	testdata << ServerAddress(HostAddress(QHostAddress("255.255.255.255")), 65535);
82 
83 	QList<ServerAddress> sorted(testdata);
84 	std::sort(sorted.begin(), sorted.end());
85 	QVERIFY(testdata == sorted);
86 }
87 
qhash()88 void TestServerAddress::qhash() {
89 	ServerAddress a1(HostAddress(QHostAddress("127.0.0.1")), 443);
90 	ServerAddress a2(HostAddress(QHostAddress("127.0.0.1")), 443);
91 	ServerAddress b(HostAddress(QHostAddress("127.0.0.1")), 64738);
92 	ServerAddress c(HostAddress(QHostAddress("10.0.0.1")), 80);
93 
94 	QVERIFY(qHash(a1) == qHash(a2));
95 	QVERIFY(qHash(a1) != qHash(b));
96 	QVERIFY(qHash(a1) != qHash(c));
97 	QVERIFY(qHash(b) != qHash(c));
98 }
99 
100 QTEST_MAIN(TestServerAddress)
101 #include "TestServerAddress.moc"
102