From 6b6955c2ffdb23c461812ffcda12a2296ef58513 Mon Sep 17 00:00:00 2001 From: Alex Trotsenko Date: Wed, 23 Dec 2015 13:10:30 +0200 Subject: QAbstractSocket: do not enable read notifications on TCP in bind() In bind+connect scenario, rejected connection can trigger a read notification while the socket is opened. But unlike UDP, reading from the socket engine or emitting a readyRead() signal is not allowed for the TCP socket in bound or connecting state. To make a bind+connect scenario work properly, disable the read notifications until a connection is established. Task-number: QTBUG-50124 Change-Id: I7b3d015b0f6021fb9ff9f83560478aa5545f41f5 Reviewed-by: Richard J. Moore --- .../network/socket/qtcpsocket/tst_qtcpsocket.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'tests/auto/network/socket') diff --git a/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp b/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp index 0ba9b6a58c..e8a942e6c4 100644 --- a/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp +++ b/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp @@ -200,6 +200,7 @@ private slots: void setSocketOption(); void clientSendDataOnDelayedDisconnect(); + void readNotificationsAfterBind(); protected slots: void nonBlockingIMAP_hostFound(); @@ -2984,5 +2985,25 @@ void tst_QTcpSocket::clientSendDataOnDelayedDisconnect() delete socket; } +// Test that the socket does not enable the read notifications in bind() +void tst_QTcpSocket::readNotificationsAfterBind() +{ + QFETCH_GLOBAL(bool, setProxy); + if (setProxy) + return; + + QAbstractSocket socket(QAbstractSocket::TcpSocket, Q_NULLPTR); + QVERIFY2(socket.bind(), "Bind error!"); + + connect(&socket, SIGNAL(error(QAbstractSocket::SocketError)), &QTestEventLoop::instance(), SLOT(exitLoop())); + QSignalSpy spyReadyRead(&socket, SIGNAL(readyRead())); + socket.connectToHost(QtNetworkSettings::serverName(), 12346); + + QTestEventLoop::instance().enterLoop(10); + QVERIFY2(!QTestEventLoop::instance().timeout(), "Connection to closed port timed out instead of refusing, something is wrong"); + QVERIFY2(socket.state() == QAbstractSocket::UnconnectedState, "Socket connected unexpectedly!"); + QCOMPARE(spyReadyRead.count(), 0); +} + QTEST_MAIN(tst_QTcpSocket) #include "tst_qtcpsocket.moc" -- cgit v1.2.3 From 294111e25a4e3639a83a87f70ed33ac0b3985f33 Mon Sep 17 00:00:00 2001 From: Alex Trotsenko Date: Sat, 28 Nov 2015 15:27:43 +0200 Subject: QHttpSocketEngine: ensure pending EOF triggers a notification When the remote peer closed the connection, a read notification needs to always be emitted, otherwise the higher layer does not get the disconnected signal. From the other side, underlying QAbstractSocket object could temporarily disable notifications from the engine at any time. To avoid possible blocking of the socket, take a pending EOF into account when the read notifications are re-enabled. Change-Id: Iac9d4e2f790530be3500baf5a2000f1f63df5cc2 Reviewed-by: Ulf Hermann --- .../qhttpsocketengine/tst_qhttpsocketengine.cpp | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'tests/auto/network/socket') diff --git a/tests/auto/network/socket/qhttpsocketengine/tst_qhttpsocketengine.cpp b/tests/auto/network/socket/qhttpsocketengine/tst_qhttpsocketengine.cpp index 179cdb76bc..f6662b6712 100644 --- a/tests/auto/network/socket/qhttpsocketengine/tst_qhttpsocketengine.cpp +++ b/tests/auto/network/socket/qhttpsocketengine/tst_qhttpsocketengine.cpp @@ -72,6 +72,7 @@ private slots: void downloadBigFile(); // void tcpLoopbackPerformance(); void passwordAuth(); + void ensureEofTriggersNotification(); protected slots: void tcpSocketNonBlocking_hostFound(); @@ -739,5 +740,51 @@ void tst_QHttpSocketEngine::passwordAuth() //---------------------------------------------------------------------------------- +void tst_QHttpSocketEngine::ensureEofTriggersNotification() +{ + QList serverData; + // Set the handshake and server response data + serverData << "HTTP/1.0 200 Connection established\r\n\r\n" << "0"; + MiniHttpServer server(serverData); + + QTcpSocket socket; + connect(&socket, SIGNAL(connected()), SLOT(exitLoopSlot())); + socket.setProxy(QNetworkProxy(QNetworkProxy::HttpProxy, server.serverAddress().toString(), + server.serverPort())); + socket.connectToHost("0.1.2.3", 12345); + + QTestEventLoop::instance().enterLoop(5); + if (QTestEventLoop::instance().timeout()) + QFAIL("Connect timed out"); + + QCOMPARE(socket.state(), QTcpSocket::ConnectedState); + // Disable read notification on server response + socket.setReadBufferSize(1); + socket.putChar(0); + + // Wait for the response + connect(&socket, SIGNAL(readyRead()), SLOT(exitLoopSlot())); + QTestEventLoop::instance().enterLoop(5); + if (QTestEventLoop::instance().timeout()) + QFAIL("Read timed out"); + + QCOMPARE(socket.state(), QTcpSocket::ConnectedState); + QCOMPARE(socket.bytesAvailable(), 1); + // Trigger a read notification + socket.readAll(); + // Check for pending EOF at input + QCOMPARE(socket.bytesAvailable(), 0); + QCOMPARE(socket.state(), QTcpSocket::ConnectedState); + + // Try to read EOF + connect(&socket, SIGNAL(disconnected()), SLOT(exitLoopSlot())); + QTestEventLoop::instance().enterLoop(5); + if (QTestEventLoop::instance().timeout()) + QFAIL("Disconnect timed out"); + + // Check that it's closed + QCOMPARE(socket.state(), QTcpSocket::UnconnectedState); +} + QTEST_MAIN(tst_QHttpSocketEngine) #include "tst_qhttpsocketengine.moc" -- cgit v1.2.3