summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/kernel/qsocketnotifier/tst_qsocketnotifier.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/corelib/kernel/qsocketnotifier/tst_qsocketnotifier.cpp')
-rw-r--r--tests/auto/corelib/kernel/qsocketnotifier/tst_qsocketnotifier.cpp194
1 files changed, 130 insertions, 64 deletions
diff --git a/tests/auto/corelib/kernel/qsocketnotifier/tst_qsocketnotifier.cpp b/tests/auto/corelib/kernel/qsocketnotifier/tst_qsocketnotifier.cpp
index e3f45df27d..17bba73dab 100644
--- a/tests/auto/corelib/kernel/qsocketnotifier/tst_qsocketnotifier.cpp
+++ b/tests/auto/corelib/kernel/qsocketnotifier/tst_qsocketnotifier.cpp
@@ -1,30 +1,5 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the test suite of the Qt Toolkit.
-**
-** $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 https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** 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$
-**
-****************************************************************************/
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QtTest/QTest>
#include <QtTest/QSignalSpy>
@@ -36,11 +11,7 @@
#include <QtNetwork/QTcpServer>
#include <QtNetwork/QTcpSocket>
#include <QtNetwork/QUdpSocket>
-#ifndef Q_OS_WINRT
#include <private/qnativesocketengine_p.h>
-#else
-#include <private/qnativesocketengine_winrt_p.h>
-#endif
#define NATIVESOCKETENGINE QNativeSocketEngine
#ifdef Q_OS_UNIX
#include <private/qnet_unix_p.h>
@@ -53,16 +24,22 @@
# undef min
#endif // Q_CC_MSVC
+using namespace std::chrono_literals;
+
class tst_QSocketNotifier : public QObject
{
Q_OBJECT
private slots:
+ void constructing();
void unexpectedDisconnection();
void mixingWithTimers();
#ifdef Q_OS_UNIX
void posixSockets();
#endif
void asyncMultipleDatagram();
+ void activationReason_data();
+ void activationReason();
+ void legacyConnect();
protected slots:
void async_readDatagramSlot();
@@ -85,6 +62,51 @@ static QHostAddress makeNonAny(const QHostAddress &address,
return address;
}
+void tst_QSocketNotifier::constructing()
+{
+ const qintptr fd = 15;
+
+ // Test constructing with no descriptor assigned.
+ {
+ QSocketNotifier notifier(QSocketNotifier::Read);
+
+ QVERIFY(!notifier.isValid());
+ QCOMPARE(notifier.socket(), Q_INT64_C(-1));
+ QCOMPARE(notifier.type(), QSocketNotifier::Read);
+ QVERIFY(!notifier.isEnabled());
+
+ notifier.setEnabled(true);
+ QVERIFY(!notifier.isEnabled());
+
+ notifier.setSocket(fd);
+ QVERIFY(notifier.isValid());
+ QCOMPARE(notifier.socket(), fd);
+ QVERIFY(!notifier.isEnabled());
+ notifier.setEnabled(true);
+ QVERIFY(notifier.isEnabled());
+ }
+
+ // Test constructing with the notifications enabled by default.
+ {
+ QSocketNotifier notifier(fd, QSocketNotifier::Write);
+
+ QVERIFY(notifier.isValid());
+ QCOMPARE(notifier.socket(), fd);
+ QCOMPARE(notifier.type(), QSocketNotifier::Write);
+ QVERIFY(notifier.isEnabled());
+
+ notifier.setSocket(fd);
+ QVERIFY(!notifier.isEnabled());
+
+ notifier.setEnabled(true);
+ QVERIFY(notifier.isEnabled());
+ notifier.setSocket(-1);
+ QVERIFY(!notifier.isValid());
+ QCOMPARE(notifier.socket(), Q_INT64_C(-1));
+ QVERIFY(!notifier.isEnabled());
+ }
+}
+
class UnexpectedDisconnectTester : public QObject
{
Q_OBJECT
@@ -97,10 +119,10 @@ public:
{
QSocketNotifier *notifier1 =
new QSocketNotifier(readEnd1->socketDescriptor(), QSocketNotifier::Read, this);
- connect(notifier1, SIGNAL(activated(int)), SLOT(handleActivated()));
+ connect(notifier1, SIGNAL(activated(QSocketDescriptor)), SLOT(handleActivated()));
QSocketNotifier *notifier2 =
new QSocketNotifier(readEnd2->socketDescriptor(), QSocketNotifier::Read, this);
- connect(notifier2, SIGNAL(activated(int)), SLOT(handleActivated()));
+ connect(notifier2, SIGNAL(activated(QSocketDescriptor)), SLOT(handleActivated()));
}
public slots:
@@ -110,11 +132,11 @@ public slots:
++sequence;
if (sequence == 1) {
// read from both ends
- (void) readEnd1->read(data1, sizeof(data1));
- (void) readEnd2->read(data2, sizeof(data2));
+ QCOMPARE(readEnd1->read(data1, sizeof(data1)), 1);
+ QCOMPARE(readEnd2->read(data2, sizeof(data2)), 1);
emit finished();
} else if (sequence == 2) {
- // we should never get here
+ // check that we can't read now because we've read our byte
QCOMPARE(readEnd2->read(data2, sizeof(data2)), qint64(-2));
QVERIFY(readEnd2->isValid());
}
@@ -126,15 +148,11 @@ signals:
void tst_QSocketNotifier::unexpectedDisconnection()
{
-#ifdef Q_OS_WINRT
- // WinRT does not allow a connection to the localhost
- QSKIP("Local connection not allowed", SkipAll);
-#else
/*
Given two sockets and two QSocketNotifiers registered on each
their socket. If both sockets receive data, and the first slot
invoked by one of the socket notifiers empties both sockets, the
- other notifier will also emit activated(). This results in
+ other notifier will also emit activated(). This was causing an
unexpected disconnection in QAbstractSocket.
The use case is that somebody calls one of the
@@ -151,7 +169,7 @@ void tst_QSocketNotifier::unexpectedDisconnection()
readEnd1.connectToHost(server.serverAddress(), server.serverPort());
QVERIFY(readEnd1.waitForWrite());
QCOMPARE(readEnd1.state(), QAbstractSocket::ConnectedState);
- QVERIFY(server.waitForNewConnection());
+ QVERIFY(server.waitForNewConnection(5000));
QTcpSocket *writeEnd1 = server.nextPendingConnection();
QVERIFY(writeEnd1 != 0);
@@ -160,7 +178,7 @@ void tst_QSocketNotifier::unexpectedDisconnection()
readEnd2.connectToHost(server.serverAddress(), server.serverPort());
QVERIFY(readEnd2.waitForWrite());
QCOMPARE(readEnd2.state(), QAbstractSocket::ConnectedState);
- QVERIFY(server.waitForNewConnection());
+ QVERIFY(server.waitForNewConnection(5000));
QTcpSocket *writeEnd2 = server.nextPendingConnection();
QVERIFY(writeEnd2 != 0);
@@ -170,8 +188,9 @@ void tst_QSocketNotifier::unexpectedDisconnection()
writeEnd1->waitForBytesWritten();
writeEnd2->waitForBytesWritten();
- writeEnd1->flush();
- writeEnd2->flush();
+ // ensure both read ends are ready for reading, before the event loop
+ QVERIFY(readEnd1.waitForRead(5s));
+ QVERIFY(readEnd2.waitForRead(5s));
UnexpectedDisconnectTester tester(&readEnd1, &readEnd2);
@@ -195,7 +214,6 @@ void tst_QSocketNotifier::unexpectedDisconnection()
writeEnd1->close();
writeEnd2->close();
server.close();
-#endif // !Q_OS_WINRT
}
class MixingWithTimersHelper : public QObject
@@ -234,9 +252,6 @@ void MixingWithTimersHelper::socketFired()
void tst_QSocketNotifier::mixingWithTimers()
{
-#ifdef Q_OS_WINRT
- QSKIP("WinRT does not allow connection to localhost", SkipAll);
-#else
QTimer timer;
timer.setInterval(0);
timer.start();
@@ -261,7 +276,6 @@ void tst_QSocketNotifier::mixingWithTimers()
QCOMPARE(helper.timerActivated, true);
QTRY_COMPARE(helper.socketActivated, true);
-#endif // !Q_OS_WINRT
}
#ifdef Q_OS_UNIX
@@ -284,12 +298,12 @@ void tst_QSocketNotifier::posixSockets()
{
QSocketNotifier rn(posixSocket, QSocketNotifier::Read);
- connect(&rn, SIGNAL(activated(int)), &QTestEventLoop::instance(), SLOT(exitLoop()));
+ connect(&rn, SIGNAL(activated(QSocketDescriptor)), &QTestEventLoop::instance(), SLOT(exitLoop()));
QSignalSpy readSpy(&rn, &QSocketNotifier::activated);
QVERIFY(readSpy.isValid());
// No write notifier, some systems trigger write notification on socket creation, but not all
QSocketNotifier en(posixSocket, QSocketNotifier::Exception);
- connect(&en, SIGNAL(activated(int)), &QTestEventLoop::instance(), SLOT(exitLoop()));
+ connect(&en, SIGNAL(activated(QSocketDescriptor)), &QTestEventLoop::instance(), SLOT(exitLoop()));
QSignalSpy errorSpy(&en, &QSocketNotifier::activated);
QVERIFY(errorSpy.isValid());
@@ -297,8 +311,8 @@ void tst_QSocketNotifier::posixSockets()
passive->waitForBytesWritten(5000);
QTestEventLoop::instance().enterLoop(3);
- QCOMPARE(readSpy.count(), 1);
- QCOMPARE(errorSpy.count(), 0);
+ QCOMPARE(readSpy.size(), 1);
+ QCOMPARE(errorSpy.size(), 0);
char buffer[100];
int r = qt_safe_read(posixSocket, buffer, 100);
@@ -306,15 +320,15 @@ void tst_QSocketNotifier::posixSockets()
QCOMPARE(buffer, "hello");
QSocketNotifier wn(posixSocket, QSocketNotifier::Write);
- connect(&wn, SIGNAL(activated(int)), &QTestEventLoop::instance(), SLOT(exitLoop()));
+ connect(&wn, SIGNAL(activated(QSocketDescriptor)), &QTestEventLoop::instance(), SLOT(exitLoop()));
QSignalSpy writeSpy(&wn, &QSocketNotifier::activated);
QVERIFY(writeSpy.isValid());
qt_safe_write(posixSocket, "goodbye", 8);
QTestEventLoop::instance().enterLoop(3);
- QCOMPARE(readSpy.count(), 1);
- QCOMPARE(writeSpy.count(), 1);
- QCOMPARE(errorSpy.count(), 0);
+ QCOMPARE(readSpy.size(), 1);
+ QCOMPARE(writeSpy.size(), 1);
+ QCOMPARE(errorSpy.size(), 0);
// Write notifier may have fired before the read notifier inside
// QTcpSocket, give QTcpSocket a chance to see the incoming data
@@ -350,9 +364,6 @@ void tst_QSocketNotifier::async_writeDatagramSlot()
void tst_QSocketNotifier::asyncMultipleDatagram()
{
-#ifdef Q_OS_WINRT
- QSKIP("WinRT does not allow connection to localhost", SkipAll);
-#else
m_asyncSender = new QUdpSocket;
m_asyncReceiver = new QUdpSocket;
@@ -365,7 +376,7 @@ void tst_QSocketNotifier::asyncMultipleDatagram()
&tst_QSocketNotifier::async_readDatagramSlot);
// activate socket notifiers
- QTestEventLoop::instance().enterLoopMSecs(100);
+ QTestEventLoop::instance().enterLoop(100ms);
m_asyncSender->writeDatagram("1", makeNonAny(m_asyncReceiver->localAddress()), port);
m_asyncSender->writeDatagram("2", makeNonAny(m_asyncReceiver->localAddress()), port);
@@ -378,12 +389,67 @@ void tst_QSocketNotifier::asyncMultipleDatagram()
QTestEventLoop::instance().enterLoop(1);
QVERIFY(!QTestEventLoop::instance().timeout());
- QCOMPARE(spy.count(), 2);
+ QCOMPARE(spy.size(), 2);
delete m_asyncSender;
delete m_asyncReceiver;
- #endif // !Q_OS_WINRT
}
+void tst_QSocketNotifier::activationReason_data()
+{
+ QTest::addColumn<QSocketNotifier::Type>("type");
+ QTest::addRow("read") << QSocketNotifier::Read;
+ QTest::addRow("write") << QSocketNotifier::Write;
+ QTest::addRow("exception") << QSocketNotifier::Exception;
+}
+void tst_QSocketNotifier::activationReason()
+{
+ QSocketDescriptor fd = 15;
+
+ QFETCH(QSocketNotifier::Type, type);
+
+ QSocketNotifier notifier(fd, type);
+ auto activation = new QEvent(QEvent::SockAct);
+ QCoreApplication::postEvent(&notifier, activation);
+
+ QSocketNotifier::Type notifierType;
+ connect(&notifier, &QSocketNotifier::activated, this,
+ [&notifierType, fd](QSocketDescriptor sockfd, QSocketNotifier::Type sntype) {
+ if (sockfd == fd)
+ notifierType = sntype;
+ else
+ qWarning() << "Got an unexpected socket file descriptor:" << qintptr(sockfd);
+ });
+
+ QCoreApplication::processEvents();
+ QCOMPARE(notifierType, type);
+}
+
+// This test ensures that we can connect QSocketNotifier::activated to a slot taking an integer
+// or qintptr.
+void tst_QSocketNotifier::legacyConnect()
+{
+ qintptr fd = 15;
+ QSocketNotifier notifier(fd, QSocketNotifier::Read);
+ auto activation = new QEvent(QEvent::SockAct);
+ QCoreApplication::postEvent(&notifier, activation);
+
+ bool receivedQIntPtr = false;
+ connect(&notifier, &QSocketNotifier::activated, this, [&receivedQIntPtr, fd](qintptr q){
+ if (q == fd)
+ receivedQIntPtr = true;
+ });
+ bool receivedInt = false;
+ connect(&notifier, &QSocketNotifier::activated, this, [&receivedInt, fd](int q){
+ if (q == fd)
+ receivedInt = true;
+ });
+
+ QCoreApplication::processEvents();
+ QVERIFY(receivedQIntPtr);
+ QVERIFY(receivedInt);
+}
+
+
QTEST_MAIN(tst_QSocketNotifier)
#include <tst_qsocketnotifier.moc>