0
+ − 1
/****************************************************************************
+ − 2
**
4
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 3
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
0
+ − 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
// Just to get Q_OS_SYMBIAN
+ − 44
#include <qglobal.h>
+ − 45
+ − 46
#if defined(_WIN32) && !defined(Q_OS_SYMBIAN)
+ − 47
#include <winsock2.h>
+ − 48
#else
+ − 49
#include <sys/types.h>
+ − 50
#include <sys/socket.h>
+ − 51
#include <fcntl.h>
+ − 52
#include <unistd.h>
+ − 53
#define SOCKET int
+ − 54
#define INVALID_SOCKET -1
+ − 55
#endif
+ − 56
+ − 57
#include <qplatformdefs.h>
+ − 58
+ − 59
#include <QtTest/QtTest>
+ − 60
+ − 61
#include <QAuthenticator>
+ − 62
#include <QCoreApplication>
+ − 63
#include <QEventLoop>
+ − 64
#include <QFile>
+ − 65
#include <QHostAddress>
+ − 66
#include <QHostInfo>
+ − 67
#include <QMap>
+ − 68
#ifndef Q_OS_VXWORKS
+ − 69
#include <QMessageBox>
+ − 70
#include <QPushButton>
+ − 71
#endif
+ − 72
#include <QPointer>
+ − 73
#include <QProcess>
+ − 74
#include <QStringList>
+ − 75
#include <QTcpServer>
+ − 76
#include <QTcpSocket>
+ − 77
#ifndef QT_NO_OPENSSL
+ − 78
#include <QSslSocket>
+ − 79
#endif
+ − 80
#include <QTextStream>
+ − 81
#include <QThread>
+ − 82
#include <QTime>
+ − 83
#include <QTimer>
+ − 84
#include <QDebug>
+ − 85
// RVCT compiles also unused inline methods
+ − 86
# include <QNetworkProxy>
+ − 87
+ − 88
#ifdef Q_OS_LINUX
+ − 89
#include <stdio.h>
+ − 90
#include <stdlib.h>
+ − 91
#include <sys/stat.h>
+ − 92
#include <unistd.h>
+ − 93
#endif
+ − 94
7
+ − 95
#include "private/qhostinfo_p.h"
+ − 96
0
+ − 97
#include "../network-settings.h"
+ − 98
+ − 99
Q_DECLARE_METATYPE(QAbstractSocket::SocketError)
+ − 100
Q_DECLARE_METATYPE(QAbstractSocket::SocketState)
+ − 101
Q_DECLARE_METATYPE(QNetworkProxy)
+ − 102
Q_DECLARE_METATYPE(QList<QNetworkProxy>)
+ − 103
+ − 104
//TESTED_CLASS=
+ − 105
//TESTED_FILES=
+ − 106
+ − 107
QT_FORWARD_DECLARE_CLASS(QTcpSocket)
+ − 108
+ − 109
class tst_QTcpSocket : public QObject
+ − 110
{
+ − 111
Q_OBJECT
+ − 112
+ − 113
public:
+ − 114
tst_QTcpSocket();
+ − 115
virtual ~tst_QTcpSocket();
+ − 116
+ − 117
static void enterLoop(int secs)
+ − 118
{
+ − 119
++loopLevel;
+ − 120
QTestEventLoop::instance().enterLoop(secs);
+ − 121
--loopLevel;
+ − 122
}
+ − 123
static void exitLoop()
+ − 124
{
+ − 125
// Safe exit - if we aren't in an event loop, don't
+ − 126
// exit one.
+ − 127
if (loopLevel > 0)
+ − 128
QTestEventLoop::instance().exitLoop();
+ − 129
}
+ − 130
static bool timeout()
+ − 131
{
+ − 132
return QTestEventLoop::instance().timeout();
+ − 133
}
+ − 134
+ − 135
public slots:
+ − 136
void initTestCase_data();
+ − 137
void init();
+ − 138
void cleanup();
+ − 139
private slots:
+ − 140
void constructing();
+ − 141
void setInvalidSocketDescriptor();
+ − 142
void setSocketDescriptor();
+ − 143
void socketDescriptor();
+ − 144
void blockingIMAP();
+ − 145
void nonBlockingIMAP();
+ − 146
void hostNotFound();
+ − 147
void timeoutConnect();
+ − 148
void delayedClose();
+ − 149
void partialRead();
+ − 150
void unget();
+ − 151
void readAllAfterClose();
+ − 152
void openCloseOpenClose();
+ − 153
void connectDisconnectConnectDisconnect();
+ − 154
void disconnectWhileConnecting_data();
+ − 155
void disconnectWhileConnecting();
+ − 156
void disconnectWhileConnectingNoEventLoop_data();
+ − 157
void disconnectWhileConnectingNoEventLoop();
+ − 158
void disconnectWhileLookingUp_data();
+ − 159
void disconnectWhileLookingUp();
+ − 160
void downloadBigFile();
+ − 161
void readLine();
+ − 162
void readLineString();
+ − 163
void readChunks();
+ − 164
void waitForBytesWritten();
+ − 165
void waitForReadyRead();
+ − 166
void flush();
+ − 167
void synchronousApi();
+ − 168
void dontCloseOnTimeout();
+ − 169
void recursiveReadyRead();
+ − 170
void atEnd();
+ − 171
void socketInAThread();
+ − 172
void socketsInThreads();
+ − 173
void waitForReadyReadInASlot();
+ − 174
void remoteCloseError();
+ − 175
void openMessageBoxInErrorSlot();
+ − 176
#ifndef Q_OS_WIN
+ − 177
void connectToLocalHostNoService();
+ − 178
#endif
+ − 179
void waitForConnectedInHostLookupSlot();
+ − 180
void waitForConnectedInHostLookupSlot2();
+ − 181
void readyReadSignalsAfterWaitForReadyRead();
+ − 182
#ifdef Q_OS_LINUX
+ − 183
void linuxKernelBugLocalSocket();
+ − 184
#endif
+ − 185
void abortiveClose();
+ − 186
void localAddressEmptyOnBSD();
+ − 187
void zeroAndMinusOneReturns();
+ − 188
void connectionRefused();
+ − 189
void suddenRemoteDisconnect_data();
+ − 190
void suddenRemoteDisconnect();
+ − 191
void connectToMultiIP();
+ − 192
void moveToThread0();
+ − 193
void increaseReadBufferSize();
3
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 194
void taskQtBug5799ConnectionErrorWaitForConnected();
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 195
void taskQtBug5799ConnectionErrorEventLoop();
4
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 196
0
+ − 197
void invalidProxy_data();
+ − 198
void invalidProxy();
+ − 199
void proxyFactory_data();
+ − 200
void proxyFactory();
+ − 201
+ − 202
protected slots:
+ − 203
void nonBlockingIMAP_hostFound();
+ − 204
void nonBlockingIMAP_connected();
+ − 205
void nonBlockingIMAP_closed();
+ − 206
void nonBlockingIMAP_readyRead();
+ − 207
void nonBlockingIMAP_bytesWritten(qint64);
+ − 208
void readRegularFile_readyRead();
+ − 209
void exitLoopSlot();
+ − 210
void downloadBigFileSlot();
+ − 211
void recursiveReadyReadSlot();
+ − 212
void waitForReadyReadInASlotSlot();
+ − 213
void messageBoxSlot();
+ − 214
void hostLookupSlot();
+ − 215
void abortiveClose_abortSlot();
+ − 216
void remoteCloseErrorSlot();
+ − 217
void proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *auth);
+ − 218
+ − 219
private:
+ − 220
QTcpSocket *newSocket() const;
+ − 221
QTcpSocket *nonBlockingIMAP_socket;
+ − 222
QStringList nonBlockingIMAP_data;
+ − 223
qint64 nonBlockingIMAP_totalWritten;
+ − 224
+ − 225
QTcpSocket *tmpSocket;
+ − 226
qint64 bytesAvailable;
+ − 227
qint64 expectedLength;
+ − 228
bool readingBody;
+ − 229
QTime timer;
+ − 230
+ − 231
mutable int proxyAuthCalled;
+ − 232
+ − 233
bool gotClosedSignal;
+ − 234
int numConnections;
+ − 235
static int loopLevel;
+ − 236
};
+ − 237
+ − 238
enum ProxyTests {
+ − 239
NoProxy = 0x00,
+ − 240
Socks5Proxy = 0x01,
+ − 241
HttpProxy = 0x02,
+ − 242
TypeMask = 0x0f,
+ − 243
+ − 244
NoAuth = 0x00,
+ − 245
AuthBasic = 0x10,
+ − 246
AuthNtlm = 0x20,
+ − 247
AuthMask = 0xf0
+ − 248
};
+ − 249
+ − 250
int tst_QTcpSocket::loopLevel = 0;
+ − 251
+ − 252
tst_QTcpSocket::tst_QTcpSocket()
+ − 253
{
+ − 254
Q_SET_DEFAULT_IAP
+ − 255
tmpSocket = 0;
+ − 256
}
+ − 257
+ − 258
tst_QTcpSocket::~tst_QTcpSocket()
+ − 259
{
+ − 260
+ − 261
}
+ − 262
+ − 263
void tst_QTcpSocket::initTestCase_data()
+ − 264
{
+ − 265
QTest::addColumn<bool>("setProxy");
+ − 266
QTest::addColumn<int>("proxyType");
+ − 267
QTest::addColumn<bool>("ssl");
+ − 268
+ − 269
qDebug() << QtNetworkSettings::serverName();
+ − 270
QTest::newRow("WithoutProxy") << false << 0 << false;
+ − 271
QTest::newRow("WithSocks5Proxy") << true << int(Socks5Proxy) << false;
+ − 272
QTest::newRow("WithSocks5ProxyAuth") << true << int(Socks5Proxy | AuthBasic) << false;
+ − 273
+ − 274
QTest::newRow("WithHttpProxy") << true << int(HttpProxy) << false;
+ − 275
QTest::newRow("WithHttpProxyBasicAuth") << true << int(HttpProxy | AuthBasic) << false;
+ − 276
// QTest::newRow("WithHttpProxyNtlmAuth") << true << int(HttpProxy | AuthNtlm) << false;
4
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 277
0
+ − 278
#ifndef QT_NO_OPENSSL
+ − 279
QTest::newRow("WithoutProxy SSL") << false << 0 << true;
+ − 280
QTest::newRow("WithSocks5Proxy SSL") << true << int(Socks5Proxy) << true;
+ − 281
QTest::newRow("WithSocks5AuthProxy SSL") << true << int(Socks5Proxy | AuthBasic) << true;
+ − 282
+ − 283
QTest::newRow("WithHttpProxy SSL") << true << int(HttpProxy) << true;
+ − 284
QTest::newRow("WithHttpProxyBasicAuth SSL") << true << int(HttpProxy | AuthBasic) << true;
+ − 285
// QTest::newRow("WithHttpProxyNtlmAuth SSL") << true << int(HttpProxy | AuthNtlm) << true;
+ − 286
#endif
+ − 287
}
+ − 288
+ − 289
void tst_QTcpSocket::init()
+ − 290
{
+ − 291
QFETCH_GLOBAL(bool, setProxy);
+ − 292
if (setProxy) {
+ − 293
QFETCH_GLOBAL(int, proxyType);
+ − 294
QString fluke = QHostInfo::fromName(QtNetworkSettings::serverName()).addresses().first().toString();
+ − 295
QNetworkProxy proxy;
+ − 296
+ − 297
switch (proxyType) {
+ − 298
case Socks5Proxy:
+ − 299
proxy = QNetworkProxy(QNetworkProxy::Socks5Proxy, fluke, 1080);
+ − 300
break;
+ − 301
+ − 302
case Socks5Proxy | AuthBasic:
+ − 303
proxy = QNetworkProxy(QNetworkProxy::Socks5Proxy, fluke, 1081);
+ − 304
break;
+ − 305
+ − 306
case HttpProxy | NoAuth:
+ − 307
proxy = QNetworkProxy(QNetworkProxy::HttpProxy, fluke, 3128);
+ − 308
break;
+ − 309
+ − 310
case HttpProxy | AuthBasic:
+ − 311
proxy = QNetworkProxy(QNetworkProxy::HttpProxy, fluke, 3129);
+ − 312
break;
+ − 313
+ − 314
case HttpProxy | AuthNtlm:
+ − 315
proxy = QNetworkProxy(QNetworkProxy::HttpProxy, fluke, 3130);
+ − 316
break;
+ − 317
}
+ − 318
QNetworkProxy::setApplicationProxy(proxy);
+ − 319
}
7
+ − 320
+ − 321
qt_qhostinfo_clear_cache();
0
+ − 322
}
+ − 323
+ − 324
QTcpSocket *tst_QTcpSocket::newSocket() const
+ − 325
{
+ − 326
QTcpSocket *socket;
+ − 327
#ifndef QT_NO_OPENSSL
+ − 328
QFETCH_GLOBAL(bool, ssl);
+ − 329
socket = ssl ? new QSslSocket : new QTcpSocket;
+ − 330
#else
+ − 331
socket = new QTcpSocket;
+ − 332
#endif
+ − 333
+ − 334
proxyAuthCalled = 0;
+ − 335
connect(socket, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
+ − 336
SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
+ − 337
Qt::DirectConnection);
+ − 338
return socket;
+ − 339
}
+ − 340
+ − 341
void tst_QTcpSocket::cleanup()
+ − 342
{
+ − 343
QNetworkProxy::setApplicationProxy(QNetworkProxy::DefaultProxy);
+ − 344
}
+ − 345
+ − 346
void tst_QTcpSocket::proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *auth)
+ − 347
{
+ − 348
++proxyAuthCalled;
+ − 349
auth->setUser("qsockstest");
+ − 350
auth->setPassword("password");
+ − 351
}
+ − 352
+ − 353
//----------------------------------------------------------------------------------
+ − 354
+ − 355
void tst_QTcpSocket::constructing()
+ − 356
{
+ − 357
QTcpSocket *socket = newSocket();
+ − 358
+ − 359
// Check the initial state of the QTcpSocket.
+ − 360
QCOMPARE(socket->state(), QTcpSocket::UnconnectedState);
+ − 361
QVERIFY(socket->isSequential());
+ − 362
QVERIFY(!socket->isOpen());
+ − 363
QVERIFY(!socket->isValid());
+ − 364
QCOMPARE(socket->socketType(), QTcpSocket::TcpSocket);
+ − 365
+ − 366
char c;
+ − 367
QCOMPARE(socket->getChar(&c), false);
+ − 368
QCOMPARE((int) socket->bytesAvailable(), 0);
+ − 369
QCOMPARE(socket->canReadLine(), false);
+ − 370
QCOMPARE(socket->readLine(), QByteArray());
+ − 371
QCOMPARE(socket->socketDescriptor(), -1);
+ − 372
QCOMPARE((int) socket->localPort(), 0);
+ − 373
QVERIFY(socket->localAddress() == QHostAddress());
+ − 374
QCOMPARE((int) socket->peerPort(), 0);
+ − 375
QVERIFY(socket->peerAddress() == QHostAddress());
+ − 376
QCOMPARE(socket->error(), QTcpSocket::UnknownSocketError);
+ − 377
QCOMPARE(socket->errorString(), QString("Unknown error"));
+ − 378
+ − 379
// Check the state of the socket layer?
+ − 380
delete socket;
+ − 381
}
+ − 382
+ − 383
//----------------------------------------------------------------------------------
+ − 384
+ − 385
void tst_QTcpSocket::setInvalidSocketDescriptor()
+ − 386
{
+ − 387
QTcpSocket *socket = newSocket();
+ − 388
QCOMPARE(socket->socketDescriptor(), -1);
+ − 389
QVERIFY(!socket->setSocketDescriptor(-5, QTcpSocket::UnconnectedState));
+ − 390
QCOMPARE(socket->socketDescriptor(), -1);
+ − 391
+ − 392
QCOMPARE(socket->error(), QTcpSocket::UnsupportedSocketOperationError);
+ − 393
+ − 394
delete socket;
+ − 395
}
+ − 396
+ − 397
//----------------------------------------------------------------------------------
+ − 398
+ − 399
void tst_QTcpSocket::setSocketDescriptor()
+ − 400
{
+ − 401
QFETCH_GLOBAL(bool, setProxy);
+ − 402
if (setProxy)
+ − 403
return; // this test doesn't make sense with proxies
+ − 404
+ − 405
#ifdef Q_OS_WIN
+ − 406
// need the dummy to ensure winsock is started
+ − 407
QTcpSocket *dummy = newSocket();
+ − 408
dummy->connectToHost(QtNetworkSettings::serverName(), 143);
+ − 409
QVERIFY(dummy->waitForConnected());
+ − 410
+ − 411
SOCKET sock = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+ − 412
if (sock == INVALID_SOCKET) {
+ − 413
qErrnoWarning(WSAGetLastError(), "INVALID_SOCKET");
+ − 414
}
+ − 415
#else
+ − 416
SOCKET sock = ::socket(AF_INET, SOCK_STREAM, 0);
+ − 417
+ − 418
// artificially increase the value of sock
+ − 419
SOCKET sock2 = ::fcntl(sock, F_DUPFD, sock + 50);
+ − 420
::close(sock);
+ − 421
sock = sock2;
+ − 422
#endif
+ − 423
+ − 424
QVERIFY(sock != INVALID_SOCKET);
+ − 425
QTcpSocket *socket = newSocket();
+ − 426
QVERIFY(socket->setSocketDescriptor(sock, QTcpSocket::UnconnectedState));
+ − 427
QCOMPARE(socket->socketDescriptor(), (int)sock);
+ − 428
+ − 429
socket->connectToHost(QtNetworkSettings::serverName(), 143);
+ − 430
QCOMPARE(socket->state(), QTcpSocket::HostLookupState);
+ − 431
QCOMPARE(socket->socketDescriptor(), (int)sock);
+ − 432
QVERIFY(socket->waitForConnected(10000));
+ − 433
// skip this, it has been broken for years, see task 260735
+ − 434
// if somebody complains, consider fixing it, but it might break existing applications.
+ − 435
QEXPECT_FAIL("", "bug has been around for years, will not fix without need", Continue);
+ − 436
QCOMPARE(socket->socketDescriptor(), (int)sock);
+ − 437
delete socket;
+ − 438
#ifdef Q_OS_WIN
+ − 439
delete dummy;
+ − 440
#endif
+ − 441
}
+ − 442
+ − 443
//----------------------------------------------------------------------------------
+ − 444
+ − 445
void tst_QTcpSocket::socketDescriptor()
+ − 446
{
+ − 447
QTcpSocket *socket = newSocket();
+ − 448
+ − 449
QCOMPARE(socket->socketDescriptor(), -1);
+ − 450
socket->connectToHost(QtNetworkSettings::serverName(), 143);
+ − 451
QVERIFY((socket->state() == QAbstractSocket::HostLookupState && socket->socketDescriptor() == -1) ||
+ − 452
(socket->state() == QAbstractSocket::ConnectingState && socket->socketDescriptor() != -1));
+ − 453
QVERIFY(socket->waitForConnected(10000));
+ − 454
QVERIFY(socket->state() == QAbstractSocket::ConnectedState);
+ − 455
QVERIFY(socket->socketDescriptor() != -1);
+ − 456
+ − 457
delete socket;
+ − 458
}
+ − 459
+ − 460
//----------------------------------------------------------------------------------
+ − 461
+ − 462
void tst_QTcpSocket::blockingIMAP()
+ − 463
{
+ − 464
QTcpSocket *socket = newSocket();
+ − 465
+ − 466
// Connect
+ − 467
socket->connectToHost(QtNetworkSettings::serverName(), 143);
+ − 468
QVERIFY(socket->waitForConnected(10000));
+ − 469
QCOMPARE(socket->state(), QTcpSocket::ConnectedState);
+ − 470
QVERIFY(socket->isValid());
+ − 471
+ − 472
// Read greeting
+ − 473
QVERIFY(socket->waitForReadyRead(5000));
+ − 474
QString s = socket->readLine();
+ − 475
// only test if an OK was returned, to make the test compatible between different
+ − 476
// IMAP server versions
+ − 477
QCOMPARE(s.left(4).toLatin1().constData(), "* OK");
+ − 478
+ − 479
// Write NOOP
+ − 480
QCOMPARE((int) socket->write("1 NOOP\r\n", 8), 8);
+ − 481
QCOMPARE((int) socket->write("2 NOOP\r\n", 8), 8);
+ − 482
+ − 483
if (!socket->canReadLine())
+ − 484
QVERIFY(socket->waitForReadyRead(5000));
+ − 485
+ − 486
// Read response
+ − 487
s = socket->readLine();
+ − 488
QCOMPARE(s.toLatin1().constData(), "1 OK Completed\r\n");
+ − 489
+ − 490
// Write a third NOOP to verify that write doesn't clear the read buffer
+ − 491
QCOMPARE((int) socket->write("3 NOOP\r\n", 8), 8);
+ − 492
+ − 493
// Read second response
+ − 494
if (!socket->canReadLine())
+ − 495
QVERIFY(socket->waitForReadyRead(5000));
+ − 496
s = socket->readLine();
+ − 497
QCOMPARE(s.toLatin1().constData(), "2 OK Completed\r\n");
+ − 498
+ − 499
// Read third response
+ − 500
if (!socket->canReadLine())
+ − 501
QVERIFY(socket->waitForReadyRead(5000));
+ − 502
s = socket->readLine();
+ − 503
QCOMPARE(s.toLatin1().constData(), "3 OK Completed\r\n");
+ − 504
+ − 505
+ − 506
// Write LOGOUT
+ − 507
QCOMPARE((int) socket->write("4 LOGOUT\r\n", 10), 10);
+ − 508
+ − 509
if (!socket->canReadLine())
+ − 510
QVERIFY(socket->waitForReadyRead(5000));
+ − 511
+ − 512
// Read two lines of respose
+ − 513
s = socket->readLine();
+ − 514
QCOMPARE(s.toLatin1().constData(), "* BYE LOGOUT received\r\n");
+ − 515
+ − 516
if (!socket->canReadLine())
+ − 517
QVERIFY(socket->waitForReadyRead(5000));
+ − 518
+ − 519
s = socket->readLine();
+ − 520
QCOMPARE(s.toLatin1().constData(), "4 OK Completed\r\n");
+ − 521
+ − 522
// Close the socket
+ − 523
socket->close();
+ − 524
+ − 525
// Check that it's closed
+ − 526
QCOMPARE(socket->state(), QTcpSocket::UnconnectedState);
+ − 527
+ − 528
delete socket;
+ − 529
}
+ − 530
+ − 531
//----------------------------------------------------------------------------------
+ − 532
+ − 533
void tst_QTcpSocket::hostNotFound()
+ − 534
{
+ − 535
QTcpSocket *socket = newSocket();
+ − 536
+ − 537
socket->connectToHost("nosuchserver.troll.no", 80);
+ − 538
QVERIFY(!socket->waitForConnected());
+ − 539
QCOMPARE(socket->state(), QTcpSocket::UnconnectedState);
+ − 540
QCOMPARE(int(socket->error()), int(QTcpSocket::HostNotFoundError));
+ − 541
+ − 542
delete socket;
+ − 543
}
+ − 544
+ − 545
//----------------------------------------------------------------------------------
+ − 546
+ − 547
void tst_QTcpSocket::timeoutConnect()
+ − 548
{
+ − 549
QTcpSocket *socket = newSocket();
+ − 550
+ − 551
// Outgoing port 53 is firewalled in the Oslo office.
+ − 552
socket->connectToHost("cisco.com", 53);
+ − 553
QVERIFY(!socket->waitForConnected(200));
+ − 554
QCOMPARE(socket->state(), QTcpSocket::UnconnectedState);
+ − 555
QCOMPARE(int(socket->error()), int(QTcpSocket::SocketTimeoutError));
+ − 556
+ − 557
socket->connectToHost("cisco.com", 53);
+ − 558
QTest::qSleep(50);
+ − 559
socket->abort();
+ − 560
QCOMPARE(socket->state(), QTcpSocket::UnconnectedState);
+ − 561
QCOMPARE(socket->openMode(), QIODevice::NotOpen);
+ − 562
+ − 563
delete socket;
+ − 564
}
+ − 565
+ − 566
//----------------------------------------------------------------------------------
+ − 567
+ − 568
void tst_QTcpSocket::nonBlockingIMAP()
+ − 569
{
+ − 570
QTcpSocket *socket = newSocket();
+ − 571
connect(socket, SIGNAL(hostFound()), SLOT(nonBlockingIMAP_hostFound()));
+ − 572
connect(socket, SIGNAL(connected()), SLOT(nonBlockingIMAP_connected()));
+ − 573
connect(socket, SIGNAL(disconnected()), SLOT(nonBlockingIMAP_closed()));
+ − 574
connect(socket, SIGNAL(bytesWritten(qint64)), SLOT(nonBlockingIMAP_bytesWritten(qint64)));
+ − 575
connect(socket, SIGNAL(readyRead()), SLOT(nonBlockingIMAP_readyRead()));
+ − 576
nonBlockingIMAP_socket = socket;
+ − 577
+ − 578
// Connect
+ − 579
socket->connectToHost(QtNetworkSettings::serverName(), 143);
+ − 580
QVERIFY(socket->state() == QTcpSocket::HostLookupState ||
+ − 581
socket->state() == QTcpSocket::ConnectingState);
+ − 582
+ − 583
enterLoop(30);
+ − 584
if (timeout()) {
+ − 585
QFAIL("Timed out");
+ − 586
}
+ − 587
+ − 588
if (socket->state() == QTcpSocket::ConnectingState) {
+ − 589
enterLoop(30);
+ − 590
if (timeout()) {
+ − 591
QFAIL("Timed out");
+ − 592
}
+ − 593
}
+ − 594
+ − 595
QCOMPARE(socket->state(), QTcpSocket::ConnectedState);
+ − 596
+ − 597
enterLoop(30);
+ − 598
if (timeout()) {
+ − 599
QFAIL("Timed out");
+ − 600
}
+ − 601
+ − 602
// Read greeting
+ − 603
QVERIFY(!nonBlockingIMAP_data.isEmpty());
+ − 604
// QCOMPARE(nonBlockingIMAP_data.at(0).toLatin1().constData(),
+ − 605
// "* OK fluke Cyrus IMAP4 v2.2.12 server ready\r\n");
+ − 606
QCOMPARE(nonBlockingIMAP_data.at(0).left(4).toLatin1().constData(), "* OK");
+ − 607
nonBlockingIMAP_data.clear();
+ − 608
+ − 609
nonBlockingIMAP_totalWritten = 0;
+ − 610
+ − 611
// Write NOOP
+ − 612
QCOMPARE((int) socket->write("1 NOOP\r\n", 8), 8);
+ − 613
+ − 614
+ − 615
enterLoop(30);
+ − 616
if (timeout()) {
+ − 617
QFAIL("Timed out");
+ − 618
}
+ − 619
+ − 620
QVERIFY(nonBlockingIMAP_totalWritten == 8);
+ − 621
+ − 622
+ − 623
enterLoop(30);
+ − 624
if (timeout()) {
+ − 625
QFAIL("Timed out");
+ − 626
}
+ − 627
+ − 628
+ − 629
// Read response
+ − 630
QVERIFY(!nonBlockingIMAP_data.isEmpty());
+ − 631
QCOMPARE(nonBlockingIMAP_data.at(0).toLatin1().constData(), "1 OK Completed\r\n");
+ − 632
nonBlockingIMAP_data.clear();
+ − 633
+ − 634
+ − 635
nonBlockingIMAP_totalWritten = 0;
+ − 636
+ − 637
// Write LOGOUT
+ − 638
QCOMPARE((int) socket->write("2 LOGOUT\r\n", 10), 10);
+ − 639
+ − 640
enterLoop(30);
+ − 641
if (timeout()) {
+ − 642
QFAIL("Timed out");
+ − 643
}
+ − 644
+ − 645
QVERIFY(nonBlockingIMAP_totalWritten == 10);
+ − 646
+ − 647
// Wait for greeting
+ − 648
enterLoop(30);
+ − 649
if (timeout()) {
+ − 650
QFAIL("Timed out");
+ − 651
}
+ − 652
+ − 653
// Read two lines of respose
+ − 654
QCOMPARE(nonBlockingIMAP_data.at(0).toLatin1().constData(), "* BYE LOGOUT received\r\n");
+ − 655
QCOMPARE(nonBlockingIMAP_data.at(1).toLatin1().constData(), "2 OK Completed\r\n");
+ − 656
nonBlockingIMAP_data.clear();
+ − 657
+ − 658
// Close the socket
+ − 659
socket->close();
+ − 660
+ − 661
// Check that it's closed
+ − 662
QCOMPARE(socket->state(), QTcpSocket::UnconnectedState);
+ − 663
+ − 664
delete socket;
+ − 665
}
+ − 666
+ − 667
void tst_QTcpSocket::nonBlockingIMAP_hostFound()
+ − 668
{
+ − 669
exitLoop();
+ − 670
}
+ − 671
+ − 672
void tst_QTcpSocket::nonBlockingIMAP_connected()
+ − 673
{
+ − 674
exitLoop();
+ − 675
}
+ − 676
+ − 677
void tst_QTcpSocket::nonBlockingIMAP_readyRead()
+ − 678
{
+ − 679
while (nonBlockingIMAP_socket->canReadLine())
+ − 680
nonBlockingIMAP_data.append(nonBlockingIMAP_socket->readLine());
+ − 681
+ − 682
exitLoop();
+ − 683
}
+ − 684
+ − 685
void tst_QTcpSocket::nonBlockingIMAP_bytesWritten(qint64 written)
+ − 686
{
+ − 687
nonBlockingIMAP_totalWritten += written;
+ − 688
exitLoop();
+ − 689
}
+ − 690
+ − 691
void tst_QTcpSocket::nonBlockingIMAP_closed()
+ − 692
{
+ − 693
}
+ − 694
+ − 695
//----------------------------------------------------------------------------------
+ − 696
+ − 697
void tst_QTcpSocket::delayedClose()
+ − 698
{
+ − 699
QTcpSocket *socket = newSocket();
+ − 700
connect(socket, SIGNAL(connected()), SLOT(nonBlockingIMAP_connected()));
+ − 701
connect(socket, SIGNAL(disconnected()), SLOT(exitLoopSlot()));
+ − 702
+ − 703
socket->connectToHost(QtNetworkSettings::serverName(), 143);
+ − 704
+ − 705
enterLoop(30);
+ − 706
if (timeout())
+ − 707
QFAIL("Timed out");
+ − 708
+ − 709
QCOMPARE(socket->state(), QTcpSocket::ConnectedState);
+ − 710
+ − 711
QCOMPARE((int) socket->write("1 LOGOUT\r\n", 10), 10);
+ − 712
+ − 713
// Add a huge bulk of data to be written after the logout
+ − 714
// command. The server will shut down after receiving the LOGOUT,
+ − 715
// so this data will not be read. But our close call should
+ − 716
// schedule a delayed close because all the data can not be
+ − 717
// written in one go.
+ − 718
QCOMPARE((int) socket->write(QByteArray(100000, '\n'), 100000), 100000);
+ − 719
+ − 720
socket->close();
+ − 721
+ − 722
QCOMPARE((int) socket->state(), (int) QTcpSocket::ClosingState);
+ − 723
+ − 724
enterLoop(10);
+ − 725
if (timeout())
+ − 726
QFAIL("Timed out");
+ − 727
+ − 728
QCOMPARE(socket->state(), QTcpSocket::UnconnectedState);
+ − 729
+ − 730
delete socket;
+ − 731
}
+ − 732
+ − 733
//----------------------------------------------------------------------------------
+ − 734
+ − 735
void tst_QTcpSocket::partialRead()
+ − 736
{
+ − 737
QTcpSocket *socket = newSocket();
+ − 738
socket->connectToHost(QtNetworkSettings::serverName(), 143);
+ − 739
QVERIFY(socket->waitForConnected(10000));
+ − 740
QVERIFY(socket->state() == QTcpSocket::ConnectedState);
+ − 741
char buf[512];
+ − 742
+ − 743
// QByteArray greeting = "* OK fluke Cyrus IMAP4 v2.2.12 server ready";
+ − 744
QByteArray greeting = "* OK [CAPABILITY IMAP4rev1 UIDPLUS CHILDREN NAMESPACE";
+ − 745
+ − 746
for (int i = 0; i < 10; i += 2) {
+ − 747
while (socket->bytesAvailable() < 2)
+ − 748
QVERIFY(socket->waitForReadyRead(5000));
+ − 749
QVERIFY(socket->read(buf, 2) == 2);
+ − 750
buf[2] = '\0';
+ − 751
QCOMPARE((char *)buf, greeting.mid(i, 2).data());
+ − 752
}
+ − 753
+ − 754
delete socket;
+ − 755
}
+ − 756
+ − 757
//----------------------------------------------------------------------------------
+ − 758
+ − 759
void tst_QTcpSocket::unget()
+ − 760
{
+ − 761
QTcpSocket *socket = newSocket();
+ − 762
socket->connectToHost(QtNetworkSettings::serverName(), 143);
+ − 763
QVERIFY(socket->waitForConnected(10000));
+ − 764
QVERIFY(socket->state() == QTcpSocket::ConnectedState);
+ − 765
char buf[512];
+ − 766
+ − 767
// QByteArray greeting = "* OK fluke Cyrus IMAP4 v2.2.12 server ready";
+ − 768
QByteArray greeting = "* OK [CAPABILITY IMAP4rev1 UIDPLUS CHILDREN NAMESPACE";
+ − 769
+ − 770
for (int i = 0; i < 10; i += 2) {
+ − 771
while (socket->bytesAvailable() < 2)
3
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 772
QVERIFY(socket->waitForReadyRead(10000));
0
+ − 773
int bA = socket->bytesAvailable();
+ − 774
QVERIFY(socket->read(buf, 2) == 2);
+ − 775
buf[2] = '\0';
+ − 776
QCOMPARE((char *)buf, greeting.mid(i, 2).data());
+ − 777
QCOMPARE((int)socket->bytesAvailable(), bA - 2);
+ − 778
socket->ungetChar(buf[1]);
+ − 779
socket->ungetChar(buf[0]);
+ − 780
QCOMPARE((int)socket->bytesAvailable(), bA);
+ − 781
QVERIFY(socket->read(buf, 2) == 2);
+ − 782
buf[2] = '\0';
+ − 783
QCOMPARE((char *)buf, greeting.mid(i, 2).data());
+ − 784
}
+ − 785
+ − 786
delete socket;
+ − 787
}
+ − 788
+ − 789
//----------------------------------------------------------------------------------
+ − 790
void tst_QTcpSocket::readRegularFile_readyRead()
+ − 791
{
+ − 792
exitLoop();
+ − 793
}
+ − 794
+ − 795
//----------------------------------------------------------------------------------
+ − 796
void tst_QTcpSocket::readAllAfterClose()
+ − 797
{
+ − 798
QTcpSocket *socket = newSocket();
+ − 799
socket->connectToHost(QtNetworkSettings::serverName(), 143);
+ − 800
connect(socket, SIGNAL(readyRead()), SLOT(readRegularFile_readyRead()));
+ − 801
enterLoop(10);
+ − 802
if (timeout())
+ − 803
QFAIL("Network operation timed out");
+ − 804
+ − 805
socket->close();
+ − 806
QByteArray array = socket->readAll();
+ − 807
QCOMPARE(array.size(), 0);
+ − 808
+ − 809
delete socket;
+ − 810
}
+ − 811
+ − 812
//----------------------------------------------------------------------------------
+ − 813
void tst_QTcpSocket::openCloseOpenClose()
+ − 814
{
+ − 815
QTcpSocket *socket = newSocket();
+ − 816
+ − 817
for (int i = 0; i < 3; ++i) {
+ − 818
QCOMPARE(socket->state(), QTcpSocket::UnconnectedState);
+ − 819
QCOMPARE(int(socket->openMode()), int(QIODevice::NotOpen));
+ − 820
QVERIFY(socket->isSequential());
+ − 821
QVERIFY(!socket->isOpen());
+ − 822
QVERIFY(socket->socketType() == QTcpSocket::TcpSocket);
+ − 823
+ − 824
char c;
+ − 825
QCOMPARE(socket->getChar(&c), false);
+ − 826
QCOMPARE((int) socket->bytesAvailable(), 0);
+ − 827
QCOMPARE(socket->canReadLine(), false);
+ − 828
QCOMPARE(socket->readLine(), QByteArray());
+ − 829
QCOMPARE(socket->socketDescriptor(), -1);
+ − 830
QCOMPARE((int) socket->localPort(), 0);
+ − 831
QVERIFY(socket->localAddress() == QHostAddress());
+ − 832
QCOMPARE((int) socket->peerPort(), 0);
+ − 833
QVERIFY(socket->peerAddress() == QHostAddress());
+ − 834
QCOMPARE(socket->error(), QTcpSocket::UnknownSocketError);
+ − 835
QCOMPARE(socket->errorString(), QString("Unknown error"));
+ − 836
+ − 837
QVERIFY(socket->state() == QTcpSocket::UnconnectedState);
+ − 838
+ − 839
socket->connectToHost(QtNetworkSettings::serverName(), 143);
+ − 840
QVERIFY(socket->waitForConnected(10000));
+ − 841
socket->close();
+ − 842
}
+ − 843
+ − 844
delete socket;
+ − 845
}
+ − 846
+ − 847
//----------------------------------------------------------------------------------
+ − 848
void tst_QTcpSocket::connectDisconnectConnectDisconnect()
+ − 849
{
+ − 850
QTcpSocket *socket = newSocket();
+ − 851
+ − 852
for (int i = 0; i < 3; ++i) {
+ − 853
QCOMPARE(socket->state(), QTcpSocket::UnconnectedState);
+ − 854
QVERIFY(socket->socketType() == QTcpSocket::TcpSocket);
+ − 855
+ − 856
socket->connectToHost(QtNetworkSettings::serverName(), 143);
+ − 857
QVERIFY(socket->waitForReadyRead(10000));
+ − 858
QCOMPARE(QString::fromLatin1(socket->read(4)), QString("* OK"));
+ − 859
+ − 860
socket->disconnectFromHost();
+ − 861
if (socket->state() != QTcpSocket::UnconnectedState)
+ − 862
QVERIFY(socket->waitForDisconnected(10000));
+ − 863
QCOMPARE(int(socket->openMode()), int(QIODevice::ReadWrite));
+ − 864
}
+ − 865
+ − 866
delete socket;
+ − 867
}
+ − 868
+ − 869
//----------------------------------------------------------------------------------
+ − 870
void tst_QTcpSocket::disconnectWhileConnecting_data()
+ − 871
{
+ − 872
QTest::addColumn<QByteArray>("data");
+ − 873
QTest::addColumn<bool>("closeDirectly");
+ − 874
+ − 875
QTest::newRow("without-data") << QByteArray() << false;
+ − 876
QTest::newRow("without-data+close") << QByteArray() << true;
+ − 877
QTest::newRow("with-data") << QByteArray("Hello, world!") << false;
+ − 878
QTest::newRow("with-data+close") << QByteArray("Hello, world!") << true;
+ − 879
+ − 880
QByteArray bigData(1024*1024, '@');
+ − 881
QTest::newRow("with-big-data") << bigData << false;
+ − 882
QTest::newRow("with-big-data+close") << bigData << true;
+ − 883
}
+ − 884
+ − 885
void tst_QTcpSocket::disconnectWhileConnecting()
+ − 886
{
+ − 887
QFETCH(QByteArray, data);
+ − 888
+ − 889
QTcpServer server;
+ − 890
QVERIFY(server.listen(QHostAddress::LocalHost));
+ − 891
+ − 892
// proceed to the connect-write-disconnect
+ − 893
QTcpSocket *socket = newSocket();
+ − 894
socket->connectToHost("127.0.0.1", server.serverPort());
+ − 895
if (!data.isEmpty())
+ − 896
socket->write(data);
+ − 897
if (socket->state() == QAbstractSocket::ConnectedState)
+ − 898
QSKIP("localhost connections are immediate, test case is invalid", SkipSingle);
+ − 899
+ − 900
QFETCH(bool, closeDirectly);
+ − 901
if (closeDirectly) {
+ − 902
socket->close();
+ − 903
QCOMPARE(int(socket->openMode()), int(QIODevice::NotOpen));
+ − 904
} else {
+ − 905
socket->disconnectFromHost();
+ − 906
}
+ − 907
+ − 908
connect(socket, SIGNAL(disconnected()), SLOT(exitLoopSlot()));
+ − 909
#ifndef Q_OS_SYMBIAN
+ − 910
enterLoop(10);
+ − 911
#else
+ − 912
enterLoop(30);
+ − 913
#endif
+ − 914
QVERIFY2(!timeout(), "Network timeout");
+ − 915
QVERIFY(socket->state() == QAbstractSocket::UnconnectedState);
+ − 916
if (!closeDirectly) {
+ − 917
QCOMPARE(int(socket->openMode()), int(QIODevice::ReadWrite));
+ − 918
socket->close();
+ − 919
}
+ − 920
QCOMPARE(int(socket->openMode()), int(QIODevice::NotOpen));
+ − 921
+ − 922
// accept the other side and verify that it was sent properly:
+ − 923
QVERIFY(server.hasPendingConnections() || server.waitForNewConnection(0));
+ − 924
QTcpSocket *othersocket = server.nextPendingConnection();
+ − 925
if (othersocket->state() != QAbstractSocket::UnconnectedState)
+ − 926
QVERIFY2(othersocket->waitForDisconnected(10000), "Network timeout");
+ − 927
QVERIFY(othersocket->state() == QAbstractSocket::UnconnectedState);
+ − 928
QCOMPARE(othersocket->readAll(), data);
+ − 929
+ − 930
delete socket;
+ − 931
delete othersocket;
+ − 932
}
+ − 933
+ − 934
//----------------------------------------------------------------------------------
+ − 935
class ReceiverThread: public QThread
+ − 936
{
+ − 937
QTcpServer *server;
+ − 938
public:
+ − 939
int serverPort;
+ − 940
bool ok;
+ − 941
QByteArray receivedData;
+ − 942
volatile bool quit;
+ − 943
+ − 944
ReceiverThread()
+ − 945
: server(0), ok(false), quit(false)
+ − 946
{ }
+ − 947
+ − 948
~ReceiverThread() { /*delete server;*/ terminate(); wait(); }
+ − 949
+ − 950
bool listen()
+ − 951
{
+ − 952
server = new QTcpServer;
+ − 953
if (!server->listen(QHostAddress::LocalHost))
+ − 954
return false;
+ − 955
serverPort = server->serverPort();
+ − 956
server->moveToThread(this);
+ − 957
return true;
+ − 958
}
+ − 959
+ − 960
protected:
+ − 961
void run()
+ − 962
{
+ − 963
bool timedOut = false;
+ − 964
while (!quit) {
+ − 965
#ifndef Q_OS_SYMBIAN
+ − 966
if (server->waitForNewConnection(500, &timedOut))
+ − 967
#else
+ − 968
if (server->waitForNewConnection(5000, &timedOut))
+ − 969
#endif
+ − 970
break;
+ − 971
if (!timedOut)
+ − 972
return;
+ − 973
}
+ − 974
+ − 975
QTcpSocket *socket = server->nextPendingConnection();
+ − 976
while (!quit) {
+ − 977
#ifndef Q_OS_SYMBIAN
+ − 978
if (socket->waitForDisconnected(500))
+ − 979
#else
+ − 980
if (socket->waitForDisconnected(5000))
+ − 981
#endif
+ − 982
break;
+ − 983
if (socket->error() != QAbstractSocket::SocketTimeoutError)
+ − 984
return;
+ − 985
}
+ − 986
+ − 987
if (!quit) {
+ − 988
receivedData = socket->readAll();
+ − 989
ok = true;
+ − 990
}
+ − 991
delete socket;
+ − 992
delete server;
+ − 993
server = 0;
+ − 994
}
+ − 995
};
+ − 996
+ − 997
void tst_QTcpSocket::disconnectWhileConnectingNoEventLoop_data()
+ − 998
{
+ − 999
disconnectWhileConnecting_data();
+ − 1000
}
+ − 1001
+ − 1002
void tst_QTcpSocket::disconnectWhileConnectingNoEventLoop()
+ − 1003
{
+ − 1004
QFETCH(QByteArray, data);
+ − 1005
+ − 1006
ReceiverThread thread;
+ − 1007
QVERIFY(thread.listen());
+ − 1008
thread.start();
+ − 1009
+ − 1010
// proceed to the connect-write-disconnect
+ − 1011
QTcpSocket *socket = newSocket();
+ − 1012
socket->connectToHost("127.0.0.1", thread.serverPort);
+ − 1013
if (!data.isEmpty())
+ − 1014
socket->write(data);
+ − 1015
if (socket->state() == QAbstractSocket::ConnectedState) {
+ − 1016
thread.quit = true;
+ − 1017
thread.wait();
+ − 1018
QSKIP("localhost connections are immediate, test case is invalid", SkipSingle);
+ − 1019
}
+ − 1020
+ − 1021
QFETCH(bool, closeDirectly);
+ − 1022
if (closeDirectly) {
+ − 1023
socket->close();
+ − 1024
QCOMPARE(int(socket->openMode()), int(QIODevice::NotOpen));
+ − 1025
} else {
+ − 1026
socket->disconnectFromHost();
+ − 1027
}
+ − 1028
+ − 1029
#ifndef Q_OS_SYMBIAN
+ − 1030
QVERIFY2(socket->waitForDisconnected(10000), "Network timeout");
+ − 1031
#else
+ − 1032
QVERIFY2(socket->waitForDisconnected(30000), "Network timeout");
+ − 1033
#endif
+ − 1034
QVERIFY(socket->state() == QAbstractSocket::UnconnectedState);
+ − 1035
if (!closeDirectly) {
+ − 1036
QCOMPARE(int(socket->openMode()), int(QIODevice::ReadWrite));
+ − 1037
socket->close();
+ − 1038
}
+ − 1039
QCOMPARE(int(socket->openMode()), int(QIODevice::NotOpen));
+ − 1040
delete socket;
+ − 1041
+ − 1042
// check if the other side received everything ok
+ − 1043
QVERIFY(thread.wait(30000));
+ − 1044
QVERIFY(thread.ok);
+ − 1045
QCOMPARE(thread.receivedData, data);
+ − 1046
}
+ − 1047
+ − 1048
//----------------------------------------------------------------------------------
+ − 1049
void tst_QTcpSocket::disconnectWhileLookingUp_data()
+ − 1050
{
+ − 1051
QTest::addColumn<bool>("doClose");
+ − 1052
+ − 1053
QTest::newRow("disconnect") << false;
+ − 1054
QTest::newRow("close") << true;
+ − 1055
}
+ − 1056
+ − 1057
void tst_QTcpSocket::disconnectWhileLookingUp()
+ − 1058
{
+ − 1059
QFETCH_GLOBAL(bool, setProxy);
+ − 1060
if (setProxy)
+ − 1061
return; // we let the proxies do the lookup now
+ − 1062
+ − 1063
// just connect and disconnect, then make sure nothing weird happened
+ − 1064
QTcpSocket *socket = newSocket();
+ − 1065
socket->connectToHost(QtNetworkSettings::serverName(), 21);
7
+ − 1066
+ − 1067
// check that connect is in progress
+ − 1068
QVERIFY(socket->state() != QAbstractSocket::UnconnectedState);
0
+ − 1069
+ − 1070
QFETCH(bool, doClose);
+ − 1071
if (doClose) {
+ − 1072
socket->close();
+ − 1073
QVERIFY(socket->openMode() == QIODevice::NotOpen);
+ − 1074
} else {
+ − 1075
socket->disconnectFromHost();
+ − 1076
QVERIFY(socket->openMode() == QIODevice::ReadWrite);
+ − 1077
}
+ − 1078
+ − 1079
// let anything queued happen
+ − 1080
QEventLoop loop;
+ − 1081
#ifndef Q_OS_SYMBIAN
+ − 1082
QTimer::singleShot(50, &loop, SLOT(quit()));
+ − 1083
#else
+ − 1084
QTimer::singleShot(5000, &loop, SLOT(quit()));
+ − 1085
#endif
+ − 1086
loop.exec();
+ − 1087
+ − 1088
// recheck
+ − 1089
if (doClose) {
+ − 1090
QVERIFY(socket->openMode() == QIODevice::NotOpen);
+ − 1091
} else {
+ − 1092
QVERIFY(socket->openMode() == QIODevice::ReadWrite);
+ − 1093
}
+ − 1094
+ − 1095
QVERIFY(socket->state() == QAbstractSocket::UnconnectedState);
+ − 1096
}
+ − 1097
+ − 1098
//----------------------------------------------------------------------------------
+ − 1099
void tst_QTcpSocket::downloadBigFile()
+ − 1100
{
+ − 1101
if (tmpSocket)
+ − 1102
delete tmpSocket;
+ − 1103
tmpSocket = newSocket();
+ − 1104
+ − 1105
connect(tmpSocket, SIGNAL(connected()), SLOT(exitLoopSlot()));
+ − 1106
connect(tmpSocket, SIGNAL(readyRead()), SLOT(downloadBigFileSlot()));
+ − 1107
+ − 1108
tmpSocket->connectToHost(QtNetworkSettings::serverName(), 80);
+ − 1109
+ − 1110
enterLoop(30);
+ − 1111
if (timeout()) {
+ − 1112
delete tmpSocket;
+ − 1113
tmpSocket = 0;
+ − 1114
QFAIL("Network operation timed out");
+ − 1115
}
+ − 1116
+ − 1117
QByteArray hostName = QtNetworkSettings::serverName().toLatin1();
+ − 1118
QVERIFY(tmpSocket->state() == QAbstractSocket::ConnectedState);
+ − 1119
QVERIFY(tmpSocket->write("GET /mediumfile HTTP/1.0\r\n") > 0);
+ − 1120
QVERIFY(tmpSocket->write("HOST: ") > 0);
+ − 1121
QVERIFY(tmpSocket->write(hostName.data()) > 0);
+ − 1122
QVERIFY(tmpSocket->write("\r\n") > 0);
+ − 1123
QVERIFY(tmpSocket->write("\r\n") > 0);
+ − 1124
+ − 1125
bytesAvailable = 0;
+ − 1126
expectedLength = 0;
+ − 1127
readingBody = false;
+ − 1128
+ − 1129
QTime stopWatch;
+ − 1130
stopWatch.start();
+ − 1131
+ − 1132
enterLoop(600);
+ − 1133
if (timeout()) {
+ − 1134
delete tmpSocket;
+ − 1135
tmpSocket = 0;
+ − 1136
if (bytesAvailable > 0)
+ − 1137
qDebug("Slow Connection, only downloaded %ld of %d", long(bytesAvailable), 10000281);
+ − 1138
QFAIL("Network operation timed out");
+ − 1139
}
+ − 1140
+ − 1141
QCOMPARE(bytesAvailable, expectedLength);
+ − 1142
+ − 1143
qDebug("\t\t%.1fMB/%.1fs: %.1fMB/s",
+ − 1144
bytesAvailable / (1024.0 * 1024.0),
+ − 1145
stopWatch.elapsed() / 1024.0,
+ − 1146
(bytesAvailable / (stopWatch.elapsed() / 1000.0)) / (1024 * 1024));
+ − 1147
+ − 1148
delete tmpSocket;
+ − 1149
tmpSocket = 0;
+ − 1150
}
+ − 1151
+ − 1152
//----------------------------------------------------------------------------------
+ − 1153
void tst_QTcpSocket::exitLoopSlot()
+ − 1154
{
+ − 1155
exitLoop();
+ − 1156
}
+ − 1157
+ − 1158
//----------------------------------------------------------------------------------
+ − 1159
void tst_QTcpSocket::downloadBigFileSlot()
+ − 1160
{
+ − 1161
if (!readingBody) {
+ − 1162
while (tmpSocket->canReadLine()) {
+ − 1163
QByteArray array = tmpSocket->readLine();
+ − 1164
if (array.startsWith("Content-Length"))
+ − 1165
expectedLength = array.simplified().split(' ').at(1).toInt();
+ − 1166
if (array == "\r\n") {
+ − 1167
readingBody = true;
+ − 1168
break;
+ − 1169
}
+ − 1170
}
+ − 1171
}
+ − 1172
if (readingBody) {
+ − 1173
bytesAvailable += tmpSocket->readAll().size();
+ − 1174
if (bytesAvailable == expectedLength)
+ − 1175
exitLoop();
+ − 1176
}
+ − 1177
}
+ − 1178
+ − 1179
//----------------------------------------------------------------------------------
+ − 1180
void tst_QTcpSocket::readLine()
+ − 1181
{
+ − 1182
QTcpSocket *socket = newSocket();
+ − 1183
socket->connectToHost(QtNetworkSettings::serverName(), 143);
+ − 1184
QVERIFY(socket->waitForConnected(5000));
+ − 1185
+ − 1186
while (!socket->canReadLine())
+ − 1187
QVERIFY(socket->waitForReadyRead(10000));
+ − 1188
+ − 1189
char buffer[1024];
+ − 1190
int expectedReplySize = QtNetworkSettings::expectedReplyIMAP().size();
+ − 1191
Q_ASSERT(expectedReplySize >= 3);
+ − 1192
QCOMPARE(socket->readLine(buffer, sizeof(buffer)), qint64(expectedReplySize));
+ − 1193
+ − 1194
QCOMPARE((int) buffer[expectedReplySize-2], (int) '\r');
+ − 1195
QCOMPARE((int) buffer[expectedReplySize-1], (int) '\n');
+ − 1196
QCOMPARE((int) buffer[expectedReplySize], (int) '\0');
+ − 1197
+ − 1198
QCOMPARE(socket->write("1 NOOP\r\n"), qint64(8));
+ − 1199
+ − 1200
while (socket->bytesAvailable() < 10)
+ − 1201
QVERIFY(socket->waitForReadyRead(10000));
+ − 1202
+ − 1203
QCOMPARE(socket->readLine(buffer, 11), qint64(10));
+ − 1204
QCOMPARE((const char *)buffer, "1 OK Compl");
+ − 1205
+ − 1206
while (socket->bytesAvailable() < 6)
+ − 1207
QVERIFY(socket->waitForReadyRead(10000));
+ − 1208
+ − 1209
QCOMPARE(socket->readLine(buffer, 11), qint64(6));
+ − 1210
QCOMPARE((const char *)buffer, "eted\r\n");
+ − 1211
+ − 1212
QVERIFY(!socket->waitForReadyRead(100));
+ − 1213
QCOMPARE(socket->readLine(buffer, sizeof(buffer)), qint64(0));
+ − 1214
QVERIFY(socket->error() == QAbstractSocket::SocketTimeoutError
+ − 1215
|| socket->error() == QAbstractSocket::RemoteHostClosedError);
+ − 1216
QCOMPARE(socket->bytesAvailable(), qint64(0));
+ − 1217
+ − 1218
socket->close();
+ − 1219
QCOMPARE(socket->readLine(buffer, sizeof(buffer)), qint64(-1));
+ − 1220
+ − 1221
delete socket;
+ − 1222
}
+ − 1223
+ − 1224
//----------------------------------------------------------------------------------
+ − 1225
void tst_QTcpSocket::readLineString()
+ − 1226
{
+ − 1227
QTcpSocket *socket = newSocket();
+ − 1228
// QByteArray expected("* OK fluke Cyrus IMAP4 v2.2.12 server ready\r\n");
+ − 1229
QByteArray expected("* OK [CAPABILITY IMAP4 IMAP4rev1 LITERAL+ ID STARTTLS LOGINDISABLED] qt-test-server.qt-test-net Cyrus IMAP4 v2.3.11-Mandriva-RPM-2.3.11-6mdv2008.1 server ready\r\n");
+ − 1230
socket->connectToHost(QtNetworkSettings::serverName(), 143);
+ − 1231
QVERIFY(socket->waitForReadyRead(10000));
+ − 1232
+ − 1233
QByteArray arr = socket->readLine();
+ − 1234
QCOMPARE(arr, QtNetworkSettings::expectedReplyIMAP());
+ − 1235
+ − 1236
delete socket;
+ − 1237
}
+ − 1238
+ − 1239
//----------------------------------------------------------------------------------
+ − 1240
void tst_QTcpSocket::readChunks()
+ − 1241
{
+ − 1242
QTcpSocket *socket = newSocket();
+ − 1243
socket->connectToHost(QtNetworkSettings::serverName(), 143);
+ − 1244
QVERIFY(socket->waitForConnected(10000));
+ − 1245
QVERIFY(socket->waitForReadyRead(5000));
+ − 1246
+ − 1247
char buf[4096];
+ − 1248
memset(buf, '@', sizeof(buf));
+ − 1249
qint64 dataLength = socket->read(buf, sizeof(buf));
+ − 1250
QVERIFY(dataLength > 0);
+ − 1251
+ − 1252
QCOMPARE(buf[dataLength - 2], '\r');
+ − 1253
QCOMPARE(buf[dataLength - 1], '\n');
+ − 1254
QCOMPARE(buf[dataLength], '@');
+ − 1255
+ − 1256
delete socket;
+ − 1257
}
+ − 1258
+ − 1259
//----------------------------------------------------------------------------------
+ − 1260
void tst_QTcpSocket::waitForBytesWritten()
+ − 1261
{
+ − 1262
QTcpSocket *socket = newSocket();
+ − 1263
socket->connectToHost(QtNetworkSettings::serverName(), 22);
+ − 1264
QVERIFY(socket->waitForConnected(10000));
+ − 1265
+ − 1266
socket->write(QByteArray(10000, '@'));
+ − 1267
qint64 toWrite = socket->bytesToWrite();
+ − 1268
QVERIFY(socket->waitForBytesWritten(5000));
+ − 1269
QVERIFY(toWrite > socket->bytesToWrite());
+ − 1270
+ − 1271
delete socket;
+ − 1272
}
+ − 1273
+ − 1274
//----------------------------------------------------------------------------------
+ − 1275
void tst_QTcpSocket::waitForReadyRead()
+ − 1276
{
+ − 1277
QTcpSocket *socket = newSocket();
+ − 1278
socket->connectToHost(QtNetworkSettings::serverName(), 22);
+ − 1279
socket->waitForReadyRead(0);
+ − 1280
delete socket;
+ − 1281
}
+ − 1282
+ − 1283
//----------------------------------------------------------------------------------
+ − 1284
void tst_QTcpSocket::flush()
+ − 1285
{
+ − 1286
QTcpSocket *socket = newSocket();
+ − 1287
socket->flush();
+ − 1288
+ − 1289
connect(socket, SIGNAL(connected()), SLOT(exitLoopSlot()));
+ − 1290
socket->connectToHost(QtNetworkSettings::serverName(), 143);
+ − 1291
enterLoop(5000);
+ − 1292
QVERIFY(socket->isOpen());
+ − 1293
+ − 1294
socket->write("1 LOGOUT\r\n");
+ − 1295
QCOMPARE(socket->bytesToWrite(), qint64(10));
+ − 1296
socket->flush();
+ − 1297
QCOMPARE(socket->bytesToWrite(), qint64(0));
+ − 1298
socket->close();
+ − 1299
+ − 1300
delete socket;
+ − 1301
}
+ − 1302
+ − 1303
//----------------------------------------------------------------------------------
+ − 1304
void tst_QTcpSocket::synchronousApi()
+ − 1305
{
+ − 1306
QTcpSocket *ftpSocket = newSocket();
+ − 1307
ftpSocket->connectToHost(QtNetworkSettings::serverName(), 21);
+ − 1308
ftpSocket->write("QUIT\r\n");
+ − 1309
QVERIFY(ftpSocket->waitForDisconnected(10000));
+ − 1310
QVERIFY(ftpSocket->bytesAvailable() > 0);
+ − 1311
QByteArray arr = ftpSocket->readAll();
+ − 1312
QVERIFY(arr.size() > 0);
+ − 1313
delete ftpSocket;
+ − 1314
}
+ − 1315
+ − 1316
//----------------------------------------------------------------------------------
+ − 1317
void tst_QTcpSocket::dontCloseOnTimeout()
+ − 1318
{
+ − 1319
QTcpServer server;
+ − 1320
server.setProxy(QNetworkProxy(QNetworkProxy::NoProxy));
+ − 1321
QVERIFY(server.listen());
+ − 1322
+ − 1323
QHostAddress serverAddress = QHostAddress::LocalHost;
+ − 1324
if (!(server.serverAddress() == QHostAddress::Any))
+ − 1325
serverAddress = server.serverAddress();
+ − 1326
+ − 1327
QTcpSocket *socket = newSocket();
+ − 1328
socket->connectToHost(serverAddress, server.serverPort());
+ − 1329
#ifndef Q_OS_SYMBIAN
+ − 1330
QVERIFY(!socket->waitForReadyRead(100));
+ − 1331
#else
+ − 1332
QVERIFY(!socket->waitForReadyRead(5000));
+ − 1333
#endif
+ − 1334
QCOMPARE(socket->error(), QTcpSocket::SocketTimeoutError);
+ − 1335
QVERIFY(socket->isOpen());
+ − 1336
+ − 1337
#ifndef Q_OS_SYMBIAN
+ − 1338
QVERIFY(!socket->waitForDisconnected(100));
+ − 1339
#else
+ − 1340
QVERIFY(!socket->waitForDisconnected(5000));
+ − 1341
#endif
+ − 1342
QCOMPARE(socket->error(), QTcpSocket::SocketTimeoutError);
+ − 1343
QVERIFY(socket->isOpen());
+ − 1344
+ − 1345
delete socket;
+ − 1346
}
+ − 1347
+ − 1348
//----------------------------------------------------------------------------------
+ − 1349
void tst_QTcpSocket::recursiveReadyRead()
+ − 1350
{
+ − 1351
QTcpSocket *smtp = newSocket();
+ − 1352
connect(smtp, SIGNAL(connected()), SLOT(exitLoopSlot()));
+ − 1353
connect(smtp, SIGNAL(readyRead()), SLOT(recursiveReadyReadSlot()));
+ − 1354
tmpSocket = smtp;
+ − 1355
+ − 1356
QSignalSpy spy(smtp, SIGNAL(readyRead()));
+ − 1357
+ − 1358
smtp->connectToHost("smtp.trolltech.com", 25);
+ − 1359
enterLoop(30);
+ − 1360
QVERIFY2(!timeout(),
+ − 1361
"Timed out when connecting to smtp.trolltech.com:25");
+ − 1362
+ − 1363
enterLoop(30);
+ − 1364
QVERIFY2(!timeout(),
+ − 1365
"Timed out when waiting for the readyRead() signal");
+ − 1366
+ − 1367
QCOMPARE(spy.count(), 1);
+ − 1368
+ − 1369
delete smtp;
+ − 1370
}
+ − 1371
+ − 1372
void tst_QTcpSocket::recursiveReadyReadSlot()
+ − 1373
{
+ − 1374
// make sure the server spits out more data
+ − 1375
tmpSocket->write("NOOP\r\n");
+ − 1376
tmpSocket->flush();
+ − 1377
+ − 1378
// indiscriminately enter the event loop and start processing
+ − 1379
// events again. but oops! future socket notifications will cause
+ − 1380
// undesired recursive behavior. Unless QTcpSocket is smart, which
+ − 1381
// it of course is. :-)
+ − 1382
QEventLoop loop;
+ − 1383
for (int i = 0; i < 100; ++i)
+ − 1384
loop.processEvents();
+ − 1385
+ − 1386
// all we really wanted to do was process some events, then exit
+ − 1387
// the loop
+ − 1388
exitLoop();
+ − 1389
}
+ − 1390
+ − 1391
//----------------------------------------------------------------------------------
+ − 1392
void tst_QTcpSocket::atEnd()
+ − 1393
{
+ − 1394
QTcpSocket *socket = newSocket();
+ − 1395
socket->connectToHost(QtNetworkSettings::serverName(), 21);
+ − 1396
+ − 1397
QVERIFY(socket->waitForReadyRead(15000));
+ − 1398
QTextStream stream(socket);
+ − 1399
QVERIFY(!stream.atEnd());
+ − 1400
QString greeting = stream.readLine();
+ − 1401
QVERIFY(stream.atEnd());
+ − 1402
// QCOMPARE(greeting, QString("220 (vsFTPd 2.0.4)"));
+ − 1403
QCOMPARE(greeting, QString("220 (vsFTPd 2.0.5)"));
+ − 1404
+ − 1405
delete socket;
+ − 1406
}
+ − 1407
+ − 1408
class TestThread : public QThread
+ − 1409
{
+ − 1410
Q_OBJECT
+ − 1411
+ − 1412
public:
+ − 1413
inline QByteArray data() const
+ − 1414
{
+ − 1415
return socketData;
+ − 1416
}
+ − 1417
+ − 1418
protected:
+ − 1419
inline void run()
+ − 1420
{
+ − 1421
#ifndef QT_NO_OPENSSL
+ − 1422
QFETCH_GLOBAL(bool, ssl);
+ − 1423
if (ssl)
+ − 1424
socket = new QSslSocket;
+ − 1425
else
+ − 1426
#endif
+ − 1427
socket = new QTcpSocket;
+ − 1428
connect(socket, SIGNAL(readyRead()), this, SLOT(getData()), Qt::DirectConnection);
+ − 1429
connect(socket, SIGNAL(disconnected()), this, SLOT(closed()), Qt::DirectConnection);
+ − 1430
connect(socket, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
+ − 1431
SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), Qt::DirectConnection);
+ − 1432
+ − 1433
socket->connectToHost(QtNetworkSettings::serverName(), 21);
+ − 1434
socket->write("QUIT\r\n");
+ − 1435
exec();
+ − 1436
+ − 1437
delete socket;
+ − 1438
}
+ − 1439
+ − 1440
private slots:
+ − 1441
inline void getData()
+ − 1442
{
+ − 1443
socketData += socket->readAll();
+ − 1444
}
+ − 1445
+ − 1446
inline void closed()
+ − 1447
{
+ − 1448
quit();
+ − 1449
}
+ − 1450
inline void proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *auth)
+ − 1451
{
+ − 1452
auth->setUser("qsockstest");
+ − 1453
auth->setPassword("password");
+ − 1454
}
+ − 1455
private:
+ − 1456
int exitCode;
+ − 1457
QTcpSocket *socket;
+ − 1458
QByteArray socketData;
+ − 1459
};
+ − 1460
+ − 1461
//----------------------------------------------------------------------------------
+ − 1462
void tst_QTcpSocket::socketInAThread()
+ − 1463
{
+ − 1464
for (int i = 0; i < 3; ++i) {
+ − 1465
TestThread thread;
+ − 1466
thread.start();
+ − 1467
QVERIFY(thread.wait(15000));
+ − 1468
QCOMPARE(thread.data(), QtNetworkSettings::expectedReplyFtp());
+ − 1469
}
+ − 1470
}
+ − 1471
+ − 1472
//----------------------------------------------------------------------------------
+ − 1473
void tst_QTcpSocket::socketsInThreads()
+ − 1474
{
+ − 1475
for (int i = 0; i < 3; ++i) {
+ − 1476
TestThread thread1;
+ − 1477
TestThread thread2;
+ − 1478
TestThread thread3;
+ − 1479
+ − 1480
thread1.start();
+ − 1481
thread2.start();
+ − 1482
thread3.start();
+ − 1483
+ − 1484
QVERIFY(thread2.wait(15000));
+ − 1485
QVERIFY(thread3.wait(15000));
+ − 1486
QVERIFY(thread1.wait(15000));
+ − 1487
+ − 1488
QCOMPARE(thread1.data(),QtNetworkSettings::expectedReplyFtp());
+ − 1489
QCOMPARE(thread2.data(),QtNetworkSettings::expectedReplyFtp());
+ − 1490
QCOMPARE(thread3.data(),QtNetworkSettings::expectedReplyFtp());
+ − 1491
}
+ − 1492
}
+ − 1493
+ − 1494
//----------------------------------------------------------------------------------
+ − 1495
void tst_QTcpSocket::waitForReadyReadInASlot()
+ − 1496
{
+ − 1497
QTcpSocket *socket = newSocket();
+ − 1498
tmpSocket = socket;
+ − 1499
connect(socket, SIGNAL(connected()), this, SLOT(waitForReadyReadInASlotSlot()));
+ − 1500
+ − 1501
socket->connectToHost(QtNetworkSettings::serverName(), 80);
+ − 1502
socket->write("GET / HTTP/1.0\r\n\r\n");
+ − 1503
+ − 1504
enterLoop(30);
+ − 1505
QVERIFY(!timeout());
+ − 1506
+ − 1507
delete socket;
+ − 1508
}
+ − 1509
+ − 1510
void tst_QTcpSocket::waitForReadyReadInASlotSlot()
+ − 1511
{
+ − 1512
QVERIFY(tmpSocket->waitForReadyRead(10000));
+ − 1513
exitLoop();
+ − 1514
}
+ − 1515
+ − 1516
class RemoteCloseErrorServer : public QTcpServer
+ − 1517
{
+ − 1518
Q_OBJECT
+ − 1519
public:
+ − 1520
RemoteCloseErrorServer()
+ − 1521
{
+ − 1522
connect(this, SIGNAL(newConnection()),
+ − 1523
this, SLOT(getConnection()));
+ − 1524
}
+ − 1525
+ − 1526
private slots:
+ − 1527
void getConnection()
+ − 1528
{
+ − 1529
tst_QTcpSocket::exitLoop();
+ − 1530
}
+ − 1531
};
+ − 1532
+ − 1533
//----------------------------------------------------------------------------------
+ − 1534
void tst_QTcpSocket::remoteCloseError()
+ − 1535
{
+ − 1536
RemoteCloseErrorServer server;
+ − 1537
QVERIFY(server.listen(QHostAddress::LocalHost));
+ − 1538
+ − 1539
QCoreApplication::instance()->processEvents();
+ − 1540
+ − 1541
QTcpSocket *clientSocket = newSocket();
+ − 1542
connect(clientSocket, SIGNAL(readyRead()), this, SLOT(exitLoopSlot()));
+ − 1543
+ − 1544
clientSocket->connectToHost(server.serverAddress(), server.serverPort());
+ − 1545
+ − 1546
enterLoop(30);
+ − 1547
QVERIFY(!timeout());
+ − 1548
+ − 1549
QVERIFY(server.hasPendingConnections());
+ − 1550
QTcpSocket *serverSocket = server.nextPendingConnection();
+ − 1551
connect(clientSocket, SIGNAL(disconnected()), this, SLOT(exitLoopSlot()));
+ − 1552
+ − 1553
serverSocket->write("Hello");
+ − 1554
+ − 1555
enterLoop(30);
+ − 1556
QVERIFY(!timeout());
+ − 1557
+ − 1558
QCOMPARE(clientSocket->bytesAvailable(), qint64(5));
+ − 1559
+ − 1560
qRegisterMetaType<QAbstractSocket::SocketError>("QAbstractSocket::SocketError");
+ − 1561
QSignalSpy errorSpy(clientSocket, SIGNAL(error(QAbstractSocket::SocketError)));
+ − 1562
QSignalSpy disconnectedSpy(clientSocket, SIGNAL(disconnected()));
+ − 1563
+ − 1564
clientSocket->write("World");
+ − 1565
serverSocket->disconnectFromHost();
+ − 1566
+ − 1567
tmpSocket = clientSocket;
+ − 1568
connect(clientSocket, SIGNAL(error(QAbstractSocket::SocketError)),
+ − 1569
this, SLOT(remoteCloseErrorSlot()));
+ − 1570
+ − 1571
enterLoop(30);
+ − 1572
QVERIFY(!timeout());
+ − 1573
+ − 1574
QCOMPARE(disconnectedSpy.count(), 1);
+ − 1575
QCOMPARE(errorSpy.count(), 1);
+ − 1576
QCOMPARE(clientSocket->error(), QAbstractSocket::RemoteHostClosedError);
+ − 1577
+ − 1578
delete serverSocket;
+ − 1579
+ − 1580
clientSocket->connectToHost(server.serverAddress(), server.serverPort());
+ − 1581
+ − 1582
enterLoop(30);
+ − 1583
QVERIFY(!timeout());
+ − 1584
+ − 1585
QVERIFY(server.hasPendingConnections());
+ − 1586
serverSocket = server.nextPendingConnection();
+ − 1587
serverSocket->disconnectFromHost();
+ − 1588
+ − 1589
enterLoop(30);
+ − 1590
QVERIFY(!timeout());
+ − 1591
+ − 1592
QCOMPARE(clientSocket->state(), QAbstractSocket::UnconnectedState);
+ − 1593
+ − 1594
delete clientSocket;
+ − 1595
}
+ − 1596
+ − 1597
void tst_QTcpSocket::remoteCloseErrorSlot()
+ − 1598
{
+ − 1599
QCOMPARE(tmpSocket->state(), QAbstractSocket::ConnectedState);
+ − 1600
static_cast<QTcpSocket *>(sender())->close();
+ − 1601
}
+ − 1602
+ − 1603
void tst_QTcpSocket::messageBoxSlot()
+ − 1604
{
+ − 1605
#if !defined(Q_OS_VXWORKS) // no gui
+ − 1606
QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
+ − 1607
socket->deleteLater();
+ − 1608
QMessageBox box;
+ − 1609
QTimer::singleShot(100, &box, SLOT(close()));
+ − 1610
+ − 1611
// This should not delete the socket
+ − 1612
box.exec();
+ − 1613
+ − 1614
// Fire a non-0 singleshot to leave time for the delete
+ − 1615
QTimer::singleShot(250, this, SLOT(exitLoopSlot()));
+ − 1616
#endif
+ − 1617
}
+ − 1618
//----------------------------------------------------------------------------------
+ − 1619
void tst_QTcpSocket::openMessageBoxInErrorSlot()
+ − 1620
{
+ − 1621
#if defined(Q_OS_VXWORKS) // no gui
+ − 1622
QSKIP("no default gui available on VxWorks", SkipAll);
+ − 1623
#else
+ − 1624
QTcpSocket *socket = newSocket();
+ − 1625
QPointer<QTcpSocket> p(socket);
+ − 1626
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(messageBoxSlot()));
+ − 1627
+ − 1628
socket->connectToHost("hostnotfoundhostnotfound.troll.no", 9999); // Host not found, fyi
+ − 1629
enterLoop(30);
+ − 1630
QVERIFY(!p);
+ − 1631
#endif
+ − 1632
}
+ − 1633
+ − 1634
//----------------------------------------------------------------------------------
+ − 1635
#ifndef Q_OS_WIN
+ − 1636
void tst_QTcpSocket::connectToLocalHostNoService()
+ − 1637
{
+ − 1638
// This test was created after we received a report that claimed
+ − 1639
// QTcpSocket would crash if trying to connect to "localhost" on a random
+ − 1640
// port with no service listening.
+ − 1641
QTcpSocket *socket = newSocket();
+ − 1642
socket->connectToHost("localhost", 31415); // no service running here, one suspects
+ − 1643
+ − 1644
while(socket->state() == QTcpSocket::HostLookupState || socket->state() == QTcpSocket::ConnectingState) {
+ − 1645
QTest::qWait(100);
+ − 1646
}
+ − 1647
QCOMPARE(socket->state(), QTcpSocket::UnconnectedState);
+ − 1648
delete socket;
+ − 1649
}
+ − 1650
#endif
+ − 1651
+ − 1652
//----------------------------------------------------------------------------------
+ − 1653
void tst_QTcpSocket::waitForConnectedInHostLookupSlot()
+ − 1654
{
+ − 1655
// This test tries to reproduce the problem where waitForConnected() is
+ − 1656
// called at a point where the host lookup is already done. QTcpSocket
+ − 1657
// will try to abort the "pending lookup", but since it's already done and
+ − 1658
// the queued signal is already underway, we will receive the signal after
+ − 1659
// waitForConnected() has returned, and control goes back to the event
+ − 1660
// loop. When the signal has been received, the connection is torn down,
+ − 1661
// then reopened. Yikes. If we reproduce this by calling
+ − 1662
// waitForConnected() inside hostLookupSlot(), it will even crash.
+ − 1663
tmpSocket = newSocket();
+ − 1664
QEventLoop loop;
+ − 1665
connect(tmpSocket, SIGNAL(connected()), &loop, SLOT(quit()));
+ − 1666
QTimer timer;
+ − 1667
connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
+ − 1668
QSignalSpy timerSpy(&timer, SIGNAL(timeout()));
+ − 1669
timer.start(15000);
+ − 1670
+ − 1671
connect(tmpSocket, SIGNAL(hostFound()), this, SLOT(hostLookupSlot()));
+ − 1672
tmpSocket->connectToHost(QtNetworkSettings::serverName(), 143);
+ − 1673
7
+ − 1674
// only execute the loop if not already connected
+ − 1675
if (tmpSocket->state() != QAbstractSocket::ConnectedState)
+ − 1676
loop.exec();
+ − 1677
0
+ − 1678
QCOMPARE(timerSpy.count(), 0);
+ − 1679
+ − 1680
delete tmpSocket;
+ − 1681
}
+ − 1682
+ − 1683
void tst_QTcpSocket::hostLookupSlot()
+ − 1684
{
+ − 1685
// This will fail to cancel the pending signal
+ − 1686
QVERIFY(tmpSocket->waitForConnected(10000));
+ − 1687
}
+ − 1688
+ − 1689
class Foo : public QObject
+ − 1690
{
+ − 1691
Q_OBJECT
+ − 1692
QTcpSocket *sock;
+ − 1693
public:
+ − 1694
bool attemptedToConnect;
+ − 1695
bool networkTimeout;
+ − 1696
int count;
+ − 1697
+ − 1698
inline Foo(QObject *parent = 0) : QObject(parent)
+ − 1699
{
+ − 1700
attemptedToConnect = false;
+ − 1701
networkTimeout = false;
+ − 1702
count = 0;
+ − 1703
#ifndef QT_NO_OPENSSL
+ − 1704
QFETCH_GLOBAL(bool, ssl);
+ − 1705
if (ssl)
+ − 1706
sock = new QSslSocket;
+ − 1707
else
+ − 1708
#endif
+ − 1709
sock = new QTcpSocket;
+ − 1710
connect(sock, SIGNAL(connected()), this, SLOT(connectedToIt()));
+ − 1711
connect(sock, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
+ − 1712
SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
+ − 1713
}
+ − 1714
+ − 1715
inline ~Foo()
+ − 1716
{
+ − 1717
delete sock;
+ − 1718
}
+ − 1719
+ − 1720
public slots:
+ − 1721
inline void connectedToIt()
+ − 1722
{ count++; }
+ − 1723
+ − 1724
inline void doIt()
+ − 1725
{
+ − 1726
attemptedToConnect = true;
+ − 1727
sock->connectToHost(QtNetworkSettings::serverName(), 80);
+ − 1728
+ − 1729
#ifdef Q_OS_MAC
+ − 1730
pthread_yield_np();
+ − 1731
#elif defined Q_OS_LINUX
+ − 1732
pthread_yield();
+ − 1733
#endif
+ − 1734
if (!sock->waitForConnected()) {
+ − 1735
networkTimeout = true;
+ − 1736
}
+ − 1737
tst_QTcpSocket::exitLoop();
+ − 1738
}
+ − 1739
+ − 1740
inline void exitLoop()
+ − 1741
{
+ − 1742
tst_QTcpSocket::exitLoop();
+ − 1743
}
+ − 1744
+ − 1745
inline void proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *auth)
+ − 1746
{
+ − 1747
auth->setUser("qsockstest");
+ − 1748
auth->setPassword("password");
+ − 1749
}
+ − 1750
};
+ − 1751
+ − 1752
//----------------------------------------------------------------------------------
+ − 1753
void tst_QTcpSocket::waitForConnectedInHostLookupSlot2()
+ − 1754
{
+ − 1755
#if defined(Q_OS_WIN) || defined(Q_OS_VXWORKS)
+ − 1756
QSKIP("waitForConnectedInHostLookupSlot2 is not run on Windows and VxWorks", SkipAll);
+ − 1757
#else
+ − 1758
+ − 1759
Foo foo;
+ − 1760
QPushButton top("Go", 0);
+ − 1761
top.show();
+ − 1762
connect(&top, SIGNAL(clicked()), &foo, SLOT(doIt()));
+ − 1763
+ − 1764
QTimer::singleShot(100, &top, SLOT(animateClick()));
+ − 1765
QTimer::singleShot(5000, &foo, SLOT(exitLoop()));
+ − 1766
+ − 1767
enterLoop(30);
+ − 1768
if (timeout() || foo.networkTimeout)
+ − 1769
QFAIL("Network timeout");
+ − 1770
+ − 1771
QVERIFY(foo.attemptedToConnect);
+ − 1772
QCOMPARE(foo.count, 1);
+ − 1773
#endif
+ − 1774
}
+ − 1775
+ − 1776
//----------------------------------------------------------------------------------
+ − 1777
void tst_QTcpSocket::readyReadSignalsAfterWaitForReadyRead()
+ − 1778
{
+ − 1779
QTcpSocket *socket = newSocket();
+ − 1780
+ − 1781
QSignalSpy readyReadSpy(socket, SIGNAL(readyRead()));
+ − 1782
+ − 1783
// Connect
+ − 1784
socket->connectToHost(QtNetworkSettings::serverName(), 143);
+ − 1785
+ − 1786
// Wait for the read
+ − 1787
QVERIFY(socket->waitForReadyRead(10000));
+ − 1788
+ − 1789
QCOMPARE(readyReadSpy.count(), 1);
+ − 1790
+ − 1791
QString s = socket->readLine();
+ − 1792
QCOMPARE(s.toLatin1().constData(), QtNetworkSettings::expectedReplyIMAP().constData());
+ − 1793
QCOMPARE(socket->bytesAvailable(), qint64(0));
+ − 1794
+ − 1795
QCoreApplication::instance()->processEvents();
+ − 1796
QCOMPARE(socket->bytesAvailable(), qint64(0));
+ − 1797
QCOMPARE(readyReadSpy.count(), 1);
+ − 1798
+ − 1799
delete socket;
+ − 1800
}
+ − 1801
+ − 1802
class TestThread2 : public QThread
+ − 1803
{
+ − 1804
Q_OBJECT
+ − 1805
public:
+ − 1806
void run()
+ − 1807
{
+ − 1808
QFile fileWriter("fifo");
+ − 1809
QVERIFY(fileWriter.open(QFile::WriteOnly));
+ − 1810
QCOMPARE(fileWriter.write(QByteArray(32, '@')), qint64(32));
+ − 1811
QCOMPARE(fileWriter.write(QByteArray(32, '@')), qint64(32));
+ − 1812
QCOMPARE(fileWriter.write(QByteArray(32, '@')), qint64(32));
+ − 1813
QCOMPARE(fileWriter.write(QByteArray(32, '@')), qint64(32));
+ − 1814
}
+ − 1815
};
+ − 1816
+ − 1817
//----------------------------------------------------------------------------------
+ − 1818
#ifdef Q_OS_LINUX
+ − 1819
void tst_QTcpSocket::linuxKernelBugLocalSocket()
+ − 1820
{
+ − 1821
QFile::remove("fifo");
+ − 1822
mkfifo("fifo", 0666);
+ − 1823
+ − 1824
TestThread2 test;
+ − 1825
test.start();
+ − 1826
+ − 1827
QFile fileReader("fifo");
+ − 1828
QVERIFY(fileReader.open(QFile::ReadOnly));
+ − 1829
+ − 1830
test.wait();
+ − 1831
+ − 1832
QTcpSocket *socket = newSocket();
+ − 1833
socket->setSocketDescriptor(fileReader.handle());
+ − 1834
QVERIFY(socket->waitForReadyRead(5000));
+ − 1835
QCOMPARE(socket->bytesAvailable(), qint64(128));
+ − 1836
+ − 1837
QFile::remove("fifo");
+ − 1838
+ − 1839
delete socket;
+ − 1840
}
+ − 1841
#endif
+ − 1842
+ − 1843
//----------------------------------------------------------------------------------
+ − 1844
void tst_QTcpSocket::abortiveClose()
+ − 1845
{
+ − 1846
QTcpServer server;
+ − 1847
QVERIFY(server.listen(QHostAddress::LocalHost));
+ − 1848
connect(&server, SIGNAL(newConnection()), this, SLOT(exitLoopSlot()));
+ − 1849
+ − 1850
QTcpSocket *clientSocket = newSocket();
+ − 1851
clientSocket->connectToHost(server.serverAddress(), server.serverPort());
+ − 1852
+ − 1853
enterLoop(10);
+ − 1854
QVERIFY(server.hasPendingConnections());
+ − 1855
+ − 1856
QVERIFY(tmpSocket = server.nextPendingConnection());
+ − 1857
+ − 1858
qRegisterMetaType<QAbstractSocket::SocketError>("QAbstractSocket::SocketError");
+ − 1859
QSignalSpy readyReadSpy(clientSocket, SIGNAL(readyRead()));
+ − 1860
QSignalSpy errorSpy(clientSocket, SIGNAL(error(QAbstractSocket::SocketError)));
+ − 1861
+ − 1862
connect(clientSocket, SIGNAL(disconnected()), this, SLOT(exitLoopSlot()));
+ − 1863
QTimer::singleShot(0, this, SLOT(abortiveClose_abortSlot()));
+ − 1864
+ − 1865
enterLoop(5);
+ − 1866
+ − 1867
QCOMPARE(readyReadSpy.count(), 0);
+ − 1868
QCOMPARE(errorSpy.count(), 1);
+ − 1869
+ − 1870
QCOMPARE(*static_cast<const int *>(errorSpy.at(0).at(0).constData()),
+ − 1871
int(QAbstractSocket::RemoteHostClosedError));
+ − 1872
+ − 1873
delete clientSocket;
+ − 1874
}
+ − 1875
+ − 1876
void tst_QTcpSocket::abortiveClose_abortSlot()
+ − 1877
{
+ − 1878
tmpSocket->abort();
+ − 1879
}
+ − 1880
+ − 1881
//----------------------------------------------------------------------------------
+ − 1882
void tst_QTcpSocket::localAddressEmptyOnBSD()
+ − 1883
{
+ − 1884
QTcpServer server;
+ − 1885
QVERIFY(server.listen(QHostAddress::LocalHost));
+ − 1886
+ − 1887
QTcpSocket *tcpSocket = 0;
+ − 1888
// we try 10 times, but note that this doesn't always provoke the bug
+ − 1889
for (int i = 0; i < 10; ++i) {
+ − 1890
delete tcpSocket;
+ − 1891
tcpSocket = newSocket();
+ − 1892
tcpSocket->connectToHost(QHostAddress::LocalHost, server.serverPort());
+ − 1893
if (!tcpSocket->waitForConnected(0)) {
+ − 1894
// to provoke the bug, we need a local socket that connects immediately
+ − 1895
// --i;
+ − 1896
tcpSocket->abort();
+ − 1897
if (tcpSocket->state() != QTcpSocket::UnconnectedState)
+ − 1898
QVERIFY(tcpSocket->waitForDisconnected(-1));
+ − 1899
continue;
+ − 1900
}
+ − 1901
QCOMPARE(tcpSocket->localAddress(), QHostAddress(QHostAddress::LocalHost));
+ − 1902
}
+ − 1903
delete tcpSocket;
+ − 1904
}
+ − 1905
+ − 1906
//----------------------------------------------------------------------------------
+ − 1907
void tst_QTcpSocket::zeroAndMinusOneReturns()
+ − 1908
{
+ − 1909
QTcpSocket *socket = newSocket();
+ − 1910
socket->connectToHost(QtNetworkSettings::serverName(), 80);
+ − 1911
socket->write("GET / HTTP/1.0\r\nConnection: keep-alive\r\n\r\n");
+ − 1912
QVERIFY(socket->waitForReadyRead(15000));
+ − 1913
+ − 1914
char c[16];
+ − 1915
QVERIFY(socket->getChar(c));
+ − 1916
QCOMPARE(socket->read(c, 16), qint64(16));
+ − 1917
QVERIFY(socket->readLine(c, 16) > 0);
+ − 1918
QVERIFY(!socket->readAll().isEmpty());
+ − 1919
+ − 1920
// the last operation emptied the read buffer
+ − 1921
// all read operations from this point on should fail
+ − 1922
// with return 0 because the socket is still open
+ − 1923
QVERIFY(socket->readAll().isEmpty());
+ − 1924
QCOMPARE(socket->read(c, 16), qint64(0));
+ − 1925
QCOMPARE(socket->readLine(c, 16), qint64(0));
+ − 1926
QVERIFY(!socket->getChar(c));
+ − 1927
+ − 1928
socket->write("GET / HTTP/1.0\r\n\r\n");
+ − 1929
QVERIFY(socket->waitForDisconnected(15000));
+ − 1930
QCOMPARE(socket->error(), QAbstractSocket::RemoteHostClosedError);
+ − 1931
+ − 1932
QCOMPARE(socket->write("BLUBBER"), qint64(-1));
+ − 1933
QVERIFY(socket->getChar(c));
+ − 1934
QCOMPARE(socket->read(c, 16), qint64(16));
+ − 1935
QVERIFY(socket->readLine(c, 16) > 0);
+ − 1936
QVERIFY(!socket->readAll().isEmpty());
+ − 1937
+ − 1938
// the last operation emptied the read buffer
+ − 1939
// all read operations from this point on should fail
+ − 1940
// with return -1 because the socket is not connected
+ − 1941
QVERIFY(socket->readAll().isEmpty());
+ − 1942
QCOMPARE(socket->read(c, 16), qint64(-1));
+ − 1943
QCOMPARE(socket->readLine(c, 16), qint64(-1));
+ − 1944
QVERIFY(!socket->getChar(c));
+ − 1945
QVERIFY(!socket->putChar('a'));
+ − 1946
+ − 1947
socket->close();
+ − 1948
+ − 1949
// now the QIODevice is closed, which means getChar complains
+ − 1950
QCOMPARE(socket->write("BLUBBER"), qint64(-1));
+ − 1951
QCOMPARE(socket->read(c, 16), qint64(-1));
+ − 1952
QCOMPARE(socket->readLine(c, 16), qint64(-1));
+ − 1953
QVERIFY(!socket->getChar(c));
+ − 1954
QVERIFY(!socket->putChar('a'));
+ − 1955
+ − 1956
delete socket;
+ − 1957
}
+ − 1958
+ − 1959
//----------------------------------------------------------------------------------
+ − 1960
void tst_QTcpSocket::connectionRefused()
+ − 1961
{
+ − 1962
qRegisterMetaType<QAbstractSocket::SocketError>("QAbstractSocket::SocketError");
+ − 1963
qRegisterMetaType<QAbstractSocket::SocketState>("QAbstractSocket::SocketState");
+ − 1964
+ − 1965
QTcpSocket *socket = newSocket();
+ − 1966
QSignalSpy stateSpy(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)));
+ − 1967
QSignalSpy errorSpy(socket, SIGNAL(error(QAbstractSocket::SocketError)));
+ − 1968
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
+ − 1969
&QTestEventLoop::instance(), SLOT(exitLoop()));
+ − 1970
+ − 1971
socket->connectToHost(QtNetworkSettings::serverName(), 144);
+ − 1972
+ − 1973
enterLoop(10);
+ − 1974
disconnect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
+ − 1975
&QTestEventLoop::instance(), SLOT(exitLoop()));
+ − 1976
QVERIFY2(!timeout(), "Network timeout");
+ − 1977
+ − 1978
QCOMPARE(socket->state(), QAbstractSocket::UnconnectedState);
+ − 1979
QCOMPARE(socket->error(), QAbstractSocket::ConnectionRefusedError);
+ − 1980
+ − 1981
QCOMPARE(stateSpy.count(), 3);
+ − 1982
QCOMPARE(qVariantValue<QAbstractSocket::SocketState>(stateSpy.at(0).at(0)), QAbstractSocket::HostLookupState);
+ − 1983
QCOMPARE(qVariantValue<QAbstractSocket::SocketState>(stateSpy.at(1).at(0)), QAbstractSocket::ConnectingState);
+ − 1984
QCOMPARE(qVariantValue<QAbstractSocket::SocketState>(stateSpy.at(2).at(0)), QAbstractSocket::UnconnectedState);
+ − 1985
QCOMPARE(errorSpy.count(), 1);
+ − 1986
+ − 1987
delete socket;
+ − 1988
}
+ − 1989
+ − 1990
//----------------------------------------------------------------------------------
+ − 1991
void tst_QTcpSocket::suddenRemoteDisconnect_data()
+ − 1992
{
+ − 1993
QTest::addColumn<QString>("client");
+ − 1994
QTest::addColumn<QString>("server");
+ − 1995
+ − 1996
#ifdef QT3_SUPPORT
+ − 1997
QTest::newRow("Qt3 Client <-> Qt3 Server") << QString::fromLatin1("qt3client") << QString::fromLatin1("qt3server");
+ − 1998
QTest::newRow("Qt3 Client <-> Qt4 Server") << QString::fromLatin1("qt3client") << QString::fromLatin1("qt4server");
+ − 1999
QTest::newRow("Qt4 Client <-> Qt3 Server") << QString::fromLatin1("qt4client") << QString::fromLatin1("qt3server");
+ − 2000
#endif
+ − 2001
+ − 2002
QTest::newRow("Qt4 Client <-> Qt4 Server") << QString::fromLatin1("qt4client") << QString::fromLatin1("qt4server");
+ − 2003
}
+ − 2004
+ − 2005
void tst_QTcpSocket::suddenRemoteDisconnect()
+ − 2006
{
+ − 2007
#if defined(Q_OS_WINCE) || defined(Q_OS_VXWORKS)
+ − 2008
QSKIP("stressTest subprocess needs Qt3Support", SkipAll);
+ − 2009
#elif defined( Q_OS_SYMBIAN )
+ − 2010
QSKIP("Symbian: QProcess IO is not yet supported, fix when supported", SkipAll);
+ − 2011
#else
+ − 2012
QFETCH(QString, client);
+ − 2013
QFETCH(QString, server);
+ − 2014
+ − 2015
QFETCH_GLOBAL(bool, setProxy);
+ − 2016
if (setProxy)
+ − 2017
return;
+ − 2018
QFETCH_GLOBAL(bool, ssl);
+ − 2019
if (ssl)
+ − 2020
return;
+ − 2021
+ − 2022
// Start server
+ − 2023
QProcess serverProcess;
+ − 2024
serverProcess.setReadChannel(QProcess::StandardError);
+ − 2025
serverProcess.start(QString::fromLatin1("stressTest/stressTest %1").arg(server),
+ − 2026
QIODevice::ReadWrite | QIODevice::Text);
+ − 2027
while (!serverProcess.canReadLine())
+ − 2028
QVERIFY(serverProcess.waitForReadyRead(10000));
+ − 2029
QCOMPARE(serverProcess.readLine().data(), (server.toLatin1() + "\n").data());
+ − 2030
+ − 2031
// Start client
+ − 2032
QProcess clientProcess;
+ − 2033
clientProcess.setReadChannel(QProcess::StandardError);
+ − 2034
clientProcess.start(QString::fromLatin1("stressTest/stressTest %1").arg(client),
+ − 2035
QIODevice::ReadWrite | QIODevice::Text);
+ − 2036
while (!clientProcess.canReadLine())
+ − 2037
QVERIFY(clientProcess.waitForReadyRead(10000));
+ − 2038
QCOMPARE(clientProcess.readLine().data(), (client.toLatin1() + "\n").data());
+ − 2039
+ − 2040
// Let them play for a while
+ − 2041
qDebug("Running stress test for 5 seconds");
+ − 2042
QEventLoop loop;
+ − 2043
connect(&serverProcess, SIGNAL(finished(int)), &loop, SLOT(quit()));
+ − 2044
connect(&clientProcess, SIGNAL(finished(int)), &loop, SLOT(quit()));
+ − 2045
QTime stopWatch;
+ − 2046
stopWatch.start();
+ − 2047
QTimer::singleShot(20000, &loop, SLOT(quit()));
+ − 2048
+ − 2049
while ((serverProcess.state() == QProcess::Running
+ − 2050
|| clientProcess.state() == QProcess::Running) && stopWatch.elapsed() < 20000)
+ − 2051
loop.exec();
+ − 2052
+ − 2053
QVERIFY(stopWatch.elapsed() < 20000);
+ − 2054
+ − 2055
// Check that both exited normally.
+ − 2056
QCOMPARE(clientProcess.readAll().constData(), "SUCCESS\n");
+ − 2057
QCOMPARE(serverProcess.readAll().constData(), "SUCCESS\n");
+ − 2058
#endif
+ − 2059
}
+ − 2060
+ − 2061
//----------------------------------------------------------------------------------
+ − 2062
void tst_QTcpSocket::connectToMultiIP()
+ − 2063
{
+ − 2064
QSKIP("TODO: setup DNS in the new network", SkipAll);
+ − 2065
+ − 2066
#if defined(Q_OS_VXWORKS)
+ − 2067
QSKIP("VxSim in standard config doesn't even run a DNS resolver", SkipAll);
+ − 2068
#else
+ − 2069
QFETCH_GLOBAL(bool, ssl);
+ − 2070
if (ssl)
+ − 2071
return;
+ − 2072
QFETCH_GLOBAL(bool, setProxy);
+ − 2073
if (setProxy)
+ − 2074
QSKIP("This test takes too long if we also add the proxies.", SkipSingle);
+ − 2075
+ − 2076
qDebug("Please wait, this test can take a while...");
+ − 2077
+ − 2078
QTcpSocket *socket = newSocket();
+ − 2079
// rationale: this domain resolves to 3 A-records, 2 of them are
+ − 2080
// invalid. QTcpSocket should never spend more than 30 seconds per IP, and
+ − 2081
// 30s*2 = 60s.
+ − 2082
QTime stopWatch;
+ − 2083
stopWatch.start();
+ − 2084
socket->connectToHost("multi.dev.troll.no", 80);
+ − 2085
QVERIFY(socket->waitForConnected(60500));
+ − 2086
QVERIFY(stopWatch.elapsed() < 70000);
+ − 2087
socket->abort();
+ − 2088
+ − 2089
stopWatch.restart();
+ − 2090
socket->connectToHost("multi.dev.troll.no", 81);
3
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2091
QVERIFY(!socket->waitForConnected(2000));
0
+ − 2092
QVERIFY(stopWatch.elapsed() < 2000);
+ − 2093
QCOMPARE(socket->error(), QAbstractSocket::SocketTimeoutError);
+ − 2094
+ − 2095
delete socket;
+ − 2096
#endif
+ − 2097
}
+ − 2098
+ − 2099
//----------------------------------------------------------------------------------
+ − 2100
void tst_QTcpSocket::moveToThread0()
+ − 2101
{
+ − 2102
QFETCH_GLOBAL(int, proxyType);
+ − 2103
if (proxyType & AuthMask)
+ − 2104
return;
+ − 2105
+ − 2106
{
+ − 2107
// Case 1: Moved after connecting, before waiting for connection.
+ − 2108
QTcpSocket *socket = newSocket();;
+ − 2109
socket->connectToHost(QtNetworkSettings::serverName(), 143);
+ − 2110
socket->moveToThread(0);
3
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2111
QVERIFY(socket->waitForConnected(5000));
0
+ − 2112
socket->write("XXX LOGOUT\r\n");
+ − 2113
QVERIFY(socket->waitForBytesWritten(5000));
+ − 2114
QVERIFY(socket->waitForDisconnected());
+ − 2115
delete socket;
+ − 2116
}
+ − 2117
{
+ − 2118
// Case 2: Moved before connecting
+ − 2119
QTcpSocket *socket = newSocket();
+ − 2120
socket->moveToThread(0);
+ − 2121
socket->connectToHost(QtNetworkSettings::serverName(), 143);
3
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2122
QVERIFY(socket->waitForConnected(5000));
0
+ − 2123
socket->write("XXX LOGOUT\r\n");
+ − 2124
QVERIFY(socket->waitForBytesWritten(5000));
+ − 2125
QVERIFY(socket->waitForDisconnected());
+ − 2126
delete socket;
+ − 2127
}
+ − 2128
{
+ − 2129
// Case 3: Moved after writing, while waiting for bytes to be written.
+ − 2130
QTcpSocket *socket = newSocket();
+ − 2131
socket->connectToHost(QtNetworkSettings::serverName(), 143);
3
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2132
QVERIFY(socket->waitForConnected(5000));
0
+ − 2133
socket->write("XXX LOGOUT\r\n");
+ − 2134
socket->moveToThread(0);
+ − 2135
QVERIFY(socket->waitForBytesWritten(5000));
+ − 2136
QVERIFY(socket->waitForDisconnected());
+ − 2137
delete socket;
+ − 2138
}
+ − 2139
{
+ − 2140
// Case 4: Moved after writing, while waiting for response.
+ − 2141
QTcpSocket *socket = newSocket();
+ − 2142
socket->connectToHost(QtNetworkSettings::serverName(), 143);
3
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2143
QVERIFY(socket->waitForConnected(5000));
0
+ − 2144
socket->write("XXX LOGOUT\r\n");
+ − 2145
QVERIFY(socket->waitForBytesWritten(5000));
+ − 2146
socket->moveToThread(0);
+ − 2147
QVERIFY(socket->waitForDisconnected());
+ − 2148
delete socket;
+ − 2149
}
+ − 2150
}
+ − 2151
+ − 2152
void tst_QTcpSocket::increaseReadBufferSize()
+ − 2153
{
+ − 2154
QTcpServer server;
+ − 2155
QTcpSocket *active = newSocket();
+ − 2156
connect(active, SIGNAL(readyRead()), SLOT(exitLoopSlot()));
+ − 2157
+ − 2158
// connect two sockets to each other:
+ − 2159
QVERIFY(server.listen(QHostAddress::LocalHost));
+ − 2160
active->connectToHost("127.0.0.1", server.serverPort());
+ − 2161
QVERIFY(active->waitForConnected(5000));
+ − 2162
QVERIFY(server.waitForNewConnection(5000));
+ − 2163
+ − 2164
QTcpSocket *passive = server.nextPendingConnection();
+ − 2165
QVERIFY(passive);
+ − 2166
+ − 2167
// now write 512 bytes of data on one end
+ − 2168
QByteArray data(512, 'a');
+ − 2169
passive->write(data);
+ − 2170
QVERIFY2(passive->waitForBytesWritten(5000), "Network timeout");
+ − 2171
+ − 2172
// set the read buffer size to less than what was written and iterate:
+ − 2173
active->setReadBufferSize(256);
+ − 2174
enterLoop(10);
+ − 2175
QVERIFY2(!timeout(), "Network timeout");
+ − 2176
QCOMPARE(active->bytesAvailable(), active->readBufferSize());
+ − 2177
+ − 2178
// increase the buffer size and iterate again:
+ − 2179
active->setReadBufferSize(384);
+ − 2180
enterLoop(10);
+ − 2181
QVERIFY2(!timeout(), "Network timeout");
+ − 2182
QCOMPARE(active->bytesAvailable(), active->readBufferSize());
+ − 2183
+ − 2184
// once more, but now it should read everything there was to read
+ − 2185
active->setReadBufferSize(1024);
+ − 2186
enterLoop(10);
+ − 2187
QVERIFY2(!timeout(), "Network timeout");
+ − 2188
QCOMPARE(active->bytesAvailable(), qint64(data.size()));
+ − 2189
+ − 2190
// drain it and compare
+ − 2191
QCOMPARE(active->readAll(), data);
+ − 2192
+ − 2193
// now one more test by setting the buffer size to unlimited:
+ − 2194
passive->write(data);
+ − 2195
QVERIFY2(passive->waitForBytesWritten(5000), "Network timeout");
+ − 2196
active->setReadBufferSize(256);
+ − 2197
enterLoop(10);
+ − 2198
QVERIFY2(!timeout(), "Network timeout");
+ − 2199
QCOMPARE(active->bytesAvailable(), active->readBufferSize());
+ − 2200
active->setReadBufferSize(0);
+ − 2201
enterLoop(10);
+ − 2202
QVERIFY2(!timeout(), "Network timeout");
+ − 2203
QCOMPARE(active->bytesAvailable(), qint64(data.size()));
+ − 2204
QCOMPARE(active->readAll(), data);
+ − 2205
+ − 2206
delete active;
+ − 2207
}
+ − 2208
3
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2209
void tst_QTcpSocket::taskQtBug5799ConnectionErrorWaitForConnected()
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2210
{
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2211
QFETCH_GLOBAL(bool, setProxy);
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2212
if (setProxy)
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2213
return;
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2214
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2215
// check that we get a proper error connecting to port 12346
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2216
// use waitForConnected, e.g. this should use a synchronous select() on the OS level
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2217
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2218
QTcpSocket socket;
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2219
socket.connectToHost(QtNetworkSettings::serverName(), 12346);
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2220
QTime timer;
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2221
timer.start();
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2222
socket.waitForConnected(10000);
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2223
QVERIFY2(timer.elapsed() < 9900, "Connection to closed port timed out instead of refusing, something is wrong");
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2224
QVERIFY2(socket.state() == QAbstractSocket::UnconnectedState, "Socket connected unexpectedly!");
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2225
QVERIFY2(socket.error() == QAbstractSocket::ConnectionRefusedError,
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2226
QString("Could not reach server: %1").arg(socket.errorString()).toLocal8Bit());
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2227
}
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2228
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2229
void tst_QTcpSocket::taskQtBug5799ConnectionErrorEventLoop()
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2230
{
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2231
QFETCH_GLOBAL(bool, setProxy);
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2232
if (setProxy)
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2233
return;
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2234
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2235
// check that we get a proper error connecting to port 12346
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2236
// This testcase uses an event loop
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2237
QTcpSocket socket;
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2238
connect(&socket, SIGNAL(error(QAbstractSocket::SocketError)), &QTestEventLoop::instance(), SLOT(exitLoop()));
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2239
socket.connectToHost(QtNetworkSettings::serverName(), 12346);
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2240
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2241
QTestEventLoop::instance().enterLoop(10);
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2242
QVERIFY2(!QTestEventLoop::instance().timeout(), "Connection to closed port timed out instead of refusing, something is wrong");
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2243
QVERIFY2(socket.state() == QAbstractSocket::UnconnectedState, "Socket connected unexpectedly!");
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2244
QVERIFY2(socket.error() == QAbstractSocket::ConnectionRefusedError,
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2245
QString("Could not reach server: %1").arg(socket.errorString()).toLocal8Bit());
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2246
}
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2247
0
+ − 2248
void tst_QTcpSocket::invalidProxy_data()
+ − 2249
{
+ − 2250
QTest::addColumn<int>("type");
+ − 2251
QTest::addColumn<QString>("host");
+ − 2252
QTest::addColumn<int>("port");
+ − 2253
QTest::addColumn<bool>("failsAtConnect");
+ − 2254
QTest::addColumn<int>("expectedError");
+ − 2255
+ − 2256
QString fluke = QHostInfo::fromName(QtNetworkSettings::serverName()).addresses().first().toString();
+ − 2257
QTest::newRow("ftp-proxy") << int(QNetworkProxy::FtpCachingProxy) << fluke << 21 << true
+ − 2258
<< int(QAbstractSocket::UnsupportedSocketOperationError);
+ − 2259
QTest::newRow("http-caching-proxy") << int(QNetworkProxy::HttpCachingProxy) << fluke << 3128 << true
+ − 2260
<< int(QAbstractSocket::UnsupportedSocketOperationError);
+ − 2261
QTest::newRow("no-such-host-socks5") << int(QNetworkProxy::Socks5Proxy)
+ − 2262
<< "this-host-will-never-exist.troll.no" << 1080 << false
+ − 2263
<< int(QAbstractSocket::ProxyNotFoundError);
+ − 2264
QTest::newRow("no-such-host-http") << int(QNetworkProxy::HttpProxy)
+ − 2265
<< "this-host-will-never-exist.troll.no" << 3128 << false
+ − 2266
<< int(QAbstractSocket::ProxyNotFoundError);
+ − 2267
#if !defined(Q_OS_SYMBIAN)
+ − 2268
QTest::newRow("http-on-socks5") << int(QNetworkProxy::HttpProxy) << fluke << 1080 << false
+ − 2269
<< int(QAbstractSocket::ProxyConnectionClosedError);
+ − 2270
QTest::newRow("socks5-on-http") << int(QNetworkProxy::Socks5Proxy) << fluke << 3128 << false
+ − 2271
<< int(QAbstractSocket::SocketTimeoutError);
+ − 2272
#endif
+ − 2273
}
+ − 2274
+ − 2275
void tst_QTcpSocket::invalidProxy()
+ − 2276
{
+ − 2277
QFETCH_GLOBAL(bool, setProxy);
+ − 2278
if (setProxy)
+ − 2279
return;
+ − 2280
+ − 2281
QFETCH(int, type);
+ − 2282
QFETCH(QString, host);
+ − 2283
QFETCH(int, port);
+ − 2284
QFETCH(bool, failsAtConnect);
+ − 2285
QNetworkProxy::ProxyType proxyType = QNetworkProxy::ProxyType(type);
+ − 2286
QNetworkProxy proxy(proxyType, host, port);
+ − 2287
+ − 2288
QTcpSocket *socket = newSocket();
+ − 2289
socket->setProxy(proxy);
+ − 2290
socket->connectToHost(QHostInfo::fromName(QtNetworkSettings::serverName()).addresses().first().toString(), 80);
+ − 2291
+ − 2292
if (failsAtConnect) {
+ − 2293
QCOMPARE(socket->state(), QAbstractSocket::UnconnectedState);
+ − 2294
} else {
+ − 2295
QCOMPARE(socket->state(), QAbstractSocket::ConnectingState);
3
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2296
QVERIFY(!socket->waitForConnected(5000));
0
+ − 2297
}
+ − 2298
QVERIFY(!socket->errorString().isEmpty());
+ − 2299
+ − 2300
// note: the following test is not a hard failure.
+ − 2301
// Sometimes, error codes change for the better
+ − 2302
QTEST(int(socket->error()), "expectedError");
+ − 2303
+ − 2304
delete socket;
+ − 2305
}
+ − 2306
+ − 2307
// copied from tst_qnetworkreply.cpp
+ − 2308
class MyProxyFactory: public QNetworkProxyFactory
+ − 2309
{
+ − 2310
public:
+ − 2311
int callCount;
+ − 2312
QList<QNetworkProxy> toReturn;
+ − 2313
QNetworkProxyQuery lastQuery;
+ − 2314
inline MyProxyFactory() { clear(); }
+ − 2315
+ − 2316
inline void clear()
+ − 2317
{
+ − 2318
callCount = 0;
+ − 2319
toReturn = QList<QNetworkProxy>() << QNetworkProxy::DefaultProxy;
+ − 2320
lastQuery = QNetworkProxyQuery();
+ − 2321
}
+ − 2322
+ − 2323
virtual QList<QNetworkProxy> queryProxy(const QNetworkProxyQuery &query)
+ − 2324
{
+ − 2325
lastQuery = query;
+ − 2326
++callCount;
+ − 2327
return toReturn;
+ − 2328
}
+ − 2329
};
+ − 2330
+ − 2331
void tst_QTcpSocket::proxyFactory_data()
+ − 2332
{
+ − 2333
QTest::addColumn<QList<QNetworkProxy> >("proxyList");
+ − 2334
QTest::addColumn<QNetworkProxy>("proxyUsed");
+ − 2335
QTest::addColumn<bool>("failsAtConnect");
+ − 2336
QTest::addColumn<int>("expectedError");
+ − 2337
+ − 2338
QList<QNetworkProxy> proxyList;
+ − 2339
+ − 2340
// tests that do connect
+ − 2341
+ − 2342
proxyList << QNetworkProxy(QNetworkProxy::HttpProxy, QtNetworkSettings::serverName(), 3129);
+ − 2343
QTest::newRow("http")
+ − 2344
<< proxyList << proxyList.at(0)
+ − 2345
<< false << int(QAbstractSocket::UnknownSocketError);
+ − 2346
+ − 2347
proxyList.clear();
+ − 2348
proxyList << QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1081);
+ − 2349
QTest::newRow("socks5")
+ − 2350
<< proxyList << proxyList.at(0)
+ − 2351
<< false << int(QAbstractSocket::UnknownSocketError);
+ − 2352
+ − 2353
proxyList.clear();
+ − 2354
proxyList << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129)
+ − 2355
<< QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1081);
+ − 2356
QTest::newRow("cachinghttp+socks5")
+ − 2357
<< proxyList << proxyList.at(1)
+ − 2358
<< false << int(QAbstractSocket::UnknownSocketError);
+ − 2359
+ − 2360
proxyList.clear();
+ − 2361
proxyList << QNetworkProxy(QNetworkProxy::FtpCachingProxy, QtNetworkSettings::serverName(), 2121)
+ − 2362
<< QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129)
+ − 2363
<< QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1081);
+ − 2364
QTest::newRow("ftp+cachinghttp+socks5")
+ − 2365
<< proxyList << proxyList.at(2)
+ − 2366
<< false << int(QAbstractSocket::UnknownSocketError);
+ − 2367
+ − 2368
// tests that fail to connect
+ − 2369
proxyList.clear();
+ − 2370
proxyList << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129);
+ − 2371
QTest::newRow("cachinghttp")
+ − 2372
<< proxyList << QNetworkProxy()
+ − 2373
<< true << int(QAbstractSocket::UnsupportedSocketOperationError);
+ − 2374
+ − 2375
proxyList.clear();
+ − 2376
proxyList << QNetworkProxy(QNetworkProxy::FtpCachingProxy, QtNetworkSettings::serverName(), 2121);
+ − 2377
QTest::newRow("ftp")
+ − 2378
<< proxyList << QNetworkProxy()
+ − 2379
<< true << int(QAbstractSocket::UnsupportedSocketOperationError);
+ − 2380
+ − 2381
proxyList.clear();
+ − 2382
proxyList << QNetworkProxy(QNetworkProxy::FtpCachingProxy, QtNetworkSettings::serverName(), 2121)
+ − 2383
<< QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129);
+ − 2384
QTest::newRow("ftp+cachinghttp")
+ − 2385
<< proxyList << QNetworkProxy()
+ − 2386
<< true << int(QAbstractSocket::UnsupportedSocketOperationError);
+ − 2387
}
+ − 2388
+ − 2389
void tst_QTcpSocket::proxyFactory()
+ − 2390
{
+ − 2391
QFETCH_GLOBAL(bool, setProxy);
+ − 2392
if (setProxy)
+ − 2393
return;
+ − 2394
+ − 2395
QFETCH(QList<QNetworkProxy>, proxyList);
+ − 2396
QFETCH(QNetworkProxy, proxyUsed);
+ − 2397
QFETCH(bool, failsAtConnect);
+ − 2398
+ − 2399
MyProxyFactory *factory = new MyProxyFactory;
+ − 2400
factory->toReturn = proxyList;
+ − 2401
QNetworkProxyFactory::setApplicationProxyFactory(factory);
+ − 2402
+ − 2403
QTcpSocket *socket = newSocket();
+ − 2404
QString host = QtNetworkSettings::serverName();
+ − 2405
socket->connectToHost(host, 80);
+ − 2406
+ − 2407
// Verify that the factory was called properly
+ − 2408
QCOMPARE(factory->callCount, 1);
+ − 2409
QCOMPARE(factory->lastQuery, QNetworkProxyQuery(host, 80));
+ − 2410
+ − 2411
if (failsAtConnect) {
+ − 2412
QCOMPARE(socket->state(), QAbstractSocket::UnconnectedState);
+ − 2413
} else {
+ − 2414
QCOMPARE(socket->state(), QAbstractSocket::ConnectingState);
3
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
+ − 2415
QVERIFY(socket->waitForConnected(5000));
0
+ − 2416
QCOMPARE(proxyAuthCalled, 1);
+ − 2417
}
+ − 2418
QVERIFY(!socket->errorString().isEmpty());
+ − 2419
+ − 2420
// note: the following test is not a hard failure.
+ − 2421
// Sometimes, error codes change for the better
+ − 2422
QTEST(int(socket->error()), "expectedError");
+ − 2423
+ − 2424
delete socket;
+ − 2425
}
+ − 2426
+ − 2427
+ − 2428
QTEST_MAIN(tst_QTcpSocket)
+ − 2429
#include "tst_qtcpsocket.moc"