0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the test suite of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
|
|
43 |
#include <QtTest/QtTest>
|
|
44 |
|
|
45 |
#include <qcoreapplication.h>
|
|
46 |
#include <qnetworkinterface.h>
|
|
47 |
#include <qtcpsocket.h>
|
|
48 |
#include "../network-settings.h"
|
|
49 |
|
|
50 |
//TESTED_FILES=qnetworkinterface.cpp qnetworkinterface.h qnetworkinterface_unix.cpp qnetworkinterface_win.cpp
|
|
51 |
|
|
52 |
class tst_QNetworkInterface : public QObject
|
|
53 |
{
|
|
54 |
Q_OBJECT
|
|
55 |
|
|
56 |
public:
|
|
57 |
tst_QNetworkInterface();
|
|
58 |
virtual ~tst_QNetworkInterface();
|
|
59 |
|
|
60 |
private slots:
|
|
61 |
void dump();
|
|
62 |
void loopbackIPv4();
|
|
63 |
void loopbackIPv6();
|
|
64 |
void localAddress();
|
|
65 |
void interfaceFromXXX();
|
|
66 |
void copyInvalidInterface();
|
|
67 |
};
|
|
68 |
|
|
69 |
tst_QNetworkInterface::tst_QNetworkInterface()
|
|
70 |
{
|
|
71 |
Q_SET_DEFAULT_IAP
|
|
72 |
}
|
|
73 |
|
|
74 |
tst_QNetworkInterface::~tst_QNetworkInterface()
|
|
75 |
{
|
|
76 |
}
|
|
77 |
|
|
78 |
|
|
79 |
void tst_QNetworkInterface::dump()
|
|
80 |
{
|
|
81 |
// This is for manual testing:
|
|
82 |
QList<QNetworkInterface> allInterfaces = QNetworkInterface::allInterfaces();
|
|
83 |
foreach (const QNetworkInterface &i, allInterfaces) {
|
|
84 |
QString flags;
|
|
85 |
if (i.flags() & QNetworkInterface::IsUp) flags += "Up,";
|
|
86 |
if (i.flags() & QNetworkInterface::IsRunning) flags += "Running,";
|
|
87 |
if (i.flags() & QNetworkInterface::CanBroadcast) flags += "Broadcast,";
|
|
88 |
if (i.flags() & QNetworkInterface::IsLoopBack) flags += "Loopback,";
|
|
89 |
if (i.flags() & QNetworkInterface::IsPointToPoint) flags += "PointToPoint,";
|
|
90 |
if (i.flags() & QNetworkInterface::CanMulticast) flags += "Multicast,";
|
|
91 |
flags.chop(1); // drop last comma
|
|
92 |
|
|
93 |
QString friendlyName = i.humanReadableName();
|
|
94 |
if (friendlyName != i.name()) {
|
|
95 |
friendlyName.prepend('(');
|
|
96 |
friendlyName.append(')');
|
|
97 |
} else {
|
|
98 |
friendlyName.clear();
|
|
99 |
}
|
|
100 |
qDebug() << "Interface: " << i.name() << qPrintable(friendlyName);
|
|
101 |
QVERIFY(i.isValid());
|
|
102 |
|
|
103 |
qDebug() << " index: " << i.index();
|
|
104 |
qDebug() << " flags: " << qPrintable(flags);
|
|
105 |
qDebug() << " hw address:" << qPrintable(i.hardwareAddress());
|
|
106 |
|
|
107 |
int count = 0;
|
|
108 |
foreach (const QNetworkAddressEntry &e, i.addressEntries()) {
|
|
109 |
QDebug s = qDebug();
|
|
110 |
s.nospace() << " address "
|
|
111 |
<< qSetFieldWidth(2) << count++ << qSetFieldWidth(0);
|
|
112 |
s.nospace() << ": " << qPrintable(e.ip().toString());
|
|
113 |
if (!e.netmask().isNull())
|
|
114 |
s.nospace() << '/' << e.prefixLength()
|
|
115 |
<< " (" << qPrintable(e.netmask().toString()) << ")";
|
|
116 |
if (!e.broadcast().isNull())
|
|
117 |
s.nospace() << " broadcast " << qPrintable(e.broadcast().toString());
|
|
118 |
}
|
|
119 |
}
|
|
120 |
}
|
|
121 |
|
|
122 |
void tst_QNetworkInterface::loopbackIPv4()
|
|
123 |
{
|
|
124 |
QList<QHostAddress> all = QNetworkInterface::allAddresses();
|
|
125 |
QVERIFY(all.contains(QHostAddress(QHostAddress::LocalHost)));
|
|
126 |
}
|
|
127 |
|
|
128 |
void tst_QNetworkInterface::loopbackIPv6()
|
|
129 |
{
|
|
130 |
#ifdef Q_OS_SYMBIAN
|
|
131 |
QSKIP( "Symbian: IPv6 is not yet supported", SkipAll );
|
|
132 |
#else
|
|
133 |
|
|
134 |
QList<QHostAddress> all = QNetworkInterface::allAddresses();
|
|
135 |
|
|
136 |
bool loopbackfound = false;
|
|
137 |
bool anyIPv6 = false;
|
|
138 |
foreach (QHostAddress addr, all)
|
|
139 |
if (addr == QHostAddress::LocalHostIPv6) {
|
|
140 |
loopbackfound = true;
|
|
141 |
anyIPv6 = true;
|
|
142 |
break;
|
|
143 |
} else if (addr.protocol() == QAbstractSocket::IPv6Protocol)
|
|
144 |
anyIPv6 = true;
|
|
145 |
|
|
146 |
QVERIFY(!anyIPv6 || loopbackfound);
|
|
147 |
#endif
|
|
148 |
}
|
|
149 |
|
|
150 |
void tst_QNetworkInterface::localAddress()
|
|
151 |
{
|
|
152 |
QTcpSocket socket;
|
|
153 |
socket.connectToHost(QtNetworkSettings::serverName(), 80);
|
|
154 |
QVERIFY(socket.waitForConnected(5000));
|
|
155 |
|
|
156 |
QHostAddress local = socket.localAddress();
|
|
157 |
|
|
158 |
// make Apache happy on fluke
|
|
159 |
socket.write("GET / HTTP/1.0\r\n\r\n");
|
|
160 |
socket.waitForBytesWritten(1000);
|
|
161 |
socket.close();
|
|
162 |
|
|
163 |
// test that we can find the address that QTcpSocket reported
|
|
164 |
QList<QHostAddress> all = QNetworkInterface::allAddresses();
|
|
165 |
QVERIFY(all.contains(local));
|
|
166 |
}
|
|
167 |
|
|
168 |
void tst_QNetworkInterface::interfaceFromXXX()
|
|
169 |
{
|
|
170 |
QList<QNetworkInterface> allInterfaces = QNetworkInterface::allInterfaces();
|
|
171 |
|
|
172 |
foreach (QNetworkInterface iface, allInterfaces) {
|
|
173 |
QVERIFY(QNetworkInterface::interfaceFromName(iface.name()).isValid());
|
|
174 |
foreach (QNetworkAddressEntry entry, iface.addressEntries()) {
|
|
175 |
QVERIFY(!entry.ip().isNull());
|
|
176 |
|
|
177 |
if (!entry.netmask().isNull()) {
|
|
178 |
QCOMPARE(entry.netmask().protocol(), entry.ip().protocol());
|
|
179 |
|
|
180 |
// if the netmask is known, the broadcast is known
|
|
181 |
// but only for IPv4 (there is no such thing as broadcast in IPv6)
|
|
182 |
if (entry.ip().protocol() == QAbstractSocket::IPv4Protocol) {
|
|
183 |
QVERIFY(!entry.broadcast().isNull());
|
|
184 |
|
|
185 |
// verify that the broadcast address is correct
|
|
186 |
quint32 ip = entry.ip().toIPv4Address();
|
|
187 |
quint32 mask = entry.netmask().toIPv4Address();
|
|
188 |
quint32 bcast = entry.broadcast().toIPv4Address();
|
|
189 |
|
|
190 |
QCOMPARE(bcast, ip | ~mask);
|
|
191 |
}
|
|
192 |
}
|
|
193 |
|
|
194 |
if (!entry.broadcast().isNull())
|
|
195 |
QCOMPARE(entry.broadcast().protocol(), entry.ip().protocol());
|
|
196 |
}
|
|
197 |
}
|
|
198 |
}
|
|
199 |
|
|
200 |
void tst_QNetworkInterface::copyInvalidInterface()
|
|
201 |
{
|
|
202 |
// Force a copy of an interfaces that isn't likely to exist
|
|
203 |
QNetworkInterface i = QNetworkInterface::interfaceFromName("plopp");
|
|
204 |
QVERIFY(!i.isValid());
|
|
205 |
|
|
206 |
QCOMPARE(i.index(), 0);
|
|
207 |
QVERIFY(i.name().isEmpty());
|
|
208 |
QVERIFY(i.humanReadableName().isEmpty());
|
|
209 |
QVERIFY(i.hardwareAddress().isEmpty());
|
|
210 |
QCOMPARE(int(i.flags()), 0);
|
|
211 |
QVERIFY(i.addressEntries().isEmpty());
|
|
212 |
}
|
|
213 |
|
|
214 |
QTEST_MAIN(tst_QNetworkInterface)
|
|
215 |
#include "tst_qnetworkinterface.moc"
|