1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the test suite of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:LGPL$
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 Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
17
** GNU Lesser General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU Lesser
19
** General Public License version 2.1 as published by the Free Software
20
** Foundation and appearing in the file LICENSE.LGPL included in the
21
** packaging of this file. Please review the following information to
22
** ensure the GNU Lesser General Public License version 2.1 requirements
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
** In addition, as a special exception, Digia gives you certain additional
26
** rights. These rights are described in the Digia Qt LGPL Exception
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
** GNU General Public License Usage
30
** Alternatively, this file may be used under the terms of the GNU
31
** General Public License version 3.0 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file. Please review the following information to
34
** ensure the GNU General Public License version 3.0 requirements will be
35
** met: http://www.gnu.org/copyleft/gpl.html.
40
****************************************************************************/
43
// When using WinSock2 on Windows, it's the first thing that can be included
44
// (except qglobal.h), or else you'll get tons of compile errors
47
// To prevent windows system header files from re-defining min/max
51
# include <winsock2.h>
52
# include <ws2tcpip.h>
55
#include <QtTest/QtTest>
56
#include <qcoreapplication.h>
59
#include <private/qthread_p.h>
62
#ifndef QT_NO_BEARERMANAGEMENT
63
#include <QtNetwork/qnetworkconfigmanager.h>
64
#include <QtNetwork/qnetworkconfiguration.h>
65
#include <QtNetwork/qnetworksession.h>
70
#if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
77
#include <qhostinfo.h>
78
#include "private/qhostinfo_p.h"
80
#if !defined(QT_NO_GETADDRINFO)
81
# if !defined(Q_OS_WINCE)
82
# include <sys/types.h>
86
# if defined(Q_OS_UNIX)
87
# include <sys/socket.h>
89
# if !defined(Q_OS_WIN)
94
#include "../../../network-settings.h"
96
const char * const lupinellaIp = "10.3.4.6";
99
class tst_QHostInfo : public QObject
105
virtual ~tst_QHostInfo();
115
void staticInformation();
116
void lookupIPv4_data();
118
void lookupIPv6_data();
120
void reverseLookup_data();
121
void reverseLookup();
123
void blockingLookup_data();
124
void blockingLookup();
126
void raceCondition();
128
void threadSafetyAsynchronousAPI();
130
void multipleSameLookups();
131
void multipleDifferentLookups_data();
132
void multipleDifferentLookups();
136
void abortHostLookup();
137
void abortHostLookupInDifferentThread();
139
void resultsReady(const QHostInfo &);
142
bool ipv6LookupsAvailable;
145
int lookupsDoneCounter;
146
QHostInfo lookupResults;
147
#ifndef QT_NO_BEARERMANAGEMENT
148
QNetworkConfigurationManager *netConfMan;
149
QNetworkConfiguration networkConfiguration;
150
QScopedPointer<QNetworkSession> networkSession;
154
// Testing get/set functions
155
void tst_QHostInfo::getSetCheck()
158
// HostInfoError QHostInfo::error()
159
// void QHostInfo::setError(HostInfoError)
160
obj1.setError(QHostInfo::HostInfoError(0));
161
QCOMPARE(QHostInfo::HostInfoError(0), obj1.error());
162
obj1.setError(QHostInfo::HostInfoError(1));
163
QCOMPARE(QHostInfo::HostInfoError(1), obj1.error());
165
// int QHostInfo::lookupId()
166
// void QHostInfo::setLookupId(int)
168
QCOMPARE(0, obj1.lookupId());
169
obj1.setLookupId(INT_MIN);
170
QCOMPARE(INT_MIN, obj1.lookupId());
171
obj1.setLookupId(INT_MAX);
172
QCOMPARE(INT_MAX, obj1.lookupId());
175
void tst_QHostInfo::staticInformation()
177
qDebug() << "Hostname:" << QHostInfo::localHostName();
178
qDebug() << "Domain name:" << QHostInfo::localDomainName();
181
tst_QHostInfo::tst_QHostInfo()
185
tst_QHostInfo::~tst_QHostInfo()
189
void tst_QHostInfo::initTestCase()
191
QVERIFY(QtNetworkSettings::verifyTestNetworkSettings());
192
#ifndef QT_NO_BEARERMANAGEMENT
193
//start the default network
194
netConfMan = new QNetworkConfigurationManager(this);
195
networkConfiguration = netConfMan->defaultConfiguration();
196
networkSession.reset(new QNetworkSession(networkConfiguration));
197
if (!networkSession->isOpen()) {
198
networkSession->open();
199
QVERIFY(networkSession->waitForOpened(30000));
203
ipv6Available = false;
204
ipv6LookupsAvailable = false;
207
if (server.listen(QHostAddress("::1"))) {
208
// We have IPv6 support
209
ipv6Available = true;
212
// HP-UX 11i does not support IPv6 reverse lookups.
213
#if !defined(QT_NO_GETADDRINFO) && !(defined(Q_OS_HPUX) && defined(__ia64))
214
// check if the system getaddrinfo can do IPv6 lookups
215
struct addrinfo hint, *result = 0;
216
memset(&hint, 0, sizeof hint);
217
hint.ai_family = AF_UNSPEC;
218
# ifdef AI_ADDRCONFIG
219
hint.ai_flags = AI_ADDRCONFIG;
222
int res = getaddrinfo("::1", "80", &hint, &result);
225
freeaddrinfo(result);
226
res = getaddrinfo("ipv6-test.dev.troll.no", "80", &hint, &result);
227
if (res == 0 && result != 0 && result->ai_family != AF_INET) {
228
freeaddrinfo(result);
229
ipv6LookupsAvailable = true;
234
// run each testcase with and without test enabled
235
QTest::addColumn<bool>("cache");
236
QTest::newRow("WithCache") << true;
237
QTest::newRow("WithoutCache") << false;
240
void tst_QHostInfo::init()
242
// delete the cache so inidividual testcase results are independent from each other
243
qt_qhostinfo_clear_cache();
245
QFETCH_GLOBAL(bool, cache);
246
qt_qhostinfo_enable_cache(cache);
249
void tst_QHostInfo::cleanup()
253
void tst_QHostInfo::lookupIPv4_data()
255
QTest::addColumn<QString>("hostname");
256
QTest::addColumn<QString>("addresses");
257
QTest::addColumn<int>("err");
259
// Test server lookup
260
QTest::newRow("lookup_01") << QtNetworkSettings::serverName() << QtNetworkSettings::serverIP().toString() << int(QHostInfo::NoError);
261
QTest::newRow("empty") << "" << "" << int(QHostInfo::HostNotFound);
263
QTest::newRow("single_ip4") << "lupinella.troll.no" << lupinellaIp << int(QHostInfo::NoError);
264
QTest::newRow("multiple_ip4") << "multi.dev.troll.no" << "1.2.3.4 1.2.3.5 10.3.3.31" << int(QHostInfo::NoError);
265
QTest::newRow("literal_ip4") << lupinellaIp << lupinellaIp << int(QHostInfo::NoError);
267
QTest::newRow("notfound") << "this-name-does-not-exist-hopefully." << "" << int(QHostInfo::HostNotFound);
269
QTest::newRow("idn-ace") << "xn--alqualond-34a.troll.no" << "10.3.3.55" << int(QHostInfo::NoError);
270
QTest::newRow("idn-unicode") << QString::fromLatin1("alqualond\353.troll.no") << "10.3.3.55" << int(QHostInfo::NoError);
273
void tst_QHostInfo::lookupIPv4()
275
QFETCH(QString, hostname);
277
QFETCH(QString, addresses);
280
QHostInfo::lookupHost(hostname, this, SLOT(resultsReady(QHostInfo)));
282
QTestEventLoop::instance().enterLoop(10);
283
QVERIFY(!QTestEventLoop::instance().timeout());
286
if ((int)lookupResults.error() != (int)err) {
287
qWarning() << hostname << "=>" << lookupResults.errorString();
289
QCOMPARE((int)lookupResults.error(), (int)err);
292
for (int i = 0; i < lookupResults.addresses().count(); ++i)
293
tmp.append(lookupResults.addresses().at(i).toString());
296
QStringList expected = addresses.split(' ');
299
QCOMPARE(tmp.join(' '), expected.join(' '));
302
void tst_QHostInfo::lookupIPv6_data()
304
QTest::addColumn<QString>("hostname");
305
QTest::addColumn<QString>("addresses");
306
QTest::addColumn<int>("err");
308
QTest::newRow("ipv6-net") << "www.ipv6-net.org" << "62.93.217.177 2001:618:1401::4" << int(QHostInfo::NoError);
309
QTest::newRow("ipv6-test") << "ipv6-test.dev.troll.no" << "2001:638:a00:2::2" << int(QHostInfo::NoError);
310
QTest::newRow("dns6-test") << "dns6-test-dev.troll.no" << "2001:470:1f01:115::10" << int(QHostInfo::NoError);
311
QTest::newRow("multi-dns6") << "multi-dns6-test-dev.troll.no" << "2001:470:1f01:115::11 2001:470:1f01:115::12" << int(QHostInfo::NoError);
312
QTest::newRow("dns46-test") << "dns46-test-dev.troll.no" << "10.3.4.90 2001:470:1f01:115::13" << int(QHostInfo::NoError);
314
// avoid using real IPv6 addresses here because this will do a DNS query
315
// real addresses are between 2000:: and 3fff:ffff:ffff:ffff:ffff:ffff:ffff
316
QTest::newRow("literal_ip6") << "f001:6b0:1:ea:202:a5ff:fecd:13a6" << "f001:6b0:1:ea:202:a5ff:fecd:13a6" << int(QHostInfo::NoError);
317
QTest::newRow("literal_shortip6") << "f001:618:1401::4" << "f001:618:1401::4" << int(QHostInfo::NoError);
320
void tst_QHostInfo::lookupIPv6()
322
QFETCH(QString, hostname);
324
QFETCH(QString, addresses);
326
if (!ipv6LookupsAvailable)
327
QSKIP("This platform does not support IPv6 lookups");
330
QHostInfo::lookupHost(hostname, this, SLOT(resultsReady(QHostInfo)));
332
QTestEventLoop::instance().enterLoop(10);
333
QVERIFY(!QTestEventLoop::instance().timeout());
336
QCOMPARE((int)lookupResults.error(), (int)err);
339
for (int i = 0; i < lookupResults.addresses().count(); ++i)
340
tmp.append(lookupResults.addresses().at(i).toString());
343
QStringList expected = addresses.split(' ');
346
QCOMPARE(tmp.join(' ').toLower(), expected.join(' ').toLower());
349
void tst_QHostInfo::reverseLookup_data()
351
QTest::addColumn<QString>("address");
352
QTest::addColumn<QStringList>("hostNames");
353
QTest::addColumn<int>("err");
354
QTest::addColumn<bool>("ipv6");
356
QTest::newRow("google-public-dns-a.google.com") << QString("8.8.8.8") << QStringList(QString("google-public-dns-a.google.com")) << 0 << false;
357
QTest::newRow("gitorious.org") << QString("87.238.52.168") << QStringList(QString("gitorious.org")) << 0 << false;
358
QTest::newRow("bogus-name") << QString("1::2::3::4") << QStringList() << 1 << true;
361
void tst_QHostInfo::reverseLookup()
363
QFETCH(QString, address);
364
QFETCH(QStringList, hostNames);
368
if (ipv6 && !ipv6LookupsAvailable) {
369
QSKIP("IPv6 reverse lookups are not supported on this platform");
372
QHostInfo info = QHostInfo::fromName(address);
375
QVERIFY(hostNames.contains(info.hostName()));
376
QCOMPARE(info.addresses().first(), QHostAddress(address));
378
QCOMPARE(info.hostName(), address);
379
QCOMPARE(info.error(), QHostInfo::HostNotFound);
384
void tst_QHostInfo::blockingLookup_data()
387
if (ipv6LookupsAvailable)
391
void tst_QHostInfo::blockingLookup()
393
QFETCH(QString, hostname);
395
QFETCH(QString, addresses);
397
QHostInfo hostInfo = QHostInfo::fromName(hostname);
399
for (int i = 0; i < hostInfo.addresses().count(); ++i)
400
tmp.append(hostInfo.addresses().at(i).toString());
403
if ((int)hostInfo.error() != (int)err) {
404
qWarning() << hostname << "=>" << lookupResults.errorString();
406
QCOMPARE((int)hostInfo.error(), (int)err);
408
QStringList expected = addresses.split(' ');
411
QCOMPARE(tmp.join(' ').toUpper(), expected.join(' ').toUpper());
414
void tst_QHostInfo::raceCondition()
416
for (int i = 0; i < 1000; ++i) {
418
socket.connectToHost("notavalidname.troll.no", 80);
422
class LookupThread : public QThread
427
QHostInfo info = QHostInfo::fromName("qt-project.org");
428
QCOMPARE(info.error(), QHostInfo::NoError);
429
QVERIFY(info.addresses().count() > 0);
430
QCOMPARE(info.addresses().at(0).toString(), QString("87.238.53.172"));
434
void tst_QHostInfo::threadSafety()
436
const int nattempts = 5;
437
#if defined(Q_OS_WINCE)
440
const int runs = 100;
442
LookupThread thr[nattempts];
443
for (int j = 0; j < runs; ++j) {
444
for (int i = 0; i < nattempts; ++i)
446
for (int k = nattempts - 1; k >= 0; --k)
451
class LookupReceiver : public QObject
456
void resultsReady(const QHostInfo&);
462
void LookupReceiver::start()
464
for (int i=0;i<numrequests;i++)
465
QHostInfo::lookupHost(QString("qt-project.org"), this, SLOT(resultsReady(QHostInfo)));
468
void LookupReceiver::resultsReady(const QHostInfo &info)
472
if (numrequests == 0 || info.error() != QHostInfo::NoError)
473
QThread::currentThread()->quit();
476
void tst_QHostInfo::threadSafetyAsynchronousAPI()
478
const int nattempts = 10;
479
const int lookupsperthread = 10;
480
QList<QThread*> threads;
481
QList<LookupReceiver*> receivers;
482
for (int i = 0; i < nattempts; ++i) {
483
QThread* thread = new QThread;
484
LookupReceiver* receiver = new LookupReceiver;
485
receiver->numrequests = lookupsperthread;
486
receivers.append(receiver);
487
receiver->moveToThread(thread);
488
connect(thread, SIGNAL(started()), receiver, SLOT(start()));
490
threads.append(thread);
492
for (int k = threads.count() - 1; k >= 0; --k)
493
QVERIFY(threads.at(k)->wait(60000));
494
foreach (LookupReceiver* receiver, receivers) {
495
QCOMPARE(receiver->result.error(), QHostInfo::NoError);
496
QCOMPARE(receiver->result.addresses().at(0).toString(), QString("87.238.53.172"));
497
QCOMPARE(receiver->numrequests, 0);
501
// this test is for the multi-threaded QHostInfo rewrite. It is about getting results at all,
502
// not about getting correct IPs
503
void tst_QHostInfo::multipleSameLookups()
505
const int COUNT = 10;
506
lookupsDoneCounter = 0;
508
for (int i = 0; i < COUNT; i++)
509
QHostInfo::lookupHost("localhost", this, SLOT(resultsReady(QHostInfo)));
513
while (timer.elapsed() < 10000 && lookupsDoneCounter < COUNT) {
514
QTestEventLoop::instance().enterLoop(2);
516
QCOMPARE(lookupsDoneCounter, COUNT);
519
// this test is for the multi-threaded QHostInfo rewrite. It is about getting results at all,
520
// not about getting correct IPs
521
void tst_QHostInfo::multipleDifferentLookups_data()
523
QTest::addColumn<int>("repeats");
524
QTest::newRow("1") << 1;
525
QTest::newRow("2") << 2;
526
QTest::newRow("5") << 5;
527
QTest::newRow("10") << 10;
530
void tst_QHostInfo::multipleDifferentLookups()
532
QStringList hostnameList;
533
hostnameList << "www.ovi.com" << "www.nokia.com" << "qt-project.org" << "www.trolltech.com" << "troll.no"
534
<< "www.qtcentre.org" << "forum.nokia.com" << "www.nokia.com" << "wiki.forum.nokia.com"
535
<< "www.nokia.com" << "nokia.de" << "127.0.0.1" << "----";
537
QFETCH(int, repeats);
538
const int COUNT = hostnameList.size();
539
lookupsDoneCounter = 0;
541
for (int i = 0; i < hostnameList.size(); i++)
542
for (int j = 0; j < repeats; ++j)
543
QHostInfo::lookupHost(hostnameList.at(i), this, SLOT(resultsReady(QHostInfo)));
547
while (timer.elapsed() < 60000 && lookupsDoneCounter < repeats*COUNT) {
548
QTestEventLoop::instance().enterLoop(2);
549
//qDebug() << "t:" << timer.elapsed();
551
QCOMPARE(lookupsDoneCounter, repeats*COUNT);
554
void tst_QHostInfo::cache()
556
QFETCH_GLOBAL(bool, cache);
558
return; // test makes only sense when cache enabled
560
// reset slot counter
561
lookupsDoneCounter = 0;
563
// lookup once, wait in event loop, result should not come directly.
566
QHostInfo result = qt_qhostinfo_lookup("localhost", this, SLOT(resultsReady(QHostInfo)), &valid, &id);
567
QTestEventLoop::instance().enterLoop(5);
568
QVERIFY(!QTestEventLoop::instance().timeout());
569
QVERIFY(valid == false);
570
QVERIFY(result.addresses().isEmpty());
572
// loopkup second time, result should come directly
574
result = qt_qhostinfo_lookup("localhost", this, SLOT(resultsReady(QHostInfo)), &valid, &id);
575
QVERIFY(valid == true);
576
QVERIFY(!result.addresses().isEmpty());
579
qt_qhostinfo_clear_cache();
581
// lookup third time, result should not come directly.
583
result = qt_qhostinfo_lookup("localhost", this, SLOT(resultsReady(QHostInfo)), &valid, &id);
584
QTestEventLoop::instance().enterLoop(5);
585
QVERIFY(!QTestEventLoop::instance().timeout());
586
QVERIFY(valid == false);
587
QVERIFY(result.addresses().isEmpty());
589
// the slot should have been called 2 times.
590
QCOMPARE(lookupsDoneCounter, 2);
593
void tst_QHostInfo::resultsReady(const QHostInfo &hi)
597
lookupsDoneCounter++;
598
QTestEventLoop::instance().exitLoop();
601
void tst_QHostInfo::abortHostLookup()
604
lookupsDoneCounter = 0;
607
QHostInfo result = qt_qhostinfo_lookup("qt-project.org", this, SLOT(resultsReady(QHostInfo)), &valid, &id);
609
//it is assumed that the DNS request/response in the backend is slower than it takes to call abort
610
QHostInfo::abortHostLookup(id);
611
QTestEventLoop::instance().enterLoop(5);
612
QCOMPARE(lookupsDoneCounter, 0);
615
class LookupAborter : public QObject
621
QHostInfo::abortHostLookup(id);
622
QThread::currentThread()->quit();
628
void tst_QHostInfo::abortHostLookupInDifferentThread()
631
lookupsDoneCounter = 0;
634
QHostInfo result = qt_qhostinfo_lookup("qt-project.org", this, SLOT(resultsReady(QHostInfo)), &valid, &id);
637
LookupAborter aborter;
639
aborter.moveToThread(&thread);
640
connect(&thread, SIGNAL(started()), &aborter, SLOT(abort()));
641
//it is assumed that the DNS request/response in the backend is slower than it takes to schedule the thread and call abort
643
QVERIFY(thread.wait(5000));
644
QTestEventLoop::instance().enterLoop(5);
645
QCOMPARE(lookupsDoneCounter, 0);
648
QTEST_MAIN(tst_QHostInfo)
649
#include "tst_qhostinfo.moc"