~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to tests/auto/network/kernel/qhostinfo/tst_qhostinfo.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the test suite of the Qt Toolkit.
 
7
**
 
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.
 
16
**
 
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.
 
24
**
 
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.
 
28
**
 
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.
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
42
 
 
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
 
45
#include <qglobal.h>
 
46
 
 
47
// To prevent windows system header files from re-defining min/max
 
48
#define NOMINMAX 1
 
49
 
 
50
#if defined(Q_OS_WIN)
 
51
# include <winsock2.h>
 
52
# include <ws2tcpip.h>
 
53
#endif
 
54
 
 
55
#include <QtTest/QtTest>
 
56
#include <qcoreapplication.h>
 
57
#include <QDebug>
 
58
#include <QTcpSocket>
 
59
#include <private/qthread_p.h>
 
60
#include <QTcpServer>
 
61
 
 
62
#ifndef QT_NO_BEARERMANAGEMENT
 
63
#include <QtNetwork/qnetworkconfigmanager.h>
 
64
#include <QtNetwork/qnetworkconfiguration.h>
 
65
#include <QtNetwork/qnetworksession.h>
 
66
#endif
 
67
 
 
68
#include <time.h>
 
69
#include <qlibrary.h>
 
70
#if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
 
71
#include <windows.h>
 
72
#else
 
73
#include <unistd.h>
 
74
#include <signal.h>
 
75
#endif
 
76
 
 
77
#include <qhostinfo.h>
 
78
#include "private/qhostinfo_p.h"
 
79
 
 
80
#if !defined(QT_NO_GETADDRINFO)
 
81
# if !defined(Q_OS_WINCE)
 
82
#  include <sys/types.h>
 
83
# else
 
84
#  include <types.h>
 
85
# endif
 
86
# if defined(Q_OS_UNIX)
 
87
#  include <sys/socket.h>
 
88
# endif
 
89
# if !defined(Q_OS_WIN)
 
90
#  include <netdb.h>
 
91
# endif
 
92
#endif
 
93
 
 
94
#include "../../../network-settings.h"
 
95
 
 
96
const char * const lupinellaIp = "10.3.4.6";
 
97
 
 
98
 
 
99
class tst_QHostInfo : public QObject
 
100
{
 
101
    Q_OBJECT
 
102
 
 
103
public:
 
104
    tst_QHostInfo();
 
105
    virtual ~tst_QHostInfo();
 
106
 
 
107
 
 
108
public slots:
 
109
    void init();
 
110
    void cleanup();
 
111
    void initTestCase();
 
112
 
 
113
private slots:
 
114
    void getSetCheck();
 
115
    void staticInformation();
 
116
    void lookupIPv4_data();
 
117
    void lookupIPv4();
 
118
    void lookupIPv6_data();
 
119
    void lookupIPv6();
 
120
    void reverseLookup_data();
 
121
    void reverseLookup();
 
122
 
 
123
    void blockingLookup_data();
 
124
    void blockingLookup();
 
125
 
 
126
    void raceCondition();
 
127
    void threadSafety();
 
128
    void threadSafetyAsynchronousAPI();
 
129
 
 
130
    void multipleSameLookups();
 
131
    void multipleDifferentLookups_data();
 
132
    void multipleDifferentLookups();
 
133
 
 
134
    void cache();
 
135
 
 
136
    void abortHostLookup();
 
137
    void abortHostLookupInDifferentThread();
 
138
protected slots:
 
139
    void resultsReady(const QHostInfo &);
 
140
 
 
141
private:
 
142
    bool ipv6LookupsAvailable;
 
143
    bool ipv6Available;
 
144
    bool lookupDone;
 
145
    int lookupsDoneCounter;
 
146
    QHostInfo lookupResults;
 
147
#ifndef QT_NO_BEARERMANAGEMENT
 
148
    QNetworkConfigurationManager *netConfMan;
 
149
    QNetworkConfiguration networkConfiguration;
 
150
    QScopedPointer<QNetworkSession> networkSession;
 
151
#endif
 
152
};
 
153
 
 
154
// Testing get/set functions
 
155
void tst_QHostInfo::getSetCheck()
 
156
{
 
157
    QHostInfo obj1;
 
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());
 
164
 
 
165
    // int QHostInfo::lookupId()
 
166
    // void QHostInfo::setLookupId(int)
 
167
    obj1.setLookupId(0);
 
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());
 
173
}
 
174
 
 
175
void tst_QHostInfo::staticInformation()
 
176
{
 
177
    qDebug() << "Hostname:" << QHostInfo::localHostName();
 
178
    qDebug() << "Domain name:" << QHostInfo::localDomainName();
 
179
}
 
180
 
 
181
tst_QHostInfo::tst_QHostInfo()
 
182
{
 
183
}
 
184
 
 
185
tst_QHostInfo::~tst_QHostInfo()
 
186
{
 
187
}
 
188
 
 
189
void tst_QHostInfo::initTestCase()
 
190
{
 
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));
 
200
    }
 
201
#endif
 
202
 
 
203
    ipv6Available = false;
 
204
    ipv6LookupsAvailable = false;
 
205
 
 
206
    QTcpServer server;
 
207
    if (server.listen(QHostAddress("::1"))) {
 
208
        // We have IPv6 support
 
209
        ipv6Available = true;
 
210
    }
 
211
 
 
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;
 
220
# endif
 
221
 
 
222
    int res = getaddrinfo("::1", "80", &hint, &result);
 
223
    if (res == 0) {
 
224
        // this test worked
 
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;
 
230
        }
 
231
    }
 
232
#endif
 
233
 
 
234
    // run each testcase with and without test enabled
 
235
    QTest::addColumn<bool>("cache");
 
236
    QTest::newRow("WithCache") << true;
 
237
    QTest::newRow("WithoutCache") << false;
 
238
}
 
239
 
 
240
void tst_QHostInfo::init()
 
241
{
 
242
    // delete the cache so inidividual testcase results are independent from each other
 
243
    qt_qhostinfo_clear_cache();
 
244
 
 
245
    QFETCH_GLOBAL(bool, cache);
 
246
    qt_qhostinfo_enable_cache(cache);
 
247
}
 
248
 
 
249
void tst_QHostInfo::cleanup()
 
250
{
 
251
}
 
252
 
 
253
void tst_QHostInfo::lookupIPv4_data()
 
254
{
 
255
    QTest::addColumn<QString>("hostname");
 
256
    QTest::addColumn<QString>("addresses");
 
257
    QTest::addColumn<int>("err");
 
258
 
 
259
    // Test server lookup
 
260
    QTest::newRow("lookup_01") << QtNetworkSettings::serverName() << QtNetworkSettings::serverIP().toString() << int(QHostInfo::NoError);
 
261
    QTest::newRow("empty") << "" << "" << int(QHostInfo::HostNotFound);
 
262
 
 
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);
 
266
 
 
267
    QTest::newRow("notfound") << "this-name-does-not-exist-hopefully." << "" << int(QHostInfo::HostNotFound);
 
268
 
 
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);
 
271
}
 
272
 
 
273
void tst_QHostInfo::lookupIPv4()
 
274
{
 
275
    QFETCH(QString, hostname);
 
276
    QFETCH(int, err);
 
277
    QFETCH(QString, addresses);
 
278
 
 
279
    lookupDone = false;
 
280
    QHostInfo::lookupHost(hostname, this, SLOT(resultsReady(QHostInfo)));
 
281
 
 
282
    QTestEventLoop::instance().enterLoop(10);
 
283
    QVERIFY(!QTestEventLoop::instance().timeout());
 
284
    QVERIFY(lookupDone);
 
285
 
 
286
    if ((int)lookupResults.error() != (int)err) {
 
287
        qWarning() << hostname << "=>" << lookupResults.errorString();
 
288
    }
 
289
    QCOMPARE((int)lookupResults.error(), (int)err);
 
290
 
 
291
    QStringList tmp;
 
292
    for (int i = 0; i < lookupResults.addresses().count(); ++i)
 
293
        tmp.append(lookupResults.addresses().at(i).toString());
 
294
    tmp.sort();
 
295
 
 
296
    QStringList expected = addresses.split(' ');
 
297
    expected.sort();
 
298
 
 
299
    QCOMPARE(tmp.join(' '), expected.join(' '));
 
300
}
 
301
 
 
302
void tst_QHostInfo::lookupIPv6_data()
 
303
{
 
304
    QTest::addColumn<QString>("hostname");
 
305
    QTest::addColumn<QString>("addresses");
 
306
    QTest::addColumn<int>("err");
 
307
 
 
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);
 
313
 
 
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);
 
318
}
 
319
 
 
320
void tst_QHostInfo::lookupIPv6()
 
321
{
 
322
    QFETCH(QString, hostname);
 
323
    QFETCH(int, err);
 
324
    QFETCH(QString, addresses);
 
325
 
 
326
    if (!ipv6LookupsAvailable)
 
327
        QSKIP("This platform does not support IPv6 lookups");
 
328
 
 
329
    lookupDone = false;
 
330
    QHostInfo::lookupHost(hostname, this, SLOT(resultsReady(QHostInfo)));
 
331
 
 
332
    QTestEventLoop::instance().enterLoop(10);
 
333
    QVERIFY(!QTestEventLoop::instance().timeout());
 
334
    QVERIFY(lookupDone);
 
335
 
 
336
    QCOMPARE((int)lookupResults.error(), (int)err);
 
337
 
 
338
    QStringList tmp;
 
339
    for (int i = 0; i < lookupResults.addresses().count(); ++i)
 
340
        tmp.append(lookupResults.addresses().at(i).toString());
 
341
    tmp.sort();
 
342
 
 
343
    QStringList expected = addresses.split(' ');
 
344
    expected.sort();
 
345
 
 
346
    QCOMPARE(tmp.join(' ').toLower(), expected.join(' ').toLower());
 
347
}
 
348
 
 
349
void tst_QHostInfo::reverseLookup_data()
 
350
{
 
351
    QTest::addColumn<QString>("address");
 
352
    QTest::addColumn<QStringList>("hostNames");
 
353
    QTest::addColumn<int>("err");
 
354
    QTest::addColumn<bool>("ipv6");
 
355
 
 
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;
 
359
}
 
360
 
 
361
void tst_QHostInfo::reverseLookup()
 
362
{
 
363
    QFETCH(QString, address);
 
364
    QFETCH(QStringList, hostNames);
 
365
    QFETCH(int, err);
 
366
    QFETCH(bool, ipv6);
 
367
 
 
368
    if (ipv6 && !ipv6LookupsAvailable) {
 
369
        QSKIP("IPv6 reverse lookups are not supported on this platform");
 
370
    }
 
371
 
 
372
    QHostInfo info = QHostInfo::fromName(address);
 
373
 
 
374
    if (err == 0) {
 
375
        QVERIFY(hostNames.contains(info.hostName()));
 
376
        QCOMPARE(info.addresses().first(), QHostAddress(address));
 
377
    } else {
 
378
        QCOMPARE(info.hostName(), address);
 
379
        QCOMPARE(info.error(), QHostInfo::HostNotFound);
 
380
    }
 
381
 
 
382
}
 
383
 
 
384
void tst_QHostInfo::blockingLookup_data()
 
385
{
 
386
    lookupIPv4_data();
 
387
    if (ipv6LookupsAvailable)
 
388
        lookupIPv6_data();
 
389
}
 
390
 
 
391
void tst_QHostInfo::blockingLookup()
 
392
{
 
393
    QFETCH(QString, hostname);
 
394
    QFETCH(int, err);
 
395
    QFETCH(QString, addresses);
 
396
 
 
397
    QHostInfo hostInfo = QHostInfo::fromName(hostname);
 
398
    QStringList tmp;
 
399
    for (int i = 0; i < hostInfo.addresses().count(); ++i)
 
400
        tmp.append(hostInfo.addresses().at(i).toString());
 
401
    tmp.sort();
 
402
 
 
403
    if ((int)hostInfo.error() != (int)err) {
 
404
        qWarning() << hostname << "=>" << lookupResults.errorString();
 
405
    }
 
406
    QCOMPARE((int)hostInfo.error(), (int)err);
 
407
 
 
408
    QStringList expected = addresses.split(' ');
 
409
    expected.sort();
 
410
 
 
411
    QCOMPARE(tmp.join(' ').toUpper(), expected.join(' ').toUpper());
 
412
}
 
413
 
 
414
void tst_QHostInfo::raceCondition()
 
415
{
 
416
    for (int i = 0; i < 1000; ++i) {
 
417
        QTcpSocket socket;
 
418
        socket.connectToHost("notavalidname.troll.no", 80);
 
419
    }
 
420
}
 
421
 
 
422
class LookupThread : public QThread
 
423
{
 
424
protected:
 
425
    inline void run()
 
426
    {
 
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"));
 
431
    }
 
432
};
 
433
 
 
434
void tst_QHostInfo::threadSafety()
 
435
{
 
436
    const int nattempts = 5;
 
437
#if defined(Q_OS_WINCE)
 
438
    const int runs = 10;
 
439
#else
 
440
    const int runs = 100;
 
441
#endif
 
442
    LookupThread thr[nattempts];
 
443
    for (int j = 0; j < runs; ++j) {
 
444
        for (int i = 0; i < nattempts; ++i)
 
445
            thr[i].start();
 
446
        for (int k = nattempts - 1; k >= 0; --k)
 
447
            thr[k].wait();
 
448
    }
 
449
}
 
450
 
 
451
class LookupReceiver : public QObject
 
452
{
 
453
    Q_OBJECT
 
454
public slots:
 
455
    void start();
 
456
    void resultsReady(const QHostInfo&);
 
457
public:
 
458
    QHostInfo result;
 
459
    int numrequests;
 
460
};
 
461
 
 
462
void LookupReceiver::start()
 
463
{
 
464
    for (int i=0;i<numrequests;i++)
 
465
        QHostInfo::lookupHost(QString("qt-project.org"), this, SLOT(resultsReady(QHostInfo)));
 
466
}
 
467
 
 
468
void LookupReceiver::resultsReady(const QHostInfo &info)
 
469
{
 
470
    result = info;
 
471
    numrequests--;
 
472
    if (numrequests == 0 || info.error() != QHostInfo::NoError)
 
473
        QThread::currentThread()->quit();
 
474
}
 
475
 
 
476
void tst_QHostInfo::threadSafetyAsynchronousAPI()
 
477
{
 
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()));
 
489
        thread->start();
 
490
        threads.append(thread);
 
491
    }
 
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);
 
498
    }
 
499
}
 
500
 
 
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()
 
504
{
 
505
    const int COUNT = 10;
 
506
    lookupsDoneCounter = 0;
 
507
 
 
508
    for (int i = 0; i < COUNT; i++)
 
509
        QHostInfo::lookupHost("localhost", this, SLOT(resultsReady(QHostInfo)));
 
510
 
 
511
    QElapsedTimer timer;
 
512
    timer.start();
 
513
    while (timer.elapsed() < 10000 && lookupsDoneCounter < COUNT) {
 
514
        QTestEventLoop::instance().enterLoop(2);
 
515
    }
 
516
    QCOMPARE(lookupsDoneCounter, COUNT);
 
517
}
 
518
 
 
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()
 
522
{
 
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;
 
528
}
 
529
 
 
530
void tst_QHostInfo::multipleDifferentLookups()
 
531
{
 
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" << "----";
 
536
 
 
537
    QFETCH(int, repeats);
 
538
    const int COUNT = hostnameList.size();
 
539
    lookupsDoneCounter = 0;
 
540
 
 
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)));
 
544
 
 
545
    QElapsedTimer timer;
 
546
    timer.start();
 
547
    while (timer.elapsed() < 60000 && lookupsDoneCounter < repeats*COUNT) {
 
548
        QTestEventLoop::instance().enterLoop(2);
 
549
        //qDebug() << "t:" << timer.elapsed();
 
550
    }
 
551
    QCOMPARE(lookupsDoneCounter, repeats*COUNT);
 
552
}
 
553
 
 
554
void tst_QHostInfo::cache()
 
555
{
 
556
    QFETCH_GLOBAL(bool, cache);
 
557
    if (!cache)
 
558
        return; // test makes only sense when cache enabled
 
559
 
 
560
    // reset slot counter
 
561
    lookupsDoneCounter = 0;
 
562
 
 
563
    // lookup once, wait in event loop, result should not come directly.
 
564
    bool valid = true;
 
565
    int id = -1;
 
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());
 
571
 
 
572
    // loopkup second time, result should come directly
 
573
    valid = false;
 
574
    result = qt_qhostinfo_lookup("localhost", this, SLOT(resultsReady(QHostInfo)), &valid, &id);
 
575
    QVERIFY(valid == true);
 
576
    QVERIFY(!result.addresses().isEmpty());
 
577
 
 
578
    // clear the cache
 
579
    qt_qhostinfo_clear_cache();
 
580
 
 
581
    // lookup third time, result should not come directly.
 
582
    valid = true;
 
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());
 
588
 
 
589
    // the slot should have been called 2 times.
 
590
    QCOMPARE(lookupsDoneCounter, 2);
 
591
}
 
592
 
 
593
void tst_QHostInfo::resultsReady(const QHostInfo &hi)
 
594
{
 
595
    lookupDone = true;
 
596
    lookupResults = hi;
 
597
    lookupsDoneCounter++;
 
598
    QTestEventLoop::instance().exitLoop();
 
599
}
 
600
 
 
601
void tst_QHostInfo::abortHostLookup()
 
602
{
 
603
    //reset counter
 
604
    lookupsDoneCounter = 0;
 
605
    bool valid = false;
 
606
    int id = -1;
 
607
    QHostInfo result = qt_qhostinfo_lookup("qt-project.org", this, SLOT(resultsReady(QHostInfo)), &valid, &id);
 
608
    QVERIFY(!valid);
 
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);
 
613
}
 
614
 
 
615
class LookupAborter : public QObject
 
616
{
 
617
    Q_OBJECT
 
618
public slots:
 
619
    void abort()
 
620
    {
 
621
        QHostInfo::abortHostLookup(id);
 
622
        QThread::currentThread()->quit();
 
623
    }
 
624
public:
 
625
    int id;
 
626
};
 
627
 
 
628
void tst_QHostInfo::abortHostLookupInDifferentThread()
 
629
{
 
630
    //reset counter
 
631
    lookupsDoneCounter = 0;
 
632
    bool valid = false;
 
633
    int id = -1;
 
634
    QHostInfo result = qt_qhostinfo_lookup("qt-project.org", this, SLOT(resultsReady(QHostInfo)), &valid, &id);
 
635
    QVERIFY(!valid);
 
636
    QThread thread;
 
637
    LookupAborter aborter;
 
638
    aborter.id = id;
 
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
 
642
    thread.start();
 
643
    QVERIFY(thread.wait(5000));
 
644
    QTestEventLoop::instance().enterLoop(5);
 
645
    QCOMPARE(lookupsDoneCounter, 0);
 
646
}
 
647
 
 
648
QTEST_MAIN(tst_QHostInfo)
 
649
#include "tst_qhostinfo.moc"