1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the test suite of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | |
30 | #include <QtTest/QtTest> |
31 | #include <qcoreapplication.h> |
32 | #include <qnetworkinterface.h> |
33 | |
34 | Q_DECLARE_METATYPE(QHostAddress) |
35 | |
36 | class tst_QNetworkAddressEntry: public QObject |
37 | { |
38 | Q_OBJECT |
39 | private slots: |
40 | void getSetCheck(); |
41 | void prefixAndNetmask_data(); |
42 | void prefixAndNetmask(); |
43 | }; |
44 | |
45 | void tst_QNetworkAddressEntry::getSetCheck() |
46 | { |
47 | QNetworkAddressEntry entry; |
48 | |
49 | QVERIFY(entry.ip().isNull()); |
50 | QVERIFY(entry.netmask().isNull()); |
51 | QVERIFY(entry.broadcast().isNull()); |
52 | QCOMPARE(entry.prefixLength(), -1); |
53 | |
54 | entry.setIp(QHostAddress::LocalHost); |
55 | QCOMPARE(entry.ip(), QHostAddress(QHostAddress::LocalHost)); |
56 | entry.setIp(QHostAddress()); |
57 | QVERIFY(entry.ip().isNull()); |
58 | |
59 | entry.setBroadcast(QHostAddress::LocalHost); |
60 | QCOMPARE(entry.broadcast(), QHostAddress(QHostAddress::LocalHost)); |
61 | entry.setBroadcast(QHostAddress()); |
62 | QVERIFY(entry.broadcast().isNull()); |
63 | |
64 | // netmask and prefix length tested in the next test |
65 | entry.setIp(QHostAddress::LocalHost); |
66 | entry.setBroadcast(QHostAddress::LocalHost); |
67 | |
68 | QNetworkAddressEntry entry2; |
69 | QVERIFY(entry != entry2); |
70 | QVERIFY(!(entry == entry2)); |
71 | |
72 | entry = entry2; |
73 | QCOMPARE(entry, entry2); |
74 | QCOMPARE(entry, entry); |
75 | QVERIFY(!(entry != entry2)); |
76 | } |
77 | |
78 | void tst_QNetworkAddressEntry::prefixAndNetmask_data() |
79 | { |
80 | QTest::addColumn<QHostAddress>(name: "ip" ); |
81 | QTest::addColumn<QHostAddress>(name: "netmask" ); |
82 | QTest::addColumn<int>(name: "prefix" ); |
83 | |
84 | // IPv4 set: |
85 | QHostAddress ipv4(QHostAddress::LocalHost); |
86 | QTest::newRow(dataTag: "v4/0" ) << ipv4 << QHostAddress(QHostAddress::AnyIPv4) << 0; |
87 | QTest::newRow(dataTag: "v4/32" ) << ipv4 << QHostAddress("255.255.255.255" ) << 32; |
88 | QTest::newRow(dataTag: "v4/24" ) << ipv4 << QHostAddress("255.255.255.0" ) << 24; |
89 | QTest::newRow(dataTag: "v4/23" ) << ipv4 << QHostAddress("255.255.254.0" ) << 23; |
90 | QTest::newRow(dataTag: "v4/20" ) << ipv4 << QHostAddress("255.255.240.0" ) << 20; |
91 | QTest::newRow(dataTag: "v4/invalid1" ) << ipv4 << QHostAddress(QHostAddress::LocalHost) << -1; |
92 | QTest::newRow(dataTag: "v4/invalid2" ) << ipv4 << QHostAddress(QHostAddress::AnyIPv6) << -1; |
93 | QTest::newRow(dataTag: "v4/invalid3" ) << ipv4 << QHostAddress("255.255.253.0" ) << -1; |
94 | QTest::newRow(dataTag: "v4/invalid4" ) << ipv4 << QHostAddress() << -2; |
95 | QTest::newRow(dataTag: "v4/invalid5" ) << ipv4 << QHostAddress() << 33; |
96 | |
97 | // IPv6 set: |
98 | QHostAddress ipv6(QHostAddress::LocalHostIPv6); |
99 | QTest::newRow(dataTag: "v6/0" ) << ipv6 << QHostAddress(QHostAddress::AnyIPv6) << 0; |
100 | QTest::newRow(dataTag: "v6/128" ) << ipv6 << QHostAddress("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" ) << 128; |
101 | QTest::newRow(dataTag: "v6/64" ) << ipv6 << QHostAddress("ffff:ffff:ffff:ffff::" ) << 64; |
102 | QTest::newRow(dataTag: "v6/63" ) << ipv6 << QHostAddress("ffff:ffff:ffff:fffe::" ) << 63; |
103 | QTest::newRow(dataTag: "v6/60" ) << ipv6 << QHostAddress("ffff:ffff:ffff:fff0::" ) << 60; |
104 | QTest::newRow(dataTag: "v6/48" ) << ipv6 << QHostAddress("ffff:ffff:ffff::" ) << 48; |
105 | QTest::newRow(dataTag: "v6/3" ) << ipv6 << QHostAddress("e000::" ) << 3; |
106 | QTest::newRow(dataTag: "v6/invalid1" ) << ipv6 << QHostAddress(QHostAddress::LocalHostIPv6) << -1; |
107 | QTest::newRow(dataTag: "v6/invalid2" ) << ipv6 << QHostAddress(QHostAddress::Any) << -1; |
108 | QTest::newRow(dataTag: "v6/invalid3" ) << ipv6 << QHostAddress("fffd::" ) << -1; |
109 | QTest::newRow(dataTag: "v6/invalid4" ) << ipv6 << QHostAddress() << -2; |
110 | QTest::newRow(dataTag: "v6/invalid5" ) << ipv6 << QHostAddress() << 129; |
111 | } |
112 | |
113 | void tst_QNetworkAddressEntry::prefixAndNetmask() |
114 | { |
115 | QFETCH(QHostAddress, ip); |
116 | QFETCH(QHostAddress, netmask); |
117 | QFETCH(int, prefix); |
118 | |
119 | QNetworkAddressEntry entry; |
120 | |
121 | // first, without setting the IP, all must be invalid: |
122 | entry.setNetmask(netmask); |
123 | QVERIFY(entry.netmask().isNull()); |
124 | entry.setPrefixLength(prefix); |
125 | QCOMPARE(entry.prefixLength(), -1); |
126 | |
127 | // set the IP: |
128 | entry.setIp(ip); |
129 | |
130 | // set the netmask: |
131 | if (!netmask.isNull()) { |
132 | entry.setNetmask(netmask); |
133 | |
134 | // was it a valid one? |
135 | if (prefix != -1) { |
136 | QVERIFY(!entry.netmask().isNull()); |
137 | QCOMPARE(entry.netmask(), netmask); |
138 | QCOMPARE(entry.prefixLength(), prefix); |
139 | } else { |
140 | // not valid |
141 | QVERIFY(entry.netmask().isNull()); |
142 | QCOMPARE(entry.prefixLength(), -1); |
143 | } |
144 | } |
145 | entry.setNetmask(QHostAddress()); |
146 | QVERIFY(entry.netmask().isNull()); |
147 | QCOMPARE(entry.prefixLength(), -1); |
148 | |
149 | // set the prefix |
150 | if (prefix != -1) { |
151 | entry.setPrefixLength(prefix); |
152 | |
153 | // was it a valid one? |
154 | if (!netmask.isNull()) { |
155 | QVERIFY(!entry.netmask().isNull()); |
156 | QCOMPARE(entry.netmask(), netmask); |
157 | QCOMPARE(entry.prefixLength(), prefix); |
158 | } else { |
159 | // not valid |
160 | QVERIFY(entry.netmask().isNull()); |
161 | QCOMPARE(entry.prefixLength(), -1); |
162 | } |
163 | } |
164 | entry.setPrefixLength(-1); |
165 | QVERIFY(entry.netmask().isNull()); |
166 | QCOMPARE(entry.prefixLength(), -1); |
167 | } |
168 | |
169 | QTEST_MAIN(tst_QNetworkAddressEntry) |
170 | #include "tst_qnetworkaddressentry.moc" |
171 | |
172 | |