diff options
Diffstat (limited to 'tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp')
-rw-r--r-- | tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp | 133 |
1 files changed, 109 insertions, 24 deletions
diff --git a/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp b/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp index bdfb7d2fb8..fb6b0c6e32 100644 --- a/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp +++ b/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp @@ -1,31 +1,27 @@ /**************************************************************************** ** -** Copyright (C) 2015 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ +** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2016 Intel Corporation. +** Contact: https://www.qt.io/licensing/ ** ** This file is part of the test suite of the Qt Toolkit. ** -** $QT_BEGIN_LICENSE:LGPL21$ +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. ** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** As a special exception, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** @@ -94,7 +90,6 @@ class tst_QTcpSocket : public QObject public: tst_QTcpSocket(); - virtual ~tst_QTcpSocket(); static void enterLoop(int secs) { @@ -201,6 +196,8 @@ private slots: void setSocketOption(); void clientSendDataOnDelayedDisconnect(); void serverDisconnectWithBuffered(); + void socketDiscardDataInWriteMode(); + void writeOnReadBufferOverflow(); void readNotificationsAfterBind(); protected slots: @@ -322,11 +319,6 @@ tst_QTcpSocket::tst_QTcpSocket() firstFailInfo.setAddresses(QList<QHostAddress>() << QHostAddress("224.0.0.0") << QtNetworkSettings::serverIP()); } -tst_QTcpSocket::~tst_QTcpSocket() -{ - -} - void tst_QTcpSocket::initTestCase_data() { QTest::addColumn<bool>("setProxy"); @@ -487,6 +479,8 @@ void tst_QTcpSocket::constructing() QCOMPARE(socket->localAddress(), QHostAddress()); QCOMPARE((int) socket->peerPort(), 0); QCOMPARE(socket->peerAddress(), QHostAddress()); + QCOMPARE(socket->readChannelCount(), 0); + QCOMPARE(socket->writeChannelCount(), 0); QCOMPARE(socket->error(), QTcpSocket::UnknownSocketError); QCOMPARE(socket->errorString(), QString("Unknown error")); @@ -603,6 +597,8 @@ void tst_QTcpSocket::bind() } while (randomPort && attemptsLeft); QCOMPARE(socket->state(), QAbstractSocket::BoundState); + QCOMPARE(socket->readChannelCount(), 0); + QCOMPARE(socket->writeChannelCount(), 0); boundPort = socket->localPort(); if (port) QCOMPARE(int(boundPort), port); @@ -677,6 +673,12 @@ void tst_QTcpSocket::bindThenResolveHost() const quint16 port = 80; socket->connectToHost(hostName, port); + // Additionally, initiate a delayed close before the socket connects + // to ensure that we don't lose the socket engine in HostLookupState. + // After a connection has been established, socket should send all + // the pending data and close the socket engine automatically. + QVERIFY(socket->putChar(0)); + socket->close(); QVERIFY2(socket->waitForConnected(), (hostName.toLocal8Bit() + ": " + QByteArray::number(port) + ' ' + QtNetworkSettings::msgSocketError(*socket)).constData()); @@ -740,6 +742,8 @@ void tst_QTcpSocket::setSocketDescriptor() QCOMPARE(socket->socketDescriptor(), (qintptr)sock); QVERIFY(socket->waitForConnected(10000)); QCOMPARE(socket->socketDescriptor(), (qintptr)sock); + QCOMPARE(socket->readChannelCount(), 1); + QCOMPARE(socket->writeChannelCount(), 1); delete socket; #ifdef Q_OS_WIN delete dummy; @@ -775,6 +779,8 @@ void tst_QTcpSocket::blockingIMAP() QVERIFY(socket->waitForConnected(10000)); QCOMPARE(socket->state(), QTcpSocket::ConnectedState); QVERIFY(socket->isValid()); + QCOMPARE(socket->readChannelCount(), 1); + QCOMPARE(socket->writeChannelCount(), 1); // Read greeting QVERIFY(socket->waitForReadyRead(5000)); @@ -831,6 +837,8 @@ void tst_QTcpSocket::blockingIMAP() // Check that it's closed QCOMPARE(socket->state(), QTcpSocket::UnconnectedState); + QCOMPARE(socket->readChannelCount(), 0); + QCOMPARE(socket->writeChannelCount(), 0); delete socket; } @@ -871,6 +879,8 @@ void tst_QTcpSocket::timeoutConnect() QVERIFY(!socket->waitForConnected(1000)); //200ms is too short when using SOCKS proxy authentication QCOMPARE(socket->state(), QTcpSocket::UnconnectedState); QCOMPARE(int(socket->error()), int(QTcpSocket::SocketTimeoutError)); + QCOMPARE(socket->readChannelCount(), 0); + QCOMPARE(socket->writeChannelCount(), 0); timer.start(); socket->connectToHost(address, 1357); @@ -917,6 +927,8 @@ void tst_QTcpSocket::nonBlockingIMAP() } QCOMPARE(socket->state(), QTcpSocket::ConnectedState); + QCOMPARE(socket->readChannelCount(), 1); + QCOMPARE(socket->writeChannelCount(), 1); enterLoop(30); if (timeout()) { @@ -982,6 +994,8 @@ void tst_QTcpSocket::nonBlockingIMAP() // Check that it's closed QCOMPARE(socket->state(), QTcpSocket::UnconnectedState); + QCOMPARE(socket->readChannelCount(), 0); + QCOMPARE(socket->writeChannelCount(), 0); delete socket; } @@ -1205,6 +1219,12 @@ void tst_QTcpSocket::connectDisconnectConnectDisconnect() QCOMPARE(socket->state(), QTcpSocket::UnconnectedState); QCOMPARE(socket->socketType(), QTcpSocket::TcpSocket); + QCOMPARE(socket->socketDescriptor(), qintptr(-1)); + QCOMPARE(int(socket->localPort()), 0); + QCOMPARE(socket->localAddress(), QHostAddress()); + QCOMPARE(int(socket->peerPort()), 0); + QCOMPARE(socket->peerAddress(), QHostAddress()); + socket->connectToHost(QtNetworkSettings::serverName(), 143); QVERIFY(socket->waitForReadyRead(10000)); QCOMPARE(QString::fromLatin1(socket->read(4)), QString("* OK")); @@ -1778,7 +1798,7 @@ void tst_QTcpSocket::atEnd() // Test server must use some vsFTPd 2.x.x version QVERIFY2(greeting.length() == sizeof("220 (vsFTPd 2.x.x)")-1, qPrintable(greeting)); QVERIFY2(greeting.startsWith("220 (vsFTPd 2."), qPrintable(greeting)); - QVERIFY2(greeting.endsWith(")"), qPrintable(greeting)); + QVERIFY2(greeting.endsWith(QLatin1Char(')')), qPrintable(greeting)); delete socket; } @@ -3031,6 +3051,71 @@ void tst_QTcpSocket::serverDisconnectWithBuffered() delete socket; } +// Test buffered sockets discard input when opened in WriteOnly mode +void tst_QTcpSocket::socketDiscardDataInWriteMode() +{ + QFETCH_GLOBAL(bool, setProxy); + if (setProxy) + return; + + QTcpServer tcpServer; + QTcpSocket *socket = newSocket(); + + QVERIFY(tcpServer.listen(QHostAddress::LocalHost)); + socket->connectToHost(tcpServer.serverAddress(), tcpServer.serverPort(), + QIODevice::WriteOnly); + QVERIFY(socket->waitForConnected(5000)); // ready for write + QCOMPARE(socket->state(), QAbstractSocket::ConnectedState); + + // Accept connection on server side + QVERIFY2(tcpServer.waitForNewConnection(5000), "Network timeout"); + QTcpSocket *newConnection = tcpServer.nextPendingConnection(); + // Send one char and drop link + QVERIFY(newConnection != NULL); + QVERIFY(newConnection->putChar(0)); + QVERIFY(newConnection->flush()); + delete newConnection; + + QVERIFY(socket->waitForReadyRead(5000)); // discard input + QVERIFY(socket->atEnd()); + + delete socket; +} + +// Test waitForBytesWritten() does not fail on read buffer overflow +void tst_QTcpSocket::writeOnReadBufferOverflow() +{ + QFETCH_GLOBAL(bool, setProxy); + if (setProxy) + return; + + QTcpServer tcpServer; + QTcpSocket *socket = newSocket(); + + QVERIFY(tcpServer.listen(QHostAddress::LocalHost)); + socket->setReadBufferSize(1); + socket->connectToHost(tcpServer.serverAddress(), tcpServer.serverPort()); + QVERIFY(socket->waitForConnected(5000)); + QCOMPARE(socket->state(), QAbstractSocket::ConnectedState); + + // Accept connection on server side + QVERIFY2(tcpServer.waitForNewConnection(5000), "Network timeout"); + QTcpSocket *newConnection = tcpServer.nextPendingConnection(); + QVERIFY(newConnection != nullptr); + QCOMPARE(newConnection->write("1", 2), Q_INT64_C(2)); + QVERIFY(newConnection->flush()); + + // Wait for buffer overflow + QVERIFY(socket->waitForReadyRead(5000)); + QCOMPARE(socket->bytesAvailable(), Q_INT64_C(1)); + // Write data and wait for successful send + QVERIFY(socket->putChar(0)); + QVERIFY(socket->waitForBytesWritten(5000)); + + delete newConnection; + delete socket; +} + // Test that the socket does not enable the read notifications in bind() void tst_QTcpSocket::readNotificationsAfterBind() { |