2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-15 12:36:27 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the test suite of the Qt Toolkit.
|
|
|
|
**
|
2016-01-15 12:36:27 +00:00
|
|
|
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
|
2012-09-19 12:28:29 +00:00
|
|
|
** 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
|
2015-01-28 08:44:43 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
2016-01-15 12:36:27 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2016-01-15 12:36:27 +00:00
|
|
|
** 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.
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <qglobal.h>
|
2012-06-12 07:57:10 +00:00
|
|
|
// To prevent windows system header files from re-defining min/max
|
2012-09-17 10:32:09 +00:00
|
|
|
#define NOMINMAX 1
|
2011-09-27 09:53:01 +00:00
|
|
|
#if defined(_WIN32)
|
2011-04-27 10:05:43 +00:00
|
|
|
#include <winsock2.h>
|
|
|
|
#else
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#define SOCKET int
|
|
|
|
#define INVALID_SOCKET -1
|
|
|
|
#endif
|
|
|
|
|
2020-11-26 16:31:50 +00:00
|
|
|
#include <QTest>
|
|
|
|
#include <QSignalSpy>
|
|
|
|
#include <QTimer>
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
#ifndef Q_OS_WIN
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <qcoreapplication.h>
|
|
|
|
#include <qtcpsocket.h>
|
|
|
|
#include <qtcpserver.h>
|
|
|
|
#include <qhostaddress.h>
|
2016-11-23 11:25:17 +00:00
|
|
|
#if QT_CONFIG(process)
|
|
|
|
# include <qprocess.h>
|
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
#include <qstringlist.h>
|
|
|
|
#include <qplatformdefs.h>
|
|
|
|
#include <qhostinfo.h>
|
2020-03-04 15:33:51 +00:00
|
|
|
#include <qnetworkinterface.h>
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
#include <QNetworkProxy>
|
|
|
|
|
2011-09-06 11:46:40 +00:00
|
|
|
#include "../../../network-settings.h"
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-04-18 15:05:32 +00:00
|
|
|
#if defined(Q_OS_LINUX)
|
|
|
|
#define SHOULD_CHECK_SYSCALL_SUPPORT
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
class tst_QTcpServer : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
2015-12-03 15:59:30 +00:00
|
|
|
private slots:
|
2011-04-27 10:05:43 +00:00
|
|
|
void initTestCase_data();
|
|
|
|
void initTestCase();
|
|
|
|
void init();
|
|
|
|
void cleanup();
|
|
|
|
void getSetCheck();
|
|
|
|
void constructing();
|
|
|
|
void clientServerLoop();
|
|
|
|
void ipv6Server();
|
2011-06-07 13:38:38 +00:00
|
|
|
void dualStack_data();
|
|
|
|
void dualStack();
|
2011-04-27 10:05:43 +00:00
|
|
|
void ipv6ServerMapped();
|
|
|
|
void crashTests();
|
|
|
|
void maxPendingConnections();
|
|
|
|
void listenError();
|
|
|
|
void waitForConnectionTest();
|
|
|
|
void setSocketDescriptor();
|
|
|
|
void listenWhileListening();
|
|
|
|
void addressReusable();
|
|
|
|
void setNewSocketDescriptorBlocking();
|
2013-12-06 12:36:03 +00:00
|
|
|
#ifndef QT_NO_NETWORKPROXY
|
2011-04-27 10:05:43 +00:00
|
|
|
void invalidProxy_data();
|
|
|
|
void invalidProxy();
|
|
|
|
void proxyFactory_data();
|
|
|
|
void proxyFactory();
|
2013-12-06 12:36:03 +00:00
|
|
|
#endif // !QT_NO_NETWORKPROXY
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
void qtbug14268_peek();
|
|
|
|
|
2011-11-30 17:21:51 +00:00
|
|
|
void serverAddress_data();
|
|
|
|
void serverAddress();
|
2012-03-30 19:21:40 +00:00
|
|
|
|
|
|
|
void qtbug6305_data() { serverAddress_data(); }
|
|
|
|
void qtbug6305();
|
|
|
|
|
2012-05-10 15:37:57 +00:00
|
|
|
void linkLocal();
|
|
|
|
|
2012-06-12 01:27:03 +00:00
|
|
|
void eagainBlockingAccept();
|
|
|
|
|
2016-03-07 13:37:57 +00:00
|
|
|
void canAccessPendingConnectionsWhileNotListening();
|
2016-03-02 15:52:22 +00:00
|
|
|
|
2020-11-24 16:21:41 +00:00
|
|
|
void pauseAccepting();
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
private:
|
2017-04-18 15:05:32 +00:00
|
|
|
bool shouldSkipIpv6TestsForBrokenGetsockopt();
|
|
|
|
#ifdef SHOULD_CHECK_SYSCALL_SUPPORT
|
|
|
|
bool ipv6GetsockoptionMissing(int level, int optname);
|
|
|
|
#endif
|
|
|
|
|
2012-02-13 14:45:13 +00:00
|
|
|
QString crashingServerDir;
|
2011-04-27 10:05:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Testing get/set functions
|
|
|
|
void tst_QTcpServer::getSetCheck()
|
|
|
|
{
|
|
|
|
QTcpServer obj1;
|
|
|
|
// int QTcpServer::maxPendingConnections()
|
|
|
|
// void QTcpServer::setMaxPendingConnections(int)
|
|
|
|
obj1.setMaxPendingConnections(0);
|
|
|
|
QCOMPARE(0, obj1.maxPendingConnections());
|
|
|
|
obj1.setMaxPendingConnections(INT_MIN);
|
|
|
|
QCOMPARE(INT_MIN, obj1.maxPendingConnections());
|
|
|
|
obj1.setMaxPendingConnections(INT_MAX);
|
|
|
|
QCOMPARE(INT_MAX, obj1.maxPendingConnections());
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QTcpServer::initTestCase_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn<bool>("setProxy");
|
|
|
|
QTest::addColumn<int>("proxyType");
|
|
|
|
|
|
|
|
QTest::newRow("WithoutProxy") << false << 0;
|
2017-09-04 19:52:30 +00:00
|
|
|
#if QT_CONFIG(socks5)
|
2011-04-27 10:05:43 +00:00
|
|
|
QTest::newRow("WithSocks5Proxy") << true << int(QNetworkProxy::Socks5Proxy);
|
2013-12-06 12:36:03 +00:00
|
|
|
#endif
|
2012-02-13 14:45:13 +00:00
|
|
|
|
|
|
|
crashingServerDir = QFINDTESTDATA("crashingServer");
|
|
|
|
QVERIFY2(!crashingServerDir.isEmpty(), qPrintable(
|
|
|
|
QString::fromLatin1("Couldn't find crashingServer dir starting from %1.").arg(QDir::currentPath())));
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QTcpServer::initTestCase()
|
|
|
|
{
|
2019-02-21 14:16:37 +00:00
|
|
|
#ifdef QT_TEST_SERVER
|
|
|
|
QVERIFY(QtNetworkSettings::verifyConnection(QtNetworkSettings::socksProxyServerName(), 1080));
|
|
|
|
QVERIFY(QtNetworkSettings::verifyConnection(QtNetworkSettings::httpProxyServerName(), 3128));
|
|
|
|
QVERIFY(QtNetworkSettings::verifyConnection(QtNetworkSettings::ftpProxyServerName(), 2121));
|
|
|
|
QVERIFY(QtNetworkSettings::verifyConnection(QtNetworkSettings::imapServerName(), 143));
|
|
|
|
#else
|
2012-10-22 10:17:48 +00:00
|
|
|
if (!QtNetworkSettings::verifyTestNetworkSettings())
|
|
|
|
QSKIP("No network test server available");
|
2019-02-21 14:16:37 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QTcpServer::init()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(bool, setProxy);
|
|
|
|
if (setProxy) {
|
2013-12-06 12:36:03 +00:00
|
|
|
#ifndef QT_NO_NETWORKPROXY
|
2011-04-27 10:05:43 +00:00
|
|
|
QFETCH_GLOBAL(int, proxyType);
|
|
|
|
if (proxyType == QNetworkProxy::Socks5Proxy) {
|
2019-02-21 14:16:37 +00:00
|
|
|
QNetworkProxy::setApplicationProxy(QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::socksProxyServerName(), 1080));
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2013-12-06 12:36:03 +00:00
|
|
|
#else // !QT_NO_NETWORKPROXY
|
|
|
|
QSKIP("No proxy support");
|
|
|
|
#endif // QT_NO_NETWORKPROXY
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QTcpServer::cleanup()
|
|
|
|
{
|
2013-12-06 12:36:03 +00:00
|
|
|
#ifndef QT_NO_NETWORKPROXY
|
2011-04-27 10:05:43 +00:00
|
|
|
QNetworkProxy::setApplicationProxy(QNetworkProxy::DefaultProxy);
|
2013-12-06 12:36:03 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2017-04-18 15:05:32 +00:00
|
|
|
#ifdef SHOULD_CHECK_SYSCALL_SUPPORT
|
|
|
|
bool tst_QTcpServer::ipv6GetsockoptionMissing(int level, int optname)
|
|
|
|
{
|
|
|
|
int testSocket;
|
|
|
|
|
|
|
|
testSocket = socket(PF_INET6, SOCK_STREAM, 0);
|
|
|
|
|
|
|
|
// If we can't test here, assume it's not missing
|
|
|
|
if (testSocket == -1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool result = false;
|
|
|
|
if (getsockopt(testSocket, level, optname, nullptr, 0) == -1) {
|
|
|
|
if (errno == EOPNOTSUPP) {
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(testSocket);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif //SHOULD_CHECK_SYSCALL_SUPPORT
|
|
|
|
|
|
|
|
bool tst_QTcpServer::shouldSkipIpv6TestsForBrokenGetsockopt()
|
|
|
|
{
|
|
|
|
#ifdef SHOULD_CHECK_SYSCALL_SUPPORT
|
|
|
|
// Following parameters for setsockopt are not supported by all QEMU versions:
|
|
|
|
if (ipv6GetsockoptionMissing(SOL_IPV6, IPV6_V6ONLY)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif //SHOULD_CHECK_SYSCALL_SUPPORT
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void tst_QTcpServer::constructing()
|
|
|
|
{
|
|
|
|
QTcpServer socket;
|
|
|
|
|
|
|
|
// Check the initial state of the QTcpSocket.
|
|
|
|
QCOMPARE(socket.isListening(), false);
|
|
|
|
QCOMPARE((int)socket.serverPort(), 0);
|
|
|
|
QCOMPARE(socket.serverAddress(), QHostAddress());
|
|
|
|
QCOMPARE(socket.maxPendingConnections(), 30);
|
|
|
|
QCOMPARE(socket.hasPendingConnections(), false);
|
2012-01-05 12:38:39 +00:00
|
|
|
QCOMPARE(socket.socketDescriptor(), (qintptr)-1);
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(socket.serverError(), QAbstractSocket::UnknownSocketError);
|
|
|
|
|
|
|
|
// Check the state of the socket layer?
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
void tst_QTcpServer::clientServerLoop()
|
|
|
|
{
|
|
|
|
QTcpServer server;
|
|
|
|
|
|
|
|
QSignalSpy spy(&server, SIGNAL(newConnection()));
|
|
|
|
|
|
|
|
QVERIFY(!server.isListening());
|
|
|
|
QVERIFY(!server.hasPendingConnections());
|
|
|
|
QVERIFY(server.listen(QHostAddress::Any, 11423));
|
|
|
|
QVERIFY(server.isListening());
|
|
|
|
|
|
|
|
QTcpSocket client;
|
|
|
|
|
|
|
|
QHostAddress serverAddress = QHostAddress::LocalHost;
|
2012-07-03 12:20:37 +00:00
|
|
|
if (!(server.serverAddress() == QHostAddress::Any) && !(server.serverAddress() == QHostAddress::AnyIPv6) && !(server.serverAddress() == QHostAddress::AnyIPv4))
|
2011-04-27 10:05:43 +00:00
|
|
|
serverAddress = server.serverAddress();
|
|
|
|
|
|
|
|
client.connectToHost(serverAddress, server.serverPort());
|
|
|
|
QVERIFY(client.waitForConnected(5000));
|
|
|
|
|
|
|
|
QVERIFY(server.waitForNewConnection(5000));
|
|
|
|
QVERIFY(server.hasPendingConnections());
|
|
|
|
|
|
|
|
QCOMPARE(spy.count(), 1);
|
|
|
|
|
|
|
|
QTcpSocket *serverSocket = server.nextPendingConnection();
|
|
|
|
QVERIFY(serverSocket != 0);
|
|
|
|
|
|
|
|
QVERIFY(serverSocket->write("Greetings, client!\n", 19) == 19);
|
|
|
|
serverSocket->flush();
|
|
|
|
|
|
|
|
QVERIFY(client.waitForReadyRead(5000));
|
|
|
|
QByteArray arr = client.readAll();
|
|
|
|
QCOMPARE(arr.constData(), "Greetings, client!\n");
|
|
|
|
|
|
|
|
QVERIFY(client.write("Well, hello to you!\n", 20) == 20);
|
|
|
|
client.flush();
|
|
|
|
|
|
|
|
QVERIFY(serverSocket->waitForReadyRead(5000));
|
|
|
|
arr = serverSocket->readAll();
|
|
|
|
QCOMPARE(arr.constData(), "Well, hello to you!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
void tst_QTcpServer::ipv6Server()
|
|
|
|
{
|
2012-05-04 13:18:39 +00:00
|
|
|
if (!QtNetworkSettings::hasIPv6())
|
|
|
|
QSKIP("system doesn't support ipv6!");
|
2011-04-27 10:05:43 +00:00
|
|
|
//### need to enter the event loop for the server to get the connection ?? ( windows)
|
|
|
|
QTcpServer server;
|
|
|
|
if (!server.listen(QHostAddress::LocalHostIPv6, 8944)) {
|
2015-07-24 13:47:02 +00:00
|
|
|
QCOMPARE(server.serverError(), QAbstractSocket::UnsupportedSocketOperationError);
|
2011-04-27 10:05:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-24 13:47:02 +00:00
|
|
|
QCOMPARE(server.serverPort(), quint16(8944));
|
2011-04-27 10:05:43 +00:00
|
|
|
QVERIFY(server.serverAddress() == QHostAddress::LocalHostIPv6);
|
|
|
|
|
|
|
|
QTcpSocket client;
|
|
|
|
client.connectToHost("::1", 8944);
|
|
|
|
QVERIFY(client.waitForConnected(5000));
|
|
|
|
|
|
|
|
QVERIFY(server.waitForNewConnection());
|
|
|
|
QVERIFY(server.hasPendingConnections());
|
|
|
|
|
|
|
|
QTcpSocket *serverSocket = 0;
|
|
|
|
QVERIFY((serverSocket = server.nextPendingConnection()));
|
|
|
|
serverSocket->close();
|
|
|
|
delete serverSocket;
|
|
|
|
}
|
|
|
|
|
2011-06-07 13:38:38 +00:00
|
|
|
Q_DECLARE_METATYPE(QHostAddress);
|
|
|
|
|
|
|
|
void tst_QTcpServer::dualStack_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn<QHostAddress>("bindAddress");
|
|
|
|
QTest::addColumn<bool>("v4ok");
|
|
|
|
QTest::addColumn<bool>("v6ok");
|
|
|
|
QTest::newRow("any") << QHostAddress(QHostAddress::Any) << true << true;
|
|
|
|
QTest::newRow("anyIPv4") << QHostAddress(QHostAddress::AnyIPv4) << true << false;
|
|
|
|
QTest::newRow("anyIPv6") << QHostAddress(QHostAddress::AnyIPv6) << false << true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QTcpServer::dualStack()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(bool, setProxy);
|
|
|
|
if (setProxy)
|
2011-10-19 02:53:13 +00:00
|
|
|
QSKIP("test server proxy doesn't support ipv6");
|
2012-05-04 13:18:39 +00:00
|
|
|
if (!QtNetworkSettings::hasIPv6())
|
|
|
|
QSKIP("system doesn't support ipv6!");
|
2011-06-07 13:38:38 +00:00
|
|
|
QFETCH(QHostAddress, bindAddress);
|
|
|
|
QFETCH(bool, v4ok);
|
|
|
|
QFETCH(bool, v6ok);
|
|
|
|
|
|
|
|
QTcpServer server;
|
|
|
|
QVERIFY(server.listen(bindAddress));
|
|
|
|
|
|
|
|
QTcpSocket v4client;
|
|
|
|
v4client.connectToHost(QHostAddress::LocalHost, server.serverPort());
|
|
|
|
|
|
|
|
QTcpSocket v6client;
|
|
|
|
v6client.connectToHost(QHostAddress::LocalHostIPv6, server.serverPort());
|
|
|
|
|
|
|
|
QCOMPARE(v4client.waitForConnected(5000), v4ok);
|
|
|
|
QCOMPARE(v6client.waitForConnected(5000), v6ok);
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
void tst_QTcpServer::ipv6ServerMapped()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(bool, setProxy);
|
|
|
|
if (setProxy)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QTcpServer server;
|
|
|
|
QVERIFY(server.listen(QHostAddress::LocalHost));
|
|
|
|
|
|
|
|
// let's try the normal case
|
|
|
|
QTcpSocket client1;
|
|
|
|
client1.connectToHost("127.0.0.1", server.serverPort());
|
|
|
|
QVERIFY(server.waitForNewConnection(5000));
|
|
|
|
delete server.nextPendingConnection();
|
|
|
|
|
2012-05-04 13:18:39 +00:00
|
|
|
if (!QtNetworkSettings::hasIPv6())
|
|
|
|
QSKIP("system doesn't support ipv6!");
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
// let's try the mapped one in the nice format
|
|
|
|
QTcpSocket client2;
|
|
|
|
client2.connectToHost("::ffff:127.0.0.1", server.serverPort());
|
|
|
|
QVERIFY(server.waitForNewConnection(5000));
|
|
|
|
delete server.nextPendingConnection();
|
|
|
|
|
|
|
|
// let's try the mapped in hex format
|
|
|
|
QTcpSocket client3;
|
|
|
|
client3.connectToHost("::ffff:7F00:0001", server.serverPort());
|
|
|
|
QVERIFY(server.waitForNewConnection(5000));
|
|
|
|
delete server.nextPendingConnection();
|
|
|
|
|
|
|
|
// However connecting to the v6 localhost should not work
|
|
|
|
QTcpSocket client4;
|
|
|
|
client4.connectToHost("::1", server.serverPort());
|
|
|
|
QVERIFY(!server.waitForNewConnection(5000));
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
void tst_QTcpServer::crashTests()
|
|
|
|
{
|
|
|
|
QTcpServer server;
|
|
|
|
server.close();
|
|
|
|
QVERIFY(server.listen());
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
void tst_QTcpServer::maxPendingConnections()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(bool, setProxy);
|
|
|
|
if (setProxy) {
|
2013-12-06 12:36:03 +00:00
|
|
|
#ifndef QT_NO_NETWORKPROXY
|
2011-04-27 10:05:43 +00:00
|
|
|
QFETCH_GLOBAL(int, proxyType);
|
2011-10-19 02:53:13 +00:00
|
|
|
if (proxyType == QNetworkProxy::Socks5Proxy)
|
|
|
|
QSKIP("With socks5 only 1 connection is allowed ever");
|
2013-12-06 12:36:03 +00:00
|
|
|
#else // !QT_NO_NETWORKPROXY
|
|
|
|
QSKIP("No proxy support");
|
|
|
|
#endif // QT_NO_NETWORKPROXY
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
//### sees to fail sometimes ... a timing issue with the test on windows
|
|
|
|
QTcpServer server;
|
|
|
|
server.setMaxPendingConnections(2);
|
|
|
|
|
|
|
|
QTcpSocket socket1;
|
|
|
|
QTcpSocket socket2;
|
|
|
|
QTcpSocket socket3;
|
|
|
|
|
2018-02-02 14:46:03 +00:00
|
|
|
QSignalSpy spy(&server, SIGNAL(newConnection()));
|
2011-04-27 10:05:43 +00:00
|
|
|
QVERIFY(server.listen());
|
|
|
|
|
|
|
|
socket1.connectToHost(QHostAddress::LocalHost, server.serverPort());
|
|
|
|
socket2.connectToHost(QHostAddress::LocalHost, server.serverPort());
|
|
|
|
socket3.connectToHost(QHostAddress::LocalHost, server.serverPort());
|
|
|
|
|
2018-02-02 14:46:03 +00:00
|
|
|
// We must have two and only two connections. First compare waits until
|
|
|
|
// two connections have been made. The second compare makes sure no
|
|
|
|
// more are accepted. Creating connections happens multithreaded so
|
|
|
|
// qWait must be used for that.
|
|
|
|
QTRY_COMPARE(spy.count(), 2);
|
|
|
|
QTest::qWait(100);
|
|
|
|
QCOMPARE(spy.count(), 2);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QVERIFY(server.hasPendingConnections());
|
|
|
|
QVERIFY(server.nextPendingConnection());
|
|
|
|
QVERIFY(server.hasPendingConnections());
|
|
|
|
QVERIFY(server.nextPendingConnection());
|
|
|
|
QVERIFY(!server.hasPendingConnections());
|
|
|
|
QCOMPARE(server.nextPendingConnection(), (QTcpSocket*)0);
|
|
|
|
|
|
|
|
QVERIFY(server.waitForNewConnection(5000));
|
|
|
|
|
|
|
|
QVERIFY(server.hasPendingConnections());
|
|
|
|
QVERIFY(server.nextPendingConnection());
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
void tst_QTcpServer::listenError()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(bool, setProxy);
|
|
|
|
if (setProxy) {
|
2013-12-06 12:36:03 +00:00
|
|
|
#ifndef QT_NO_NETWORKPROXY
|
2011-04-27 10:05:43 +00:00
|
|
|
QFETCH_GLOBAL(int, proxyType);
|
2011-10-19 02:53:13 +00:00
|
|
|
if (proxyType == QNetworkProxy::Socks5Proxy)
|
|
|
|
QSKIP("With socks5 we can not make hard requirements on the address or port");
|
2013-12-06 12:36:03 +00:00
|
|
|
#else // !QT_NO_NETWORKPROXY
|
|
|
|
QSKIP("No proxy support");
|
|
|
|
#endif //QT_NO_NETWORKPROXY
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
QTcpServer server;
|
|
|
|
QVERIFY(!server.listen(QHostAddress("1.2.3.4"), 0));
|
|
|
|
QCOMPARE(server.serverError(), QAbstractSocket::SocketAddressNotAvailableError);
|
|
|
|
QCOMPARE(server.errorString().toLatin1().constData(), "The address is not available");
|
|
|
|
}
|
|
|
|
|
|
|
|
class ThreadConnector : public QThread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ThreadConnector(const QHostAddress &host, quint16 port)
|
|
|
|
: host(host), port(port)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
~ThreadConnector()
|
|
|
|
{
|
|
|
|
wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2020-09-10 18:39:49 +00:00
|
|
|
void run() override
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
sleep(2);
|
|
|
|
|
|
|
|
QTcpSocket socket;
|
|
|
|
socket.connectToHost(host, port);
|
|
|
|
|
|
|
|
QEventLoop loop;
|
|
|
|
QTimer::singleShot(5000, &loop, SLOT(quit()));
|
|
|
|
loop.exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
QHostAddress host;
|
|
|
|
quint16 port;
|
|
|
|
};
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
void tst_QTcpServer::waitForConnectionTest()
|
|
|
|
{
|
|
|
|
|
|
|
|
QFETCH_GLOBAL(bool, setProxy);
|
|
|
|
if (setProxy) {
|
2013-12-06 12:36:03 +00:00
|
|
|
#ifndef QT_NO_NETWORKPROXY
|
2011-04-27 10:05:43 +00:00
|
|
|
QFETCH_GLOBAL(int, proxyType);
|
2011-10-19 02:53:13 +00:00
|
|
|
if (proxyType == QNetworkProxy::Socks5Proxy)
|
|
|
|
QSKIP("Localhost servers don't work well with SOCKS5");
|
2013-12-06 12:36:03 +00:00
|
|
|
#else // !QT_NO_NETWORKPROXY
|
|
|
|
QSKIP("No proxy support");
|
|
|
|
#endif // QT_NO_NETWORKPROXY
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QTcpSocket findLocalIpSocket;
|
2019-02-21 14:16:37 +00:00
|
|
|
findLocalIpSocket.connectToHost(QtNetworkSettings::imapServerName(), 143);
|
2011-04-27 10:05:43 +00:00
|
|
|
QVERIFY(findLocalIpSocket.waitForConnected(5000));
|
|
|
|
|
|
|
|
QTcpServer server;
|
|
|
|
bool timeout = false;
|
|
|
|
QVERIFY(server.listen(findLocalIpSocket.localAddress()));
|
|
|
|
QVERIFY(!server.waitForNewConnection(1000, &timeout));
|
|
|
|
QCOMPARE(server.serverError(), QAbstractSocket::SocketTimeoutError);
|
|
|
|
QVERIFY(timeout);
|
|
|
|
|
|
|
|
ThreadConnector connector(findLocalIpSocket.localAddress(), server.serverPort());
|
|
|
|
connector.start();
|
|
|
|
|
|
|
|
QVERIFY(server.waitForNewConnection(3000, &timeout));
|
|
|
|
QVERIFY(!timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
void tst_QTcpServer::setSocketDescriptor()
|
|
|
|
{
|
|
|
|
QTcpServer server;
|
|
|
|
QVERIFY(!server.setSocketDescriptor(42));
|
|
|
|
QCOMPARE(server.serverError(), QAbstractSocket::UnsupportedSocketOperationError);
|
|
|
|
//adopting Open C sockets is not supported, neither is adopting externally created RSocket
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
// ensure winsock is started
|
|
|
|
WSADATA wsaData;
|
|
|
|
QVERIFY(WSAStartup(MAKEWORD(2,0), &wsaData) == NO_ERROR);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
SOCKET sock = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
|
|
|
|
|
|
QVERIFY(sock != INVALID_SOCKET);
|
|
|
|
|
|
|
|
sockaddr_in sin;
|
|
|
|
memset(&sin, 0, sizeof(sockaddr_in));
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_port = 0;
|
|
|
|
sin.sin_addr.s_addr = 0x00000000;
|
|
|
|
QVERIFY(::bind(sock, (sockaddr*)&sin, sizeof(sockaddr_in)) == 0);
|
|
|
|
QVERIFY(::listen(sock, 10) == 0);
|
|
|
|
QVERIFY(server.setSocketDescriptor(sock));
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
WSACleanup();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
void tst_QTcpServer::listenWhileListening()
|
|
|
|
{
|
|
|
|
QTcpServer server;
|
|
|
|
QVERIFY(server.listen());
|
|
|
|
QTest::ignoreMessage(QtWarningMsg, "QTcpServer::listen() called when already listening");
|
|
|
|
QVERIFY(!server.listen());
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class SeverWithBlockingSockets : public QTcpServer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SeverWithBlockingSockets()
|
|
|
|
: ok(false) { }
|
|
|
|
|
|
|
|
bool ok;
|
|
|
|
|
|
|
|
protected:
|
2020-09-10 18:39:49 +00:00
|
|
|
void incomingConnection(qintptr socketDescriptor) override
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
// how a user woulddo it (qabstractsocketengine is not public)
|
|
|
|
unsigned long arg = 0;
|
2011-09-27 09:53:01 +00:00
|
|
|
#if defined(Q_OS_WIN)
|
2011-04-27 10:05:43 +00:00
|
|
|
ok = ::ioctlsocket(socketDescriptor, FIONBIO, &arg) == 0;
|
|
|
|
::closesocket(socketDescriptor);
|
|
|
|
#else
|
|
|
|
ok = ::ioctl(socketDescriptor, FIONBIO, &arg) == 0;
|
|
|
|
::close(socketDescriptor);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void tst_QTcpServer::addressReusable()
|
|
|
|
{
|
2016-11-23 11:25:17 +00:00
|
|
|
#if !QT_CONFIG(process)
|
2015-02-13 11:01:57 +00:00
|
|
|
QSKIP("No qprocess support", SkipAll);
|
|
|
|
#else
|
2014-07-01 09:47:07 +00:00
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
QSKIP("The addressReusable test is unstable on Linux. See QTBUG-39985.");
|
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
QFETCH_GLOBAL(bool, setProxy);
|
|
|
|
if (setProxy) {
|
2013-12-06 12:36:03 +00:00
|
|
|
#ifndef QT_NO_NETWORKPROXY
|
2011-04-27 10:05:43 +00:00
|
|
|
QFETCH_GLOBAL(int, proxyType);
|
2011-10-19 02:53:13 +00:00
|
|
|
if (proxyType == QNetworkProxy::Socks5Proxy)
|
|
|
|
QSKIP("With socks5 this test does not make senans at the momment");
|
2013-12-06 12:36:03 +00:00
|
|
|
#else // !QT_NO_NETWORKPROXY
|
|
|
|
QSKIP("No proxy support");
|
|
|
|
#endif // QT_NO_NETWORKPROXY
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
// The crashingServer process will crash once it gets a connection.
|
|
|
|
QProcess process;
|
2012-02-13 14:45:13 +00:00
|
|
|
QString processExe = crashingServerDir + "/crashingServer";
|
|
|
|
process.start(processExe);
|
|
|
|
QVERIFY2(process.waitForStarted(), qPrintable(
|
|
|
|
QString::fromLatin1("Could not start %1: %2").arg(processExe, process.errorString())));
|
2011-04-27 10:05:43 +00:00
|
|
|
QVERIFY(process.waitForReadyRead(5000));
|
|
|
|
|
|
|
|
QTcpSocket socket;
|
|
|
|
socket.connectToHost(QHostAddress::LocalHost, 49199);
|
|
|
|
QVERIFY(socket.waitForConnected(5000));
|
|
|
|
|
2011-08-01 00:17:51 +00:00
|
|
|
QVERIFY(process.waitForFinished(30000));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
// Give the system some time.
|
|
|
|
QTest::qSleep(10);
|
|
|
|
|
|
|
|
QTcpServer server;
|
|
|
|
QVERIFY(server.listen(QHostAddress::LocalHost, 49199));
|
2011-10-04 00:32:49 +00:00
|
|
|
#endif
|
2015-02-13 11:01:57 +00:00
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
void tst_QTcpServer::setNewSocketDescriptorBlocking()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(bool, setProxy);
|
|
|
|
if (setProxy) {
|
2013-12-06 12:36:03 +00:00
|
|
|
#ifndef QT_NO_NETWORKPROXY
|
2011-04-27 10:05:43 +00:00
|
|
|
QFETCH_GLOBAL(int, proxyType);
|
2011-10-19 02:53:13 +00:00
|
|
|
if (proxyType == QNetworkProxy::Socks5Proxy)
|
|
|
|
QSKIP("With socks5 we can not make the socket descripter blocking");
|
2013-12-06 12:36:03 +00:00
|
|
|
#else // !QT_NO_NETWORKPROXY
|
|
|
|
QSKIP("No proxy support");
|
|
|
|
#endif // QT_NO_NETWORKPROXY
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
SeverWithBlockingSockets server;
|
|
|
|
QVERIFY(server.listen());
|
|
|
|
|
|
|
|
QTcpSocket socket;
|
|
|
|
socket.connectToHost(QHostAddress::LocalHost, server.serverPort());
|
|
|
|
QVERIFY(server.waitForNewConnection(5000));
|
|
|
|
QVERIFY(server.ok);
|
|
|
|
}
|
|
|
|
|
2013-12-06 12:36:03 +00:00
|
|
|
#ifndef QT_NO_NETWORKPROXY
|
2011-04-27 10:05:43 +00:00
|
|
|
void tst_QTcpServer::invalidProxy_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn<int>("type");
|
|
|
|
QTest::addColumn<QString>("host");
|
|
|
|
QTest::addColumn<int>("port");
|
|
|
|
QTest::addColumn<int>("expectedError");
|
|
|
|
|
2019-02-21 14:16:37 +00:00
|
|
|
const QString imapIp = QtNetworkSettings::imapServerIp().toString();
|
|
|
|
const QString httpProxyIp = QtNetworkSettings::httpProxyServerIp().toString();
|
|
|
|
const QString socksIp = QtNetworkSettings::socksProxyServerIp().toString();
|
|
|
|
QTest::newRow("ftp-proxy") << int(QNetworkProxy::FtpCachingProxy) << imapIp << 143
|
2011-04-27 10:05:43 +00:00
|
|
|
<< int(QAbstractSocket::UnsupportedSocketOperationError);
|
2019-02-21 14:16:37 +00:00
|
|
|
QTest::newRow("http-proxy") << int(QNetworkProxy::HttpProxy) << httpProxyIp << 3128
|
2011-04-27 10:05:43 +00:00
|
|
|
<< int(QAbstractSocket::UnsupportedSocketOperationError);
|
|
|
|
|
|
|
|
QTest::newRow("no-such-host") << int(QNetworkProxy::Socks5Proxy)
|
2014-03-11 13:17:37 +00:00
|
|
|
<< "invalid.test.qt-project.org" << 1080
|
2011-04-27 10:05:43 +00:00
|
|
|
<< int(QAbstractSocket::ProxyNotFoundError);
|
2019-02-21 14:16:37 +00:00
|
|
|
QTest::newRow("socks5-on-http") << int(QNetworkProxy::Socks5Proxy) << httpProxyIp << 3128
|
2011-04-27 10:05:43 +00:00
|
|
|
<< int(QAbstractSocket::SocketTimeoutError);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QTcpServer::invalidProxy()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(bool, setProxy);
|
|
|
|
if (setProxy)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QFETCH(int, type);
|
|
|
|
QFETCH(QString, host);
|
|
|
|
QFETCH(int, port);
|
|
|
|
QNetworkProxy::ProxyType proxyType = QNetworkProxy::ProxyType(type);
|
|
|
|
QNetworkProxy proxy(proxyType, host, port);
|
|
|
|
|
|
|
|
QTcpServer server;
|
|
|
|
server.setProxy(proxy);
|
|
|
|
bool listenResult = server.listen();
|
|
|
|
|
|
|
|
QVERIFY(!listenResult);
|
|
|
|
QVERIFY(!server.errorString().isEmpty());
|
|
|
|
|
|
|
|
// note: the following test is not a hard failure.
|
|
|
|
// Sometimes, error codes change for the better
|
|
|
|
QTEST(int(server.serverError()), "expectedError");
|
|
|
|
}
|
|
|
|
|
|
|
|
// copied from tst_qnetworkreply.cpp
|
|
|
|
class MyProxyFactory: public QNetworkProxyFactory
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int callCount;
|
|
|
|
QList<QNetworkProxy> toReturn;
|
|
|
|
QNetworkProxyQuery lastQuery;
|
|
|
|
inline MyProxyFactory() { clear(); }
|
|
|
|
|
|
|
|
inline void clear()
|
|
|
|
{
|
|
|
|
callCount = 0;
|
|
|
|
toReturn = QList<QNetworkProxy>() << QNetworkProxy::DefaultProxy;
|
|
|
|
lastQuery = QNetworkProxyQuery();
|
|
|
|
}
|
|
|
|
|
2020-09-10 18:39:49 +00:00
|
|
|
virtual QList<QNetworkProxy> queryProxy(const QNetworkProxyQuery &query) override
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
lastQuery = query;
|
|
|
|
++callCount;
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void tst_QTcpServer::proxyFactory_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn<QList<QNetworkProxy> >("proxyList");
|
|
|
|
QTest::addColumn<QNetworkProxy>("proxyUsed");
|
|
|
|
QTest::addColumn<bool>("fails");
|
|
|
|
QTest::addColumn<int>("expectedError");
|
|
|
|
|
|
|
|
QList<QNetworkProxy> proxyList;
|
|
|
|
|
|
|
|
// tests that do get to listen
|
|
|
|
|
2019-02-21 14:16:37 +00:00
|
|
|
proxyList << QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::socksProxyServerName(), 1080);
|
2011-04-27 10:05:43 +00:00
|
|
|
QTest::newRow("socks5")
|
|
|
|
<< proxyList << proxyList.at(0)
|
|
|
|
<< false << int(QAbstractSocket::UnknownSocketError);
|
|
|
|
|
|
|
|
proxyList.clear();
|
2019-02-21 14:16:37 +00:00
|
|
|
proxyList << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::httpProxyServerName(), 3128)
|
|
|
|
<< QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::socksProxyServerName(), 1080);
|
2011-04-27 10:05:43 +00:00
|
|
|
QTest::newRow("cachinghttp+socks5")
|
|
|
|
<< proxyList << proxyList.at(1)
|
|
|
|
<< false << int(QAbstractSocket::UnknownSocketError);
|
|
|
|
|
|
|
|
proxyList.clear();
|
2019-02-21 14:16:37 +00:00
|
|
|
proxyList << QNetworkProxy(QNetworkProxy::FtpCachingProxy, QtNetworkSettings::ftpProxyServerName(), 2121)
|
|
|
|
<< QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::httpProxyServerName(), 3128)
|
|
|
|
<< QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::socksProxyServerName(), 1080);
|
2011-04-27 10:05:43 +00:00
|
|
|
QTest::newRow("ftp+cachinghttp+socks5")
|
|
|
|
<< proxyList << proxyList.at(2)
|
|
|
|
<< false << int(QAbstractSocket::UnknownSocketError);
|
|
|
|
|
|
|
|
// tests that fail to listen
|
|
|
|
proxyList.clear();
|
2019-02-21 14:16:37 +00:00
|
|
|
proxyList << QNetworkProxy(QNetworkProxy::HttpProxy, QtNetworkSettings::httpProxyServerName(), 3128);
|
2011-04-27 10:05:43 +00:00
|
|
|
QTest::newRow("http")
|
|
|
|
<< proxyList << proxyList.at(0)
|
|
|
|
<< true << int(QAbstractSocket::UnsupportedSocketOperationError);
|
|
|
|
|
|
|
|
proxyList.clear();
|
2019-02-21 14:16:37 +00:00
|
|
|
proxyList << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::httpProxyServerName(), 3128);
|
2011-04-27 10:05:43 +00:00
|
|
|
QTest::newRow("cachinghttp")
|
|
|
|
<< proxyList << QNetworkProxy()
|
|
|
|
<< true << int(QAbstractSocket::UnsupportedSocketOperationError);
|
|
|
|
|
|
|
|
proxyList.clear();
|
2019-02-21 14:16:37 +00:00
|
|
|
proxyList << QNetworkProxy(QNetworkProxy::FtpCachingProxy, QtNetworkSettings::ftpProxyServerName(), 2121);
|
2011-04-27 10:05:43 +00:00
|
|
|
QTest::newRow("ftp")
|
|
|
|
<< proxyList << QNetworkProxy()
|
|
|
|
<< true << int(QAbstractSocket::UnsupportedSocketOperationError);
|
|
|
|
|
|
|
|
proxyList.clear();
|
2019-02-21 14:16:37 +00:00
|
|
|
proxyList << QNetworkProxy(QNetworkProxy::FtpCachingProxy, QtNetworkSettings::ftpProxyServerName(), 2121)
|
|
|
|
<< QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::httpProxyServerName(), 3128);
|
2011-04-27 10:05:43 +00:00
|
|
|
QTest::newRow("ftp+cachinghttp")
|
|
|
|
<< proxyList << QNetworkProxy()
|
|
|
|
<< true << int(QAbstractSocket::UnsupportedSocketOperationError);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QTcpServer::proxyFactory()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(bool, setProxy);
|
|
|
|
if (setProxy)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QFETCH(QList<QNetworkProxy>, proxyList);
|
|
|
|
QFETCH(QNetworkProxy, proxyUsed);
|
|
|
|
QFETCH(bool, fails);
|
|
|
|
|
|
|
|
MyProxyFactory *factory = new MyProxyFactory;
|
|
|
|
factory->toReturn = proxyList;
|
|
|
|
QNetworkProxyFactory::setApplicationProxyFactory(factory);
|
|
|
|
|
|
|
|
QTcpServer server;
|
|
|
|
bool listenResult = server.listen();
|
|
|
|
|
|
|
|
// Verify that the factory was called properly
|
|
|
|
QCOMPARE(factory->callCount, 1);
|
|
|
|
QCOMPARE(factory->lastQuery, QNetworkProxyQuery(0, QString(), QNetworkProxyQuery::TcpServer));
|
|
|
|
|
|
|
|
QCOMPARE(listenResult, !fails);
|
|
|
|
QCOMPARE(server.errorString().isEmpty(), !fails);
|
|
|
|
|
|
|
|
// note: the following test is not a hard failure.
|
|
|
|
// Sometimes, error codes change for the better
|
|
|
|
QTEST(int(server.serverError()), "expectedError");
|
|
|
|
}
|
2013-12-06 12:36:03 +00:00
|
|
|
#endif // !QT_NO_NETWORKPROXY
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
class Qtbug14268Helper : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
QByteArray lastDataPeeked;
|
|
|
|
public slots:
|
|
|
|
void newConnection() {
|
|
|
|
QTcpServer* server=static_cast<QTcpServer*>(sender());
|
|
|
|
QTcpSocket* s=server->nextPendingConnection();
|
|
|
|
connect(s,SIGNAL(readyRead()),this,SLOT(onServerReadyRead()));
|
|
|
|
}
|
|
|
|
void onServerReadyRead() {
|
|
|
|
QTcpSocket* clientSocket=static_cast<QTcpSocket*>(sender());
|
|
|
|
lastDataPeeked = clientSocket->peek(128*1024).toHex();
|
|
|
|
QTestEventLoop::instance().exitLoop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// there is a similar test inside tst_qtcpsocket that uses the waitFor* functions instead
|
|
|
|
void tst_QTcpServer::qtbug14268_peek()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(bool, setProxy);
|
|
|
|
if (setProxy)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QTcpServer server;
|
|
|
|
server.listen();
|
|
|
|
|
|
|
|
Qtbug14268Helper helper;
|
|
|
|
QObject::connect(&server, SIGNAL(newConnection()), &helper, SLOT(newConnection()));
|
|
|
|
|
|
|
|
QTcpSocket client;
|
|
|
|
client.connectToHost(QHostAddress::LocalHost, server.serverPort());
|
|
|
|
QVERIFY(client.waitForConnected(2000));
|
|
|
|
|
|
|
|
client.write("abc\n");
|
|
|
|
QTestEventLoop::instance().enterLoop(5);
|
|
|
|
QVERIFY(!QTestEventLoop::instance().timeout());
|
2015-07-24 13:47:02 +00:00
|
|
|
QCOMPARE(helper.lastDataPeeked, QByteArray("6162630a"));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
client.write("def\n");
|
|
|
|
QTestEventLoop::instance().enterLoop(5);
|
|
|
|
QVERIFY(!QTestEventLoop::instance().timeout());
|
2015-07-24 13:47:02 +00:00
|
|
|
QCOMPARE(helper.lastDataPeeked, QByteArray("6162630a6465660a"));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
client.write("ghi\n");
|
|
|
|
QTestEventLoop::instance().enterLoop(5);
|
|
|
|
QVERIFY(!QTestEventLoop::instance().timeout());
|
2015-07-24 13:47:02 +00:00
|
|
|
QCOMPARE(helper.lastDataPeeked, QByteArray("6162630a6465660a6768690a"));
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2011-11-30 17:21:51 +00:00
|
|
|
void tst_QTcpServer::serverAddress_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn<QHostAddress>("listenAddress");
|
|
|
|
QTest::addColumn<QHostAddress>("serverAddress");
|
2012-05-04 13:18:39 +00:00
|
|
|
if (QtNetworkSettings::hasIPv6())
|
|
|
|
QTest::newRow("Any") << QHostAddress(QHostAddress::Any) << QHostAddress(QHostAddress::Any);
|
|
|
|
else
|
|
|
|
QTest::newRow("Any") << QHostAddress(QHostAddress::Any) << QHostAddress(QHostAddress::AnyIPv4);
|
2011-11-30 17:21:51 +00:00
|
|
|
QTest::newRow("AnyIPv4") << QHostAddress(QHostAddress::AnyIPv4) << QHostAddress(QHostAddress::AnyIPv4);
|
2012-05-04 13:18:39 +00:00
|
|
|
if (QtNetworkSettings::hasIPv6())
|
|
|
|
QTest::newRow("AnyIPv6") << QHostAddress(QHostAddress::AnyIPv6) << QHostAddress(QHostAddress::AnyIPv6);
|
2015-03-11 03:36:17 +00:00
|
|
|
foreach (const QNetworkInterface &iface, QNetworkInterface::allInterfaces()) {
|
|
|
|
if ((iface.flags() & QNetworkInterface::IsUp) == 0)
|
|
|
|
continue;
|
|
|
|
foreach (const QNetworkAddressEntry &entry, iface.addressEntries()) {
|
|
|
|
QTest::newRow(qPrintable(entry.ip().toString())) << entry.ip() << entry.ip();
|
|
|
|
}
|
2011-11-30 17:21:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QTcpServer::serverAddress()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(bool, setProxy);
|
|
|
|
if (setProxy)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QFETCH(QHostAddress, listenAddress);
|
|
|
|
QFETCH(QHostAddress, serverAddress);
|
|
|
|
QTcpServer server;
|
2012-03-30 19:21:40 +00:00
|
|
|
|
2017-04-18 15:05:32 +00:00
|
|
|
if (shouldSkipIpv6TestsForBrokenGetsockopt()
|
|
|
|
&& listenAddress == QHostAddress(QHostAddress::Any)) {
|
|
|
|
QSKIP("Syscalls needed for ipv6 sockoptions missing functionality");
|
|
|
|
}
|
|
|
|
|
2012-03-30 19:21:40 +00:00
|
|
|
// TODO: why does this QSKIP?
|
2011-11-30 17:21:51 +00:00
|
|
|
if (!server.listen(listenAddress))
|
|
|
|
QSKIP(qPrintable(server.errorString()));
|
|
|
|
QCOMPARE(server.serverAddress(), serverAddress);
|
|
|
|
}
|
|
|
|
|
2012-03-30 19:21:40 +00:00
|
|
|
// on OS X, calling listen() multiple times would succeed each time, which is
|
|
|
|
// most definitely not wanted.
|
|
|
|
void tst_QTcpServer::qtbug6305()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(bool, setProxy);
|
|
|
|
if (setProxy)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QFETCH(QHostAddress, listenAddress);
|
2012-05-04 13:18:39 +00:00
|
|
|
|
2012-03-30 19:21:40 +00:00
|
|
|
QTcpServer server;
|
|
|
|
QVERIFY2(server.listen(listenAddress), qPrintable(server.errorString()));
|
|
|
|
|
|
|
|
QTcpServer server2;
|
|
|
|
QVERIFY(!server2.listen(listenAddress, server.serverPort())); // second listen should fail
|
|
|
|
}
|
|
|
|
|
2012-05-10 15:37:57 +00:00
|
|
|
void tst_QTcpServer::linkLocal()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(bool, setProxy);
|
|
|
|
if (setProxy)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QList <QHostAddress> addresses;
|
|
|
|
QSet <QString> scopes;
|
|
|
|
QHostAddress localMaskv4("169.254.0.0");
|
|
|
|
QHostAddress localMaskv6("fe80::");
|
|
|
|
foreach (const QNetworkInterface& iface, QNetworkInterface::allInterfaces()) {
|
|
|
|
//Windows preallocates link local addresses to interfaces that are down.
|
|
|
|
//These may or may not work depending on network driver (they do not work for the Bluetooth PAN driver)
|
|
|
|
if (iface.flags() & QNetworkInterface::IsUp) {
|
2017-06-06 14:51:42 +00:00
|
|
|
#if defined(Q_OS_WIN)
|
2012-07-03 13:16:28 +00:00
|
|
|
// Do not connect to the Teredo Tunneling interface on Windows Xp.
|
|
|
|
if (iface.humanReadableName() == QString("Teredo Tunneling Pseudo-Interface"))
|
|
|
|
continue;
|
2017-06-06 14:51:42 +00:00
|
|
|
#elif defined(Q_OS_DARWIN)
|
|
|
|
// Do not add "utun" interfaces on macOS: nothing ever gets received
|
|
|
|
// (we don't know why)
|
|
|
|
if (iface.name().startsWith("utun"))
|
|
|
|
continue;
|
|
|
|
#endif
|
2012-05-10 15:37:57 +00:00
|
|
|
foreach (QNetworkAddressEntry addressEntry, iface.addressEntries()) {
|
|
|
|
QHostAddress addr = addressEntry.ip();
|
|
|
|
if (addr.isInSubnet(localMaskv4, 16)) {
|
|
|
|
addresses << addr;
|
|
|
|
qDebug() << addr;
|
|
|
|
}
|
|
|
|
else if (!addr.scopeId().isEmpty() && addr.isInSubnet(localMaskv6, 64)) {
|
|
|
|
scopes << addr.scopeId();
|
|
|
|
addresses << addr;
|
|
|
|
qDebug() << addr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (addresses.isEmpty())
|
|
|
|
QSKIP("no link local addresses");
|
|
|
|
|
|
|
|
QList<QTcpServer*> servers;
|
|
|
|
quint16 port = 0;
|
|
|
|
foreach (const QHostAddress& addr, addresses) {
|
|
|
|
QTcpServer *server = new QTcpServer;
|
|
|
|
QVERIFY(server->listen(addr, port));
|
|
|
|
port = server->serverPort(); //listen to same port on different interfaces
|
|
|
|
servers << server;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<QTcpSocket*> clients;
|
|
|
|
foreach (const QHostAddress& addr, addresses) {
|
|
|
|
//unbound socket
|
|
|
|
QTcpSocket *socket = new QTcpSocket;
|
|
|
|
socket->connectToHost(addr, port);
|
|
|
|
QVERIFY(socket->waitForConnected(5000));
|
|
|
|
clients << socket;
|
|
|
|
//bound socket
|
|
|
|
socket = new QTcpSocket;
|
|
|
|
QVERIFY(socket->bind(addr));
|
|
|
|
socket->connectToHost(addr, port);
|
|
|
|
QVERIFY(socket->waitForConnected(5000));
|
|
|
|
clients << socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
//each server should have two connections
|
|
|
|
foreach (QTcpServer* server, servers) {
|
|
|
|
//qDebug() << "checking for connections" << server->serverAddress() << ":" << server->serverPort();
|
|
|
|
QVERIFY(server->waitForNewConnection(5000));
|
2016-11-25 10:58:12 +00:00
|
|
|
QTcpSocket* remote = server->nextPendingConnection();
|
|
|
|
QVERIFY(remote != nullptr);
|
2012-05-10 15:37:57 +00:00
|
|
|
remote->close();
|
|
|
|
delete remote;
|
|
|
|
if (!server->hasPendingConnections())
|
|
|
|
QVERIFY(server->waitForNewConnection(5000));
|
2016-11-25 10:58:12 +00:00
|
|
|
remote = server->nextPendingConnection();
|
|
|
|
QVERIFY(remote != nullptr);
|
2012-05-10 15:37:57 +00:00
|
|
|
remote->close();
|
|
|
|
delete remote;
|
|
|
|
QVERIFY(!server->hasPendingConnections());
|
|
|
|
}
|
|
|
|
|
|
|
|
//Connecting to the same address with different scope should normally fail
|
|
|
|
//However it will pass if there are two interfaces connected to the same physical network,
|
|
|
|
//e.g. connected via wired and wireless interfaces, or two wired NICs.
|
|
|
|
//which is a reasonably common case.
|
|
|
|
//So this is not auto tested.
|
|
|
|
|
|
|
|
qDeleteAll(clients);
|
|
|
|
qDeleteAll(servers);
|
|
|
|
}
|
|
|
|
|
2012-06-12 01:27:03 +00:00
|
|
|
void tst_QTcpServer::eagainBlockingAccept()
|
|
|
|
{
|
|
|
|
QTcpServer server;
|
|
|
|
server.listen(QHostAddress::LocalHost, 7896);
|
|
|
|
|
|
|
|
// Receiving a new connection causes TemporaryError, but shouldn't pause accepting.
|
|
|
|
QTcpSocket s;
|
|
|
|
s.connectToHost(QHostAddress::LocalHost, 7896);
|
|
|
|
QSignalSpy spy(&server, SIGNAL(newConnection()));
|
|
|
|
QTRY_COMPARE_WITH_TIMEOUT(spy.count(), 1, 500);
|
|
|
|
s.close();
|
|
|
|
|
|
|
|
// To test try again, should connect just fine.
|
|
|
|
s.connectToHost(QHostAddress::LocalHost, 7896);
|
|
|
|
QTRY_COMPARE_WITH_TIMEOUT(spy.count(), 2, 500);
|
|
|
|
s.close();
|
|
|
|
server.close();
|
|
|
|
}
|
|
|
|
|
2016-03-02 15:52:22 +00:00
|
|
|
class NonListeningTcpServer : public QTcpServer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void addSocketFromOutside(QTcpSocket* s)
|
|
|
|
{
|
|
|
|
addPendingConnection(s);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-07 13:37:57 +00:00
|
|
|
void tst_QTcpServer::canAccessPendingConnectionsWhileNotListening()
|
2016-03-02 15:52:22 +00:00
|
|
|
{
|
|
|
|
NonListeningTcpServer server;
|
|
|
|
QTcpSocket socket;
|
|
|
|
server.addSocketFromOutside(&socket);
|
|
|
|
QCOMPARE(&socket, server.nextPendingConnection());
|
|
|
|
}
|
|
|
|
|
2020-11-24 16:21:41 +00:00
|
|
|
void tst_QTcpServer::pauseAccepting()
|
|
|
|
{
|
|
|
|
QTcpServer server;
|
|
|
|
QSignalSpy spy(&server, &QTcpServer::newConnection);
|
|
|
|
QVERIFY(server.listen());
|
|
|
|
|
|
|
|
QFETCH_GLOBAL(bool, setProxy);
|
|
|
|
const auto address = QHostAddress(setProxy ? QtNetworkSettings::socksProxyServerIp()
|
|
|
|
: QHostAddress::LocalHost);
|
|
|
|
|
|
|
|
const int NumSockets = 6;
|
|
|
|
QTcpSocket sockets[NumSockets];
|
|
|
|
sockets[0].connectToHost(address, server.serverPort());
|
|
|
|
QVERIFY(spy.wait());
|
|
|
|
QCOMPARE(spy.count(), 1);
|
|
|
|
|
|
|
|
server.pauseAccepting();
|
|
|
|
for (int i = 1; i < NumSockets; ++i)
|
|
|
|
sockets[i].connectToHost(address, server.serverPort());
|
|
|
|
QVERIFY(!spy.wait(400));
|
|
|
|
QCOMPARE(spy.count(), 1);
|
|
|
|
|
|
|
|
server.resumeAccepting();
|
|
|
|
if (setProxy) {
|
|
|
|
QEXPECT_FAIL("", "The socks proxy does weird things after accepting the first connection",
|
|
|
|
Abort);
|
|
|
|
}
|
|
|
|
QVERIFY(spy.wait());
|
|
|
|
QCOMPARE(spy.count(), 6);
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QTEST_MAIN(tst_QTcpServer)
|
|
|
|
#include "tst_qtcpserver.moc"
|