diff options
Diffstat (limited to 'tests/auto/network/socket')
16 files changed, 601 insertions, 128 deletions
diff --git a/tests/auto/network/socket/platformsocketengine/platformsocketengine.pri b/tests/auto/network/socket/platformsocketengine/platformsocketengine.pri index a3b4e89450..6c7857ea1d 100644 --- a/tests/auto/network/socket/platformsocketengine/platformsocketengine.pri +++ b/tests/auto/network/socket/platformsocketengine/platformsocketengine.pri @@ -4,13 +4,7 @@ QNETWORK_SRC = $$QT_SOURCE_TREE/src/network INCLUDEPATH += $$QNETWORK_SRC -win32 { - wince { - LIBS += -lws2 - } else { - LIBS += -lws2_32 - } -} +win32:LIBS += -lws2_32 unix:contains(QT_CONFIG, reduce_exports) { SOURCES += $$QNETWORK_SRC/socket/qnativesocketengine_unix.cpp diff --git a/tests/auto/network/socket/platformsocketengine/tst_platformsocketengine.cpp b/tests/auto/network/socket/platformsocketengine/tst_platformsocketengine.cpp index 041a0ce16c..bc9d3cc9bf 100644 --- a/tests/auto/network/socket/platformsocketengine/tst_platformsocketengine.cpp +++ b/tests/auto/network/socket/platformsocketengine/tst_platformsocketengine.cpp @@ -494,9 +494,6 @@ void tst_PlatformSocketEngine::readWriteBufferSize() qint64 bufferSize = device.receiveBufferSize(); QVERIFY(bufferSize != -1); device.setReceiveBufferSize(bufferSize + 1); -#if defined(Q_OS_WINCE) - QEXPECT_FAIL(0, "Not supported by default on WinCE", Continue); -#endif QVERIFY(device.receiveBufferSize() > bufferSize); bufferSize = device.sendBufferSize(); @@ -606,8 +603,8 @@ void tst_PlatformSocketEngine::invalidSend() PLATFORMSOCKETENGINE socket; QVERIFY(socket.initialize(QAbstractSocket::TcpSocket)); - QTest::ignoreMessage(QtWarningMsg, PLATFORMSOCKETENGINESTRING "::writeDatagram() was" - " called by a socket other than QAbstractSocket::UdpSocket"); + QTest::ignoreMessage(QtWarningMsg, PLATFORMSOCKETENGINESTRING "::writeDatagram() was called" + " not in QAbstractSocket::BoundState or QAbstractSocket::ConnectedState"); QCOMPARE(socket.writeDatagram("hei", 3, QIpPacketHeader(QHostAddress::LocalHost, 143)), (qlonglong) -1); } @@ -649,7 +646,7 @@ void tst_PlatformSocketEngine::receiveUrgentData() QByteArray response; // Native OOB data test doesn't work on HP-UX or WinCE -#if !defined(Q_OS_HPUX) && !defined(Q_OS_WINCE) +#if !defined(Q_OS_HPUX) // The server sends an urgent message msg = 'Q'; QCOMPARE(int(::send(socketDescriptor, &msg, sizeof(msg), MSG_OOB)), 1); diff --git a/tests/auto/network/socket/qhttpsocketengine/tst_qhttpsocketengine.cpp b/tests/auto/network/socket/qhttpsocketengine/tst_qhttpsocketengine.cpp index 7237542e5c..68f3ea059b 100644 --- a/tests/auto/network/socket/qhttpsocketengine/tst_qhttpsocketengine.cpp +++ b/tests/auto/network/socket/qhttpsocketengine/tst_qhttpsocketengine.cpp @@ -626,11 +626,7 @@ void tst_QHttpSocketEngine::downloadBigFile() QTime stopWatch; stopWatch.start(); -#if defined(Q_OS_WINCE) - QTestEventLoop::instance().enterLoop(240); -#else QTestEventLoop::instance().enterLoop(60); -#endif if (QTestEventLoop::instance().timeout()) QFAIL("Network operation timed out"); diff --git a/tests/auto/network/socket/qlocalsocket/test/test.pro b/tests/auto/network/socket/qlocalsocket/test/test.pro index 6a5df7f9b6..ab9ed90b1d 100644 --- a/tests/auto/network/socket/qlocalsocket/test/test.pro +++ b/tests/auto/network/socket/qlocalsocket/test/test.pro @@ -2,13 +2,7 @@ CONFIG += testcase DEFINES += QLOCALSERVER_DEBUG DEFINES += QLOCALSOCKET_DEBUG - -wince* { - DEFINES += QT_LOCALSOCKET_TCP - DEFINES += SRCDIR=\\\"../\\\" -} else { - DEFINES += SRCDIR=\\\"$$PWD/../\\\" -} +DEFINES += SRCDIR=\\\"$$PWD/../\\\" QT = core network testlib diff --git a/tests/auto/network/socket/qlocalsocket/tst_qlocalsocket.cpp b/tests/auto/network/socket/qlocalsocket/tst_qlocalsocket.cpp index 4bea9b116c..00e01094a4 100644 --- a/tests/auto/network/socket/qlocalsocket/tst_qlocalsocket.cpp +++ b/tests/auto/network/socket/qlocalsocket/tst_qlocalsocket.cpp @@ -867,10 +867,8 @@ void tst_QLocalSocket::threadedConnection_data() QTest::newRow("1 client") << 1; QTest::newRow("2 clients") << 2; QTest::newRow("5 clients") << 5; -#ifndef Q_OS_WINCE QTest::newRow("10 clients") << 10; QTest::newRow("20 clients") << 20; -#endif } void tst_QLocalSocket::threadedConnection() diff --git a/tests/auto/network/socket/qsctpsocket/qsctpsocket.pro b/tests/auto/network/socket/qsctpsocket/qsctpsocket.pro new file mode 100644 index 0000000000..49a40ce9b5 --- /dev/null +++ b/tests/auto/network/socket/qsctpsocket/qsctpsocket.pro @@ -0,0 +1,6 @@ +CONFIG += testcase +TARGET = tst_qsctpsocket +QT = core network testlib + +SOURCES += tst_qsctpsocket.cpp + diff --git a/tests/auto/network/socket/qsctpsocket/tst_qsctpsocket.cpp b/tests/auto/network/socket/qsctpsocket/tst_qsctpsocket.cpp new file mode 100644 index 0000000000..70297f8cc4 --- /dev/null +++ b/tests/auto/network/socket/qsctpsocket/tst_qsctpsocket.cpp @@ -0,0 +1,489 @@ +/**************************************************************************** +** +** Copyright (C) 2016 Alex Trotsenko <alex1973tr@gmail.com> +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** 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. +** +** 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. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/QtTest> +#include <QDebug> +#include <QEventLoop> +#include <QByteArray> +#include <QString> +#include <QHostAddress> +#include <QHostInfo> +#include <QNetworkInterface> +#include <QTime> + +#include <QSctpSocket> +#include <QSctpServer> + +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <netinet/sctp.h> + +#define SOCKET int +#define INVALID_SOCKET -1 + +class tst_QSctpSocket : public QObject +{ + Q_OBJECT + +public: + static void enterLoop(int secs) + { + ++loopLevel; + QTestEventLoop::instance().enterLoop(secs); + --loopLevel; + } + static void exitLoop() + { + // Safe exit - if we aren't in an event loop, don't + // exit one. + if (loopLevel > 0) + QTestEventLoop::instance().exitLoop(); + } + static bool timeout() + { + return QTestEventLoop::instance().timeout(); + } + +private slots: + void constructing(); + void bind_data(); + void bind(); + void setInvalidSocketDescriptor(); + void setSocketDescriptor(); + void socketDescriptor(); + void hostNotFound(); + void connecting(); + void readAndWrite(); + void loop_data(); + void loop(); + void loopInTCPMode_data(); + void loopInTCPMode(); + void readDatagramAfterClose(); + void clientSendDataOnDelayedDisconnect(); + +protected slots: + void exitLoopSlot(); + +private: + static int loopLevel; +}; + +int tst_QSctpSocket::loopLevel = 0; + +//---------------------------------------------------------------------------------- +void tst_QSctpSocket::constructing() +{ + QSctpSocket socket; + + // Check the initial state of the QSctpSocket. + QCOMPARE(socket.state(), QAbstractSocket::UnconnectedState); + QVERIFY(socket.isSequential()); + QVERIFY(!socket.isOpen()); + QVERIFY(!socket.isValid()); + QCOMPARE(socket.socketType(), QAbstractSocket::SctpSocket); + QCOMPARE(socket.maxChannelCount(), 0); + QCOMPARE(socket.readChannelCount(), 0); + QCOMPARE(socket.writeChannelCount(), 0); + + char c; + QCOMPARE(socket.getChar(&c), false); + QCOMPARE((int) socket.bytesAvailable(), 0); + QCOMPARE(socket.canReadLine(), false); + QCOMPARE(socket.readLine(), QByteArray()); + QCOMPARE(socket.socketDescriptor(), (qintptr)-1); + QCOMPARE((int) socket.localPort(), 0); + QVERIFY(socket.localAddress() == QHostAddress()); + QCOMPARE((int) socket.peerPort(), 0); + QVERIFY(socket.peerAddress() == QHostAddress()); + QCOMPARE(socket.error(), QAbstractSocket::UnknownSocketError); + QCOMPARE(socket.errorString(), QString("Unknown error")); +} + +//---------------------------------------------------------------------------------- +void tst_QSctpSocket::bind_data() +{ + QTest::addColumn<QString>("stringAddr"); + QTest::addColumn<bool>("successExpected"); + QTest::addColumn<QString>("stringExpectedLocalAddress"); + + // iterate all interfaces, add all addresses on them as test data + QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces(); + foreach (const QNetworkInterface &interface, interfaces) { + if (!interface.isValid()) + continue; + + foreach (const QNetworkAddressEntry &entry, interface.addressEntries()) { + if (entry.ip().isInSubnet(QHostAddress::parseSubnet("fe80::/10")) + || entry.ip().isInSubnet(QHostAddress::parseSubnet("169.254/16"))) + continue; // link-local bind will fail, at least on Linux, so skip it. + + QString ip(entry.ip().toString()); + QTest::newRow(ip.toLatin1().constData()) << ip << true << ip; + } + } + + // additionally, try bind to known-bad addresses, and make sure this doesn't work + // these ranges are guaranteed to be reserved for 'documentation purposes', + // and thus, should be unused in the real world. Not that I'm assuming the + // world is full of competent administrators, or anything. + QStringList knownBad; + knownBad << "198.51.100.1"; + knownBad << "2001:0DB8::1"; + foreach (const QString &badAddress, knownBad) { + QTest::newRow(badAddress.toLatin1().constData()) << badAddress << false << QString(); + } +} + +// Testing bind function +void tst_QSctpSocket::bind() +{ + QFETCH(QString, stringAddr); + QFETCH(bool, successExpected); + QFETCH(QString, stringExpectedLocalAddress); + + QHostAddress addr(stringAddr); + QHostAddress expectedLocalAddress(stringExpectedLocalAddress); + + QSctpSocket socket; + qDebug() << "Binding " << addr; + + if (successExpected) { + QVERIFY2(socket.bind(addr), qPrintable(socket.errorString())); + } else { + QVERIFY(!socket.bind(addr)); + } + + QCOMPARE(socket.localAddress(), expectedLocalAddress); +} + +//---------------------------------------------------------------------------------- +void tst_QSctpSocket::setInvalidSocketDescriptor() +{ + QSctpSocket socket; + QCOMPARE(socket.socketDescriptor(), (qintptr)INVALID_SOCKET); + QVERIFY(!socket.setSocketDescriptor(-5, QAbstractSocket::UnconnectedState)); + QCOMPARE(socket.socketDescriptor(), (qintptr)INVALID_SOCKET); + + QCOMPARE(socket.error(), QAbstractSocket::UnsupportedSocketOperationError); +} + +//---------------------------------------------------------------------------------- +void tst_QSctpSocket::setSocketDescriptor() +{ + QSctpServer server; + + server.setMaxChannelCount(16); + QVERIFY(server.listen()); + + SOCKET sock = ::socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP); + + QVERIFY(sock != INVALID_SOCKET); + QSctpSocket socket; + QVERIFY(socket.setSocketDescriptor(sock, QAbstractSocket::UnconnectedState)); + QCOMPARE(socket.socketDescriptor(), (qintptr)sock); + QCOMPARE(socket.readChannelCount(), 0); + QCOMPARE(socket.writeChannelCount(), 0); + + socket.connectToHost(QHostAddress::LocalHost, server.serverPort()); + QVERIFY(socket.waitForConnected(3000)); + QVERIFY(server.waitForNewConnection(3000)); + + QCOMPARE(socket.readChannelCount(), server.maxChannelCount()); + QVERIFY(socket.writeChannelCount() <= server.maxChannelCount()); + + QSctpSocket *acceptedSocket = server.nextPendingDatagramConnection(); + QVERIFY(acceptedSocket); + QCOMPARE(acceptedSocket->readChannelCount(), socket.writeChannelCount()); + QCOMPARE(acceptedSocket->writeChannelCount(), socket.readChannelCount()); +} + +//---------------------------------------------------------------------------------- +void tst_QSctpSocket::socketDescriptor() +{ + QSctpSocket socket; + + QSctpServer server; + + QVERIFY(server.listen()); + + QCOMPARE(socket.socketDescriptor(), (qintptr)INVALID_SOCKET); + socket.connectToHost(QHostAddress::LocalHost, server.serverPort()); + QVERIFY(server.waitForNewConnection(3000)); + if (socket.state() != QAbstractSocket::ConnectedState) { + QVERIFY((socket.state() == QAbstractSocket::HostLookupState + && socket.socketDescriptor() == INVALID_SOCKET) + || socket.state() == QAbstractSocket::ConnectingState); + QVERIFY(socket.waitForConnected(3000)); + QCOMPARE(socket.state(), QAbstractSocket::ConnectedState); + } + QVERIFY(socket.socketDescriptor() != INVALID_SOCKET); +} + +//---------------------------------------------------------------------------------- +void tst_QSctpSocket::hostNotFound() +{ + QSctpSocket socket; + + socket.connectToHost("nosuchserver.qt-project.org", 80); + QVERIFY(!socket.waitForConnected(3000)); + QCOMPARE(socket.state(), QTcpSocket::UnconnectedState); + QCOMPARE(socket.error(), QAbstractSocket::HostNotFoundError); +} + +// Testing connect function +void tst_QSctpSocket::connecting() +{ + QSctpServer server; + + QVERIFY(server.listen()); + + QSctpSocket socket; + socket.connectToHost(QHostAddress::LocalHost, server.serverPort()); + QVERIFY(socket.waitForConnected(3000)); + + QVERIFY(server.waitForNewConnection(3000)); + QSctpSocket *acceptedSocket = server.nextPendingDatagramConnection(); + QVERIFY(acceptedSocket); + + QCOMPARE(socket.state(), QAbstractSocket::ConnectedState); + QCOMPARE(acceptedSocket->state(), QAbstractSocket::ConnectedState); + QCOMPARE(socket.readChannelCount(), acceptedSocket->readChannelCount()); + QCOMPARE(socket.writeChannelCount(),acceptedSocket->writeChannelCount()); +} + +// Testing read/write functions +void tst_QSctpSocket::readAndWrite() +{ + QSctpServer server; + + QVERIFY(server.listen()); + + QSctpSocket socket; + socket.connectToHost(QHostAddress::LocalHost, server.serverPort()); + QVERIFY(socket.waitForConnected(3000)); + + QVERIFY(server.waitForNewConnection(3000)); + QSctpSocket *acceptedSocket = server.nextPendingDatagramConnection(); + QVERIFY(acceptedSocket); + + QByteArray ba(1000, 1); + QVERIFY(acceptedSocket->writeDatagram(ba)); + QVERIFY(acceptedSocket->waitForBytesWritten(3000)); + + QVERIFY(socket.waitForReadyRead(3000)); + QNetworkDatagram datagram = socket.readDatagram(); + QVERIFY(datagram.isValid()); + QCOMPARE(datagram.data(), ba); + + QCOMPARE(socket.state(), QAbstractSocket::ConnectedState); + QCOMPARE(socket.error(), QAbstractSocket::UnknownSocketError); + QCOMPARE(socket.errorString(), QString("Unknown error")); + QCOMPARE(acceptedSocket->state(), QAbstractSocket::ConnectedState); + QCOMPARE(acceptedSocket->error(), QAbstractSocket::UnknownSocketError); + QCOMPARE(acceptedSocket->errorString(), QString("Unknown error")); +} + +//---------------------------------------------------------------------------------- +void tst_QSctpSocket::loop_data() +{ + QTest::addColumn<QByteArray>("peterDatagram"); + QTest::addColumn<QByteArray>("paulDatagram"); + QTest::addColumn<int>("peterChannel"); + QTest::addColumn<int>("paulChannel"); + + QTest::newRow("\"Almond!\" | \"Joy!\"") << QByteArray("Almond!") << QByteArray("Joy!") << 0 << 0; + QTest::newRow("\"A\" | \"B\"") << QByteArray("A") << QByteArray("B") << 1 << 1; + QTest::newRow("\"AB\" | \"B\"") << QByteArray("AB") << QByteArray("B") << 0 << 1; + QTest::newRow("\"AB\" | \"BB\"") << QByteArray("AB") << QByteArray("BB") << 1 << 0; + QTest::newRow("\"A\\0B\" | \"B\\0B\"") << QByteArray::fromRawData("A\0B", 3) << QByteArray::fromRawData("B\0B", 3) << 0 << 1; + QTest::newRow("BigDatagram") << QByteArray(600, '@') << QByteArray(600, '@') << 1 << 0; +} + +void tst_QSctpSocket::loop() +{ + QFETCH(QByteArray, peterDatagram); + QFETCH(QByteArray, paulDatagram); + QFETCH(int, peterChannel); + QFETCH(int, paulChannel); + + QSctpServer server; + + server.setMaxChannelCount(10); + QVERIFY(server.listen()); + + QSctpSocket peter; + peter.setMaxChannelCount(10); + peter.connectToHost(QHostAddress::LocalHost, server.serverPort()); + QVERIFY(peter.waitForConnected(3000)); + + QVERIFY(server.waitForNewConnection(3000)); + QSctpSocket *paul = server.nextPendingDatagramConnection(); + QVERIFY(paul); + + peter.setCurrentWriteChannel(peterChannel); + QVERIFY(peter.writeDatagram(peterDatagram)); + paul->setCurrentWriteChannel(paulChannel); + QVERIFY(paul->writeDatagram(paulDatagram)); + QVERIFY(peter.flush()); + QVERIFY(paul->flush()); + + peter.setCurrentReadChannel(paulChannel); + QVERIFY(peter.waitForReadyRead(3000)); + QCOMPARE(peter.bytesAvailable(), paulDatagram.size()); + QCOMPARE(peter.readDatagram().data(), paulDatagram); + + paul->setCurrentReadChannel(peterChannel); + QVERIFY(paul->waitForReadyRead(3000)); + QCOMPARE(paul->bytesAvailable(), peterDatagram.size()); + QCOMPARE(paul->readDatagram().data(), peterDatagram); +} + +//---------------------------------------------------------------------------------- +void tst_QSctpSocket::loopInTCPMode_data() +{ + QTest::addColumn<QByteArray>("peterDatagram"); + QTest::addColumn<QByteArray>("paulDatagram"); + + QTest::newRow("\"Almond!\" | \"Joy!\"") << QByteArray("Almond!") << QByteArray("Joy!"); + QTest::newRow("\"A\" | \"B\"") << QByteArray("A") << QByteArray("B"); + QTest::newRow("\"AB\" | \"B\"") << QByteArray("AB") << QByteArray("B"); + QTest::newRow("\"AB\" | \"BB\"") << QByteArray("AB") << QByteArray("BB"); + QTest::newRow("\"A\\0B\" | \"B\\0B\"") << QByteArray::fromRawData("A\0B", 3) << QByteArray::fromRawData("B\0B", 3); + QTest::newRow("BigDatagram") << QByteArray(600, '@') << QByteArray(600, '@'); +} + +void tst_QSctpSocket::loopInTCPMode() +{ + QFETCH(QByteArray, peterDatagram); + QFETCH(QByteArray, paulDatagram); + + QSctpServer server; + + server.setMaxChannelCount(-1); + QVERIFY(server.listen()); + + QSctpSocket peter; + peter.setMaxChannelCount(-1); + peter.connectToHost(QHostAddress::LocalHost, server.serverPort()); + QVERIFY(peter.waitForConnected(3000)); + QVERIFY(server.waitForNewConnection(3000)); + + QTcpSocket *paul = server.nextPendingConnection(); + QVERIFY(paul); + + QCOMPARE(peter.write(peterDatagram), qint64(peterDatagram.size())); + QCOMPARE(paul->write(paulDatagram), qint64(paulDatagram.size())); + QVERIFY(peter.flush()); + QVERIFY(paul->flush()); + + QVERIFY(peter.waitForReadyRead(3000)); + QVERIFY(paul->waitForReadyRead(3000)); + + QCOMPARE(peter.bytesAvailable(), paulDatagram.size()); + QByteArray peterBuffer = peter.readAll(); + + QCOMPARE(paul->bytesAvailable(), peterDatagram.size()); + QByteArray paulBuffer = paul->readAll(); + + QCOMPARE(peterBuffer, paulDatagram); + QCOMPARE(paulBuffer, peterDatagram); +} + +//---------------------------------------------------------------------------------- +void tst_QSctpSocket::exitLoopSlot() +{ + exitLoop(); +} + +//---------------------------------------------------------------------------------- +void tst_QSctpSocket::readDatagramAfterClose() +{ + QSctpServer server; + + QVERIFY(server.listen()); + + QSctpSocket socket; + socket.connectToHost(QHostAddress::LocalHost, server.serverPort()); + QVERIFY(socket.waitForConnected(3000)); + QVERIFY(server.waitForNewConnection(3000)); + + QSctpSocket *acceptedSocket = server.nextPendingDatagramConnection(); + QVERIFY(acceptedSocket); + + connect(&socket, &QIODevice::readyRead, this, &tst_QSctpSocket::exitLoopSlot); + + QByteArray ba(1000, 1); + QVERIFY(acceptedSocket->writeDatagram(ba)); + + enterLoop(10); + if (timeout()) + QFAIL("Network operation timed out"); + + QCOMPARE(socket.bytesAvailable(), ba.size()); + socket.close(); + QVERIFY(!socket.readDatagram().isValid()); +} + +// Test buffered socket properly send data on delayed disconnect +void tst_QSctpSocket::clientSendDataOnDelayedDisconnect() +{ + QSctpServer server; + + QVERIFY(server.listen()); + + // Connect to server, write data and close socket + QSctpSocket socket; + socket.connectToHost(QHostAddress::LocalHost, server.serverPort()); + QVERIFY(socket.waitForConnected(3000)); + + QByteArray sendData("GET /\r\n"); + sendData = sendData.repeated(1000); + QVERIFY(socket.writeDatagram(sendData)); + socket.close(); + QCOMPARE(socket.state(), QAbstractSocket::ClosingState); + QVERIFY(socket.waitForDisconnected(3000)); + + QVERIFY(server.waitForNewConnection(3000)); + QSctpSocket *acceptedSocket = server.nextPendingDatagramConnection(); + QVERIFY(acceptedSocket); + + QVERIFY(acceptedSocket->waitForReadyRead(3000)); + QNetworkDatagram datagram = acceptedSocket->readDatagram(); + QVERIFY(datagram.isValid()); + QCOMPARE(datagram.data(), sendData); +} + +QTEST_MAIN(tst_QSctpSocket) + +#include "tst_qsctpsocket.moc" diff --git a/tests/auto/network/socket/qsocks5socketengine/tst_qsocks5socketengine.cpp b/tests/auto/network/socket/qsocks5socketengine/tst_qsocks5socketengine.cpp index b4955df107..c945d77cda 100644 --- a/tests/auto/network/socket/qsocks5socketengine/tst_qsocks5socketengine.cpp +++ b/tests/auto/network/socket/qsocks5socketengine/tst_qsocks5socketengine.cpp @@ -785,11 +785,7 @@ void tst_QSocks5SocketEngine::downloadBigFile() QTime stopWatch; stopWatch.start(); -#if !defined(Q_OS_WINCE) QTestEventLoop::instance().enterLoop(60); -#else - QTestEventLoop::instance().enterLoop(180); -#endif if (QTestEventLoop::instance().timeout()) QFAIL("Network operation timed out"); diff --git a/tests/auto/network/socket/qtcpserver/crashingServer/main.cpp b/tests/auto/network/socket/qtcpserver/crashingServer/main.cpp index fdf1c48adf..1a8e7920d3 100644 --- a/tests/auto/network/socket/qtcpserver/crashingServer/main.cpp +++ b/tests/auto/network/socket/qtcpserver/crashingServer/main.cpp @@ -47,16 +47,8 @@ int main(int argc, char *argv[]) return 1; } -#if defined(Q_OS_WINCE) - QFile file(QLatin1String("/test_signal.txt")); - file.open(QIODevice::WriteOnly); - file.write("Listening\n"); - file.flush(); - file.close(); -#else printf("Listening\n"); fflush(stdout); -#endif server.waitForNewConnection(5000); qFatal("Crash"); diff --git a/tests/auto/network/socket/qtcpserver/test/test.pro b/tests/auto/network/socket/qtcpserver/test/test.pro index f0abfbc085..4491523383 100644 --- a/tests/auto/network/socket/qtcpserver/test/test.pro +++ b/tests/auto/network/socket/qtcpserver/test/test.pro @@ -1,16 +1,7 @@ CONFIG += testcase SOURCES += ../tst_qtcpserver.cpp -win32: { -wince { - LIBS += -lws2 - crashApp.files = ../crashingServer/crashingServer.exe - crashApp.path = crashingServer - DEPLOYMENT += crashApp -} else { - LIBS += -lws2_32 -} -} +win32:LIBS += -lws2_32 TARGET = ../tst_qtcpserver diff --git a/tests/auto/network/socket/qtcpserver/tst_qtcpserver.cpp b/tests/auto/network/socket/qtcpserver/tst_qtcpserver.cpp index 30aab3bf34..5a0baf73b5 100644 --- a/tests/auto/network/socket/qtcpserver/tst_qtcpserver.cpp +++ b/tests/auto/network/socket/qtcpserver/tst_qtcpserver.cpp @@ -106,6 +106,8 @@ private slots: void eagainBlockingAccept(); + void canAccessPendingConnectionsWhileNotListening(); + private: #ifndef QT_NO_BEARERMANAGEMENT QNetworkSession *networkSession; @@ -467,11 +469,7 @@ void tst_QTcpServer::waitForConnectionTest() ThreadConnector connector(findLocalIpSocket.localAddress(), server.serverPort()); connector.start(); -#if defined(Q_OS_WINCE) - QVERIFY(server.waitForNewConnection(9000, &timeout)); -#else QVERIFY(server.waitForNewConnection(3000, &timeout)); -#endif QVERIFY(!timeout); } @@ -562,21 +560,6 @@ void tst_QTcpServer::addressReusable() QSKIP("No proxy support"); #endif // QT_NO_NETWORKPROXY } -#if defined(Q_OS_WINCE) - QString signalName = QString::fromLatin1("/test_signal.txt"); - QFile::remove(signalName); - // The crashingServer process will crash once it gets a connection. - QProcess process; - QString processExe = crashingServerDir + "/crashingServer"; - process.start(processExe); - QVERIFY2(process.waitForStarted(), qPrintable( - QString::fromLatin1("Could not start %1: %2").arg(processExe, process.errorString()))); - int waitCount = 5; - while (waitCount-- && !QFile::exists(signalName)) - QTest::qWait(1000); - QVERIFY(QFile::exists(signalName)); - QFile::remove(signalName); -#else // The crashingServer process will crash once it gets a connection. QProcess process; QString processExe = crashingServerDir + "/crashingServer"; @@ -584,7 +567,6 @@ void tst_QTcpServer::addressReusable() QVERIFY2(process.waitForStarted(), qPrintable( QString::fromLatin1("Could not start %1: %2").arg(processExe, process.errorString()))); QVERIFY(process.waitForReadyRead(5000)); -#endif QTcpSocket socket; socket.connectToHost(QHostAddress::LocalHost, 49199); @@ -990,5 +972,22 @@ void tst_QTcpServer::eagainBlockingAccept() server.close(); } +class NonListeningTcpServer : public QTcpServer +{ +public: + void addSocketFromOutside(QTcpSocket* s) + { + addPendingConnection(s); + } +}; + +void tst_QTcpServer::canAccessPendingConnectionsWhileNotListening() +{ + NonListeningTcpServer server; + QTcpSocket socket; + server.addSocketFromOutside(&socket); + QCOMPARE(&socket, server.nextPendingConnection()); +} + QTEST_MAIN(tst_QTcpServer) #include "tst_qtcpserver.moc" diff --git a/tests/auto/network/socket/qtcpsocket/qtcpsocket.pro b/tests/auto/network/socket/qtcpsocket/qtcpsocket.pro index fe6042b8a7..5686f4a697 100644 --- a/tests/auto/network/socket/qtcpsocket/qtcpsocket.pro +++ b/tests/auto/network/socket/qtcpsocket/qtcpsocket.pro @@ -1,6 +1,6 @@ TEMPLATE = subdirs SUBDIRS = test -!wince:!vxworks: SUBDIRS += stressTest +!vxworks: SUBDIRS += stressTest requires(contains(QT_CONFIG,private_tests)) diff --git a/tests/auto/network/socket/qtcpsocket/test/test.pro b/tests/auto/network/socket/qtcpsocket/test/test.pro index 3e64b87b53..337e75b372 100644 --- a/tests/auto/network/socket/qtcpsocket/test/test.pro +++ b/tests/auto/network/socket/qtcpsocket/test/test.pro @@ -2,13 +2,7 @@ CONFIG += testcase QT = core-private network-private testlib SOURCES += ../tst_qtcpsocket.cpp -win32: { -wince { - LIBS += -lws2 -} else { - LIBS += -lws2_32 -} -} +win32:LIBS += -lws2_32 TARGET = tst_qtcpsocket diff --git a/tests/auto/network/socket/qudpsocket/test/test.pro b/tests/auto/network/socket/qudpsocket/test/test.pro index e4812416dc..73486a2bc3 100644 --- a/tests/auto/network/socket/qudpsocket/test/test.pro +++ b/tests/auto/network/socket/qudpsocket/test/test.pro @@ -15,10 +15,4 @@ win32 { DESTDIR = ../ } -wince* { - addApp.files = ../clientserver/clientserver.exe - addApp.path = clientserver - DEPLOYMENT += addApp -} - TARGET = tst_qudpsocket diff --git a/tests/auto/network/socket/qudpsocket/tst_qudpsocket.cpp b/tests/auto/network/socket/qudpsocket/tst_qudpsocket.cpp index ba49e8b041..aa01384350 100644 --- a/tests/auto/network/socket/qudpsocket/tst_qudpsocket.cpp +++ b/tests/auto/network/socket/qudpsocket/tst_qudpsocket.cpp @@ -39,6 +39,7 @@ #include <qhostinfo.h> #include <qtcpsocket.h> #include <qmap.h> +#include <qnetworkdatagram.h> #include <QNetworkProxy> #include <QNetworkInterface> @@ -114,6 +115,7 @@ protected slots: void async_readDatagramSlot(); private: + QList<QHostAddress> allAddresses; #ifndef QT_NO_BEARERMANAGEMENT QNetworkConfigurationManager *netConfMan; QNetworkConfiguration networkConfiguration; @@ -173,6 +175,7 @@ void tst_QUdpSocket::initTestCase() { if (!QtNetworkSettings::verifyTestNetworkSettings()) QSKIP("No network test server available"); + allAddresses = QNetworkInterface::allAddresses(); } void tst_QUdpSocket::init() @@ -252,6 +255,11 @@ void tst_QUdpSocket::unconnectedServerAndClientTest() int(strlen(message[i]))); buf[strlen(message[i])] = '\0'; QCOMPARE(QByteArray(buf), QByteArray(message[i])); + QCOMPARE(port, clientSocket.localPort()); + if (host.toIPv4Address()) // in case the sender is IPv4 mapped in IPv6 + QCOMPARE(host.toIPv4Address(), makeNonAny(clientSocket.localAddress()).toIPv4Address()); + else + QCOMPARE(host, makeNonAny(clientSocket.localAddress())); } } @@ -325,14 +333,32 @@ void tst_QUdpSocket::broadcasting() QVERIFY(serverSocket.hasPendingDatagrams()); do { - QByteArray arr; arr.resize(serverSocket.pendingDatagramSize() + 1); - QHostAddress host; - quint16 port; const int messageLength = int(strlen(message[i])); - QCOMPARE((int) serverSocket.readDatagram(arr.data(), arr.size() - 1, &host, &port), - messageLength); + QNetworkDatagram dgram = serverSocket.receiveDatagram(); + QVERIFY(dgram.isValid()); + QByteArray arr = dgram.data(); + + QCOMPARE(arr.length(), messageLength); arr.resize(messageLength); QCOMPARE(arr, QByteArray(message[i])); + + if (dgram.senderAddress().toIPv4Address()) // in case it's a v6-mapped address + QVERIFY2(allAddresses.contains(QHostAddress(dgram.senderAddress().toIPv4Address())), + dgram.senderAddress().toString().toLatin1()); + else if (!dgram.senderAddress().isNull()) + QVERIFY2(allAddresses.contains(dgram.senderAddress()), + dgram.senderAddress().toString().toLatin1()); + QCOMPARE(dgram.senderPort(), int(broadcastSocket.localPort())); + if (!dgram.destinationAddress().isNull()) { + QVERIFY2(dgram.destinationAddress() == QHostAddress::Broadcast + || broadcastAddresses.contains(dgram.destinationAddress()), + dgram.destinationAddress().toString().toLatin1()); + QCOMPARE(dgram.destinationPort(), int(serverSocket.localPort())); + } + + int ttl = dgram.hopLimit(); + if (ttl != -1) + QVERIFY(ttl != 0); } while (serverSocket.hasPendingDatagrams()); } } @@ -435,13 +461,8 @@ void tst_QUdpSocket::ipv6Loop() char peterBuffer[16*1024]; char paulBuffer[16*1024]; -#if !defined(Q_OS_WINCE) - QVERIFY2(peter.waitForReadyRead(5000), QtNetworkSettings::msgSocketError(peter).constData()); - QVERIFY2(paul.waitForReadyRead(5000), QtNetworkSettings::msgSocketError(paul).constData()); -#else - QVERIFY(peter.waitForReadyRead(15000)); - QVERIFY(paul.waitForReadyRead(15000)); -#endif + QVERIFY(peter.waitForReadyRead(5000)); + QVERIFY(paul.waitForReadyRead(5000)); if (success) { QCOMPARE(peter.readDatagram(peterBuffer, sizeof(peterBuffer)), qint64(paulMessage.length())); QCOMPARE(paul.readDatagram(paulBuffer, sizeof(peterBuffer)), qint64(peterMessage.length())); @@ -938,9 +959,6 @@ void tst_QUdpSocket::outOfProcessConnectedClientServerTest() #ifdef QT_NO_PROCESS QSKIP("No qprocess support", SkipAll); #else -#if defined(Q_OS_WINCE) - QSKIP("This test depends on reading data from QProcess (not supported on Qt/WinCE)."); -#endif QProcess serverProcess; serverProcess.start(QLatin1String("clientserver/clientserver server 1 1"), QIODevice::ReadWrite | QIODevice::Text); @@ -1002,9 +1020,6 @@ void tst_QUdpSocket::outOfProcessUnconnectedClientServerTest() #ifdef QT_NO_PROCESS QSKIP("No qprocess support", SkipAll); #else -#if defined(Q_OS_WINCE) - QSKIP("This test depends on reading data from QProcess (not supported on Qt/WinCE)."); -#endif QProcess serverProcess; serverProcess.start(QLatin1String("clientserver/clientserver server 1 1"), QIODevice::ReadWrite | QIODevice::Text); @@ -1081,7 +1096,7 @@ void tst_QUdpSocket::zeroLengthDatagram() #ifdef FORCE_SESSION sender.setProperty("_q_networksession", QVariant::fromValue(networkSession)); #endif - QCOMPARE(sender.writeDatagram(QByteArray(), QHostAddress::LocalHost, receiver.localPort()), qint64(0)); + QCOMPARE(sender.writeDatagram(QNetworkDatagram(QByteArray(), QHostAddress::LocalHost, receiver.localPort())), qint64(0)); QVERIFY2(receiver.waitForReadyRead(1000), QtNetworkSettings::msgSocketError(receiver).constData()); QVERIFY(receiver.hasPendingDatagrams()); @@ -1366,10 +1381,20 @@ void tst_QUdpSocket::multicast() QVERIFY(receiver.hasPendingDatagrams()); QList<QByteArray> receivedDatagrams; while (receiver.hasPendingDatagrams()) { - QByteArray datagram; - datagram.resize(receiver.pendingDatagramSize()); - receiver.readDatagram(datagram.data(), datagram.size(), 0, 0); - receivedDatagrams << datagram; + QNetworkDatagram dgram = receiver.receiveDatagram(); + receivedDatagrams << dgram.data(); + + QVERIFY2(allAddresses.contains(dgram.senderAddress()), + dgram.senderAddress().toString().toLatin1()); + QCOMPARE(dgram.senderPort(), int(sender.localPort())); + if (!dgram.destinationAddress().isNull()) { + QCOMPARE(dgram.destinationAddress(), groupAddress); + QCOMPARE(dgram.destinationPort(), int(receiver.localPort())); + } + + int ttl = dgram.hopLimit(); + if (ttl != -1) + QVERIFY(ttl != 0); } QCOMPARE(receivedDatagrams, datagrams); @@ -1464,7 +1489,8 @@ void tst_QUdpSocket::linkLocalIPv6() quint16 port = 0; foreach (const QHostAddress& addr, addresses) { QUdpSocket *s = new QUdpSocket; - QVERIFY2(s->bind(addr, port), qPrintable(s->errorString())); + QVERIFY2(s->bind(addr, port), addr.toString().toLatin1() + + '/' + QByteArray::number(port) + ": " + qPrintable(s->errorString())); port = s->localPort(); //bind same port, different networks sockets << s; } @@ -1474,24 +1500,25 @@ void tst_QUdpSocket::linkLocalIPv6() QSignalSpy neutralReadSpy(&neutral, SIGNAL(readyRead())); QByteArray testData("hello"); - QByteArray receiveBuffer("xxxxx"); foreach (QUdpSocket *s, sockets) { QSignalSpy spy(s, SIGNAL(readyRead())); neutralReadSpy.clear(); QVERIFY(s->writeDatagram(testData, s->localAddress(), neutral.localPort())); QTRY_VERIFY(neutralReadSpy.count() > 0); //note may need to accept a firewall prompt - QHostAddress from; - quint16 fromPort; - QCOMPARE((int)neutral.readDatagram(receiveBuffer.data(), receiveBuffer.length(), &from, &fromPort), testData.length()); - QCOMPARE(from, s->localAddress()); - QCOMPARE(fromPort, s->localPort()); - QCOMPARE(receiveBuffer, testData); - - QVERIFY(neutral.writeDatagram(testData, s->localAddress(), s->localPort())); + + QNetworkDatagram dgram = neutral.receiveDatagram(testData.length() * 2); + QVERIFY(dgram.isValid()); + QCOMPARE(dgram.senderAddress(), s->localAddress()); + QCOMPARE(dgram.senderPort(), int(s->localPort())); + QCOMPARE(dgram.data().length(), testData.length()); + QCOMPARE(dgram.data(), testData); + + QVERIFY(neutral.writeDatagram(dgram.makeReply(testData))); QTRY_VERIFY(spy.count() > 0); //note may need to accept a firewall prompt - QCOMPARE((int)s->readDatagram(receiveBuffer.data(), receiveBuffer.length(), &from, &fromPort), testData.length()); - QCOMPARE(receiveBuffer, testData); + + dgram = s->receiveDatagram(testData.length() * 2); + QCOMPARE(dgram.data(), testData); //sockets bound to other interfaces shouldn't have received anything foreach (QUdpSocket *s2, sockets) { @@ -1546,21 +1573,23 @@ void tst_QUdpSocket::linkLocalIPv4() QVERIFY(neutral.bind(QHostAddress(QHostAddress::AnyIPv4))); QByteArray testData("hello"); - QByteArray receiveBuffer("xxxxx"); foreach (QUdpSocket *s, sockets) { QVERIFY(s->writeDatagram(testData, s->localAddress(), neutral.localPort())); QVERIFY2(neutral.waitForReadyRead(10000), QtNetworkSettings::msgSocketError(neutral).constData()); - QHostAddress from; - quint16 fromPort; - QCOMPARE((int)neutral.readDatagram(receiveBuffer.data(), receiveBuffer.length(), &from, &fromPort), testData.length()); - QCOMPARE(from, s->localAddress()); - QCOMPARE(fromPort, s->localPort()); - QCOMPARE(receiveBuffer, testData); - - QVERIFY(neutral.writeDatagram(testData, s->localAddress(), s->localPort())); + QVERIFY2(s->waitForReadyRead(10000), QtNetworkSettings::msgSocketError(*s).constData()); - QCOMPARE((int)s->readDatagram(receiveBuffer.data(), receiveBuffer.length(), &from, &fromPort), testData.length()); - QCOMPARE(receiveBuffer, testData); + QNetworkDatagram dgram = neutral.receiveDatagram(testData.length() * 2); + QVERIFY(dgram.isValid()); + QCOMPARE(dgram.senderAddress(), s->localAddress()); + QCOMPARE(dgram.senderPort(), int(s->localPort())); + QCOMPARE(dgram.data().length(), testData.length()); + QCOMPARE(dgram.data(), testData); + + QVERIFY(neutral.writeDatagram(dgram.makeReply(testData))); + + dgram = s->receiveDatagram(testData.length() * 2); + QVERIFY(dgram.isValid()); + QCOMPARE(dgram.data(), testData); //sockets bound to other interfaces shouldn't have received anything foreach (QUdpSocket *s2, sockets) { diff --git a/tests/auto/network/socket/socket.pro b/tests/auto/network/socket/socket.pro index 436ebe5c7f..75f9e3f3a0 100644 --- a/tests/auto/network/socket/socket.pro +++ b/tests/auto/network/socket/socket.pro @@ -8,6 +8,7 @@ SUBDIRS=\ qsocks5socketengine \ qabstractsocket \ platformsocketengine \ + qsctpsocket \ !contains(QT_CONFIG, private_tests): SUBDIRS -= \ platformsocketengine \ @@ -15,6 +16,9 @@ SUBDIRS=\ qhttpsocketengine \ qsocks5socketengine \ +!contains(QT_CONFIG, sctp): SUBDIRS -= \ + qsctpsocket \ + winrt: SUBDIRS -= \ qhttpsocketengine \ qsocks5socketengine \ |