Fix QAbstractSocket::readData() behavior on buffered socket
Remove an useless check which spontaneously allow direct reads from the socket engine. Change-Id: Ia3d2a572d6f1563d613fe2f00d0d6849df259827 Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@digia.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>bb10
parent
9165815c89
commit
48a4a67e8d
|
|
@ -2420,7 +2420,7 @@ qint64 QAbstractSocket::readData(char *data, qint64 maxSize)
|
|||
return 0;
|
||||
|
||||
// This is for a buffered QTcpSocket
|
||||
if (d->isBuffered && d->buffer.isEmpty())
|
||||
if (d->isBuffered)
|
||||
// if we're still connected, return 0 indicating there may be more data in the future
|
||||
// if we're not connected, return -1 indicating EOF
|
||||
return d->state == QAbstractSocket::ConnectedState ? qint64(0) : qint64(-1);
|
||||
|
|
|
|||
|
|
@ -45,6 +45,11 @@
|
|||
#include <qcoreapplication.h>
|
||||
#include <qdebug.h>
|
||||
#include <qabstractsocket.h>
|
||||
#include <qtcpserver.h>
|
||||
#include <qtcpsocket.h>
|
||||
#ifndef QT_NO_SSL
|
||||
#include <qsslsocket.h>
|
||||
#endif
|
||||
|
||||
class tst_QAbstractSocket : public QObject
|
||||
{
|
||||
|
|
@ -55,7 +60,9 @@ public:
|
|||
virtual ~tst_QAbstractSocket();
|
||||
|
||||
private slots:
|
||||
void initTestCase();
|
||||
void getSetCheck();
|
||||
void serverDisconnectWithBuffered();
|
||||
};
|
||||
|
||||
tst_QAbstractSocket::tst_QAbstractSocket()
|
||||
|
|
@ -74,6 +81,11 @@ public:
|
|||
void setPeerPort(quint16 port) { QAbstractSocket::setPeerPort(port); }
|
||||
};
|
||||
|
||||
void tst_QAbstractSocket::initTestCase()
|
||||
{
|
||||
qRegisterMetaType<QAbstractSocket::SocketState>("QAbstractSocket::SocketState");
|
||||
}
|
||||
|
||||
// Testing get/set functions
|
||||
void tst_QAbstractSocket::getSetCheck()
|
||||
{
|
||||
|
|
@ -102,5 +114,46 @@ void tst_QAbstractSocket::getSetCheck()
|
|||
QCOMPARE(quint16(0xffff), obj1.peerPort());
|
||||
}
|
||||
|
||||
// Test buffered socket being properly closed on remote disconnect
|
||||
void tst_QAbstractSocket::serverDisconnectWithBuffered()
|
||||
{
|
||||
QTcpServer tcpServer;
|
||||
#ifndef QT_NO_SSL
|
||||
QSslSocket testSocket;
|
||||
#else
|
||||
QTcpSocket testSocket;
|
||||
#endif
|
||||
|
||||
QVERIFY(tcpServer.listen(QHostAddress::LocalHost));
|
||||
testSocket.connectToHost(tcpServer.serverAddress(), tcpServer.serverPort());
|
||||
// Accept connection on server side
|
||||
QVERIFY(tcpServer.waitForNewConnection(5000));
|
||||
QTcpSocket *newConnection = tcpServer.nextPendingConnection();
|
||||
// Send one char and drop link
|
||||
QVERIFY(newConnection != NULL);
|
||||
QVERIFY(newConnection->putChar(0));
|
||||
QVERIFY(newConnection->flush());
|
||||
delete newConnection;
|
||||
|
||||
QVERIFY(testSocket.waitForConnected(5000)); // ready for write
|
||||
QVERIFY(testSocket.state() == QAbstractSocket::ConnectedState);
|
||||
|
||||
QSignalSpy spyStateChanged(&testSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)));
|
||||
QSignalSpy spyDisconnected(&testSocket, SIGNAL(disconnected()));
|
||||
|
||||
QVERIFY(testSocket.waitForReadyRead(5000)); // have one char already in internal buffer
|
||||
char buf[128];
|
||||
QCOMPARE(testSocket.read(buf, sizeof(buf)), Q_INT64_C(1));
|
||||
if (testSocket.state() != QAbstractSocket::UnconnectedState) {
|
||||
QVERIFY(testSocket.waitForDisconnected(5000));
|
||||
QVERIFY(testSocket.state() == QAbstractSocket::UnconnectedState);
|
||||
}
|
||||
// Test signal emitting
|
||||
QVERIFY(spyDisconnected.count() == 1);
|
||||
QVERIFY(spyStateChanged.count() > 0);
|
||||
QVERIFY(qvariant_cast<QAbstractSocket::SocketState>(spyStateChanged.last().first())
|
||||
== QAbstractSocket::UnconnectedState);
|
||||
}
|
||||
|
||||
QTEST_MAIN(tst_QAbstractSocket)
|
||||
#include "tst_qabstractsocket.moc"
|
||||
|
|
|
|||
Loading…
Reference in New Issue