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 <QtTest/QtTest>
|
2011-09-27 09:53:01 +00:00
|
|
|
#include <qglobal.h>
|
2011-04-27 10:05:43 +00:00
|
|
|
#include <qcoreapplication.h>
|
|
|
|
#include <qtcpsocket.h>
|
|
|
|
#include <qtcpserver.h>
|
|
|
|
#include <qhostaddress.h>
|
|
|
|
#include <qstringlist.h>
|
|
|
|
#include <qplatformdefs.h>
|
|
|
|
#include <qhostinfo.h>
|
|
|
|
|
|
|
|
#include <QNetworkProxy>
|
|
|
|
|
|
|
|
#include "../../../../auto/network-settings.h"
|
|
|
|
|
|
|
|
class tst_QTcpServer : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
tst_QTcpServer();
|
|
|
|
virtual ~tst_QTcpServer();
|
|
|
|
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
void initTestCase_data();
|
2011-11-25 07:04:12 +00:00
|
|
|
void initTestCase();
|
2011-04-27 10:05:43 +00:00
|
|
|
void init();
|
|
|
|
void cleanup();
|
|
|
|
private slots:
|
|
|
|
void ipv4LoopbackPerformanceTest();
|
|
|
|
void ipv6LoopbackPerformanceTest();
|
|
|
|
void ipv4PerformanceTest();
|
|
|
|
};
|
|
|
|
|
|
|
|
tst_QTcpServer::tst_QTcpServer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
tst_QTcpServer::~tst_QTcpServer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QTcpServer::initTestCase_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn<bool>("setProxy");
|
|
|
|
QTest::addColumn<int>("proxyType");
|
|
|
|
|
|
|
|
QTest::newRow("WithoutProxy") << false << 0;
|
2013-12-06 12:36:03 +00:00
|
|
|
#ifndef QT_NO_NETWORKPROXY
|
2011-04-27 10:05:43 +00:00
|
|
|
QTest::newRow("WithSocks5Proxy") << true << int(QNetworkProxy::Socks5Proxy);
|
2013-12-06 12:36:03 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2011-11-25 07:04:12 +00:00
|
|
|
void tst_QTcpServer::initTestCase()
|
|
|
|
{
|
2020-01-13 15:47:15 +00:00
|
|
|
if (!QtNetworkSettings::verifyTestNetworkSettings())
|
|
|
|
QSKIP("No network test server available");
|
2011-11-25 07:04:12 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
QNetworkProxy::setApplicationProxy(QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1080));
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
void tst_QTcpServer::ipv4LoopbackPerformanceTest()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(bool, setProxy);
|
|
|
|
if (setProxy)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QTcpServer server;
|
|
|
|
QVERIFY(server.listen(QHostAddress::LocalHost));
|
|
|
|
|
|
|
|
QVERIFY(server.isListening());
|
|
|
|
|
|
|
|
QTcpSocket clientA;
|
|
|
|
clientA.connectToHost(QHostAddress::LocalHost, server.serverPort());
|
|
|
|
QVERIFY(clientA.waitForConnected(5000));
|
|
|
|
QVERIFY(clientA.state() == QAbstractSocket::ConnectedState);
|
|
|
|
|
|
|
|
QVERIFY(server.waitForNewConnection());
|
|
|
|
QTcpSocket *clientB = server.nextPendingConnection();
|
|
|
|
QVERIFY(clientB);
|
|
|
|
|
|
|
|
QByteArray buffer(16384, '@');
|
|
|
|
QTime stopWatch;
|
|
|
|
stopWatch.start();
|
|
|
|
qlonglong totalWritten = 0;
|
|
|
|
while (stopWatch.elapsed() < 5000) {
|
|
|
|
QVERIFY(clientA.write(buffer.data(), buffer.size()) > 0);
|
|
|
|
clientA.flush();
|
|
|
|
totalWritten += buffer.size();
|
|
|
|
while (clientB->waitForReadyRead(100)) {
|
|
|
|
if (clientB->bytesAvailable() == 16384)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
clientB->read(buffer.data(), buffer.size());
|
|
|
|
clientB->write(buffer.data(), buffer.size());
|
|
|
|
clientB->flush();
|
|
|
|
totalWritten += buffer.size();
|
|
|
|
while (clientA.waitForReadyRead(100)) {
|
|
|
|
if (clientA.bytesAvailable() == 16384)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
clientA.read(buffer.data(), buffer.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
qDebug("\t\t%s: %.1fMB/%.1fs: %.1fMB/s",
|
|
|
|
server.serverAddress().toString().toLatin1().constData(),
|
|
|
|
totalWritten / (1024.0 * 1024.0),
|
|
|
|
stopWatch.elapsed() / 1000.0,
|
|
|
|
(totalWritten / (stopWatch.elapsed() / 1000.0)) / (1024 * 1024));
|
|
|
|
|
|
|
|
delete clientB;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
void tst_QTcpServer::ipv6LoopbackPerformanceTest()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(bool, setProxy);
|
|
|
|
if (setProxy)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QTcpServer server;
|
|
|
|
if (!server.listen(QHostAddress::LocalHostIPv6, 0)) {
|
|
|
|
QVERIFY(server.serverError() == QAbstractSocket::UnsupportedSocketOperationError);
|
|
|
|
} else {
|
|
|
|
QTcpSocket clientA;
|
|
|
|
clientA.connectToHost(server.serverAddress(), server.serverPort());
|
|
|
|
QVERIFY(clientA.waitForConnected(5000));
|
|
|
|
|
|
|
|
QVERIFY(server.waitForNewConnection(5000));
|
|
|
|
QTcpSocket *clientB = server.nextPendingConnection();
|
|
|
|
QVERIFY(clientB);
|
|
|
|
|
|
|
|
QByteArray buffer(16384, '@');
|
|
|
|
QTime stopWatch;
|
|
|
|
stopWatch.start();
|
|
|
|
qlonglong totalWritten = 0;
|
|
|
|
while (stopWatch.elapsed() < 5000) {
|
|
|
|
clientA.write(buffer.data(), buffer.size());
|
|
|
|
clientA.flush();
|
|
|
|
totalWritten += buffer.size();
|
|
|
|
while (clientB->waitForReadyRead(100)) {
|
|
|
|
if (clientB->bytesAvailable() == 16384)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
clientB->read(buffer.data(), buffer.size());
|
|
|
|
clientB->write(buffer.data(), buffer.size());
|
|
|
|
clientB->flush();
|
|
|
|
totalWritten += buffer.size();
|
|
|
|
while (clientA.waitForReadyRead(100)) {
|
|
|
|
if (clientA.bytesAvailable() == 16384)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
clientA.read(buffer.data(), buffer.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
qDebug("\t\t%s: %.1fMB/%.1fs: %.1fMB/s",
|
|
|
|
server.serverAddress().toString().toLatin1().constData(),
|
|
|
|
totalWritten / (1024.0 * 1024.0),
|
|
|
|
stopWatch.elapsed() / 1000.0,
|
|
|
|
(totalWritten / (stopWatch.elapsed() / 1000.0)) / (1024 * 1024));
|
|
|
|
delete clientB;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
void tst_QTcpServer::ipv4PerformanceTest()
|
|
|
|
{
|
|
|
|
QTcpSocket probeSocket;
|
|
|
|
probeSocket.connectToHost(QtNetworkSettings::serverName(), 143);
|
|
|
|
QVERIFY(probeSocket.waitForConnected(5000));
|
|
|
|
|
|
|
|
QTcpServer server;
|
|
|
|
QVERIFY(server.listen(probeSocket.localAddress(), 0));
|
|
|
|
|
|
|
|
QTcpSocket clientA;
|
|
|
|
clientA.connectToHost(server.serverAddress(), server.serverPort());
|
|
|
|
QVERIFY(clientA.waitForConnected(5000));
|
|
|
|
|
|
|
|
QVERIFY(server.waitForNewConnection(5000));
|
|
|
|
QTcpSocket *clientB = server.nextPendingConnection();
|
|
|
|
QVERIFY(clientB);
|
|
|
|
|
|
|
|
QByteArray buffer(16384, '@');
|
|
|
|
QTime stopWatch;
|
|
|
|
stopWatch.start();
|
|
|
|
qlonglong totalWritten = 0;
|
|
|
|
while (stopWatch.elapsed() < 5000) {
|
|
|
|
qlonglong writtenA = clientA.write(buffer.data(), buffer.size());
|
|
|
|
clientA.flush();
|
|
|
|
totalWritten += buffer.size();
|
|
|
|
while (clientB->waitForReadyRead(100)) {
|
|
|
|
if (clientB->bytesAvailable() == writtenA)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
clientB->read(buffer.data(), buffer.size());
|
|
|
|
qlonglong writtenB = clientB->write(buffer.data(), buffer.size());
|
|
|
|
clientB->flush();
|
|
|
|
totalWritten += buffer.size();
|
|
|
|
while (clientA.waitForReadyRead(100)) {
|
|
|
|
if (clientA.bytesAvailable() == writtenB)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
clientA.read(buffer.data(), buffer.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
qDebug("\t\t%s: %.1fMB/%.1fs: %.1fMB/s",
|
|
|
|
probeSocket.localAddress().toString().toLatin1().constData(),
|
|
|
|
totalWritten / (1024.0 * 1024.0),
|
|
|
|
stopWatch.elapsed() / 1000.0,
|
|
|
|
(totalWritten / (stopWatch.elapsed() / 1000.0)) / (1024 * 1024));
|
|
|
|
|
|
|
|
delete clientB;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTEST_MAIN(tst_QTcpServer)
|
|
|
|
#include "tst_qtcpserver.moc"
|