2014-03-06 23:36:49 +00:00
|
|
|
/*************************************************************************************
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2014 Alejandro Fiestas Olivares <afiestas@kde.org> *
|
2014-03-06 23:36:49 +00:00
|
|
|
* *
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: LGPL-2.1-or-later
|
2014-03-06 23:36:49 +00:00
|
|
|
*************************************************************************************/
|
|
|
|
|
2015-07-05 14:23:53 +01:00
|
|
|
#include "../core/backends/lan/server.h"
|
2022-09-10 22:23:52 +01:00
|
|
|
#include "../core/backends/lan/socketlinereader.h"
|
2019-12-09 22:14:19 +00:00
|
|
|
#include "../core/qtcompat_p.h"
|
2014-03-06 23:36:49 +00:00
|
|
|
|
|
|
|
#include <QEventLoop>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QProcess>
|
2020-09-16 01:27:13 +01:00
|
|
|
#include <QSignalSpy>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QSslSocket>
|
|
|
|
#include <QTest>
|
|
|
|
#include <QTimer>
|
2014-03-06 23:36:49 +00:00
|
|
|
|
|
|
|
class TestSocketLineReader : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public Q_SLOTS:
|
2020-09-16 01:27:13 +01:00
|
|
|
void init();
|
2022-09-10 22:23:52 +01:00
|
|
|
void cleanup()
|
|
|
|
{
|
|
|
|
delete m_server;
|
|
|
|
}
|
2018-03-04 19:48:51 +00:00
|
|
|
void newPacket();
|
2014-03-06 23:36:49 +00:00
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
void socketLineReader();
|
2020-09-16 01:27:13 +01:00
|
|
|
void badData();
|
2014-03-06 23:36:49 +00:00
|
|
|
|
|
|
|
private:
|
2017-09-03 20:39:44 +01:00
|
|
|
QTimer m_timer;
|
|
|
|
QEventLoop m_loop;
|
2018-03-04 19:48:51 +00:00
|
|
|
QList<QByteArray> m_packets;
|
2022-09-10 22:23:52 +01:00
|
|
|
Server *m_server;
|
|
|
|
QSslSocket *m_conn;
|
|
|
|
SocketLineReader *m_reader;
|
2014-03-06 23:36:49 +00:00
|
|
|
};
|
|
|
|
|
2020-09-16 01:27:13 +01:00
|
|
|
void TestSocketLineReader::init()
|
2014-03-06 23:36:49 +00:00
|
|
|
{
|
2020-09-16 01:27:13 +01:00
|
|
|
m_packets.clear();
|
2017-09-03 20:39:44 +01:00
|
|
|
m_server = new Server(this);
|
2015-07-05 14:23:53 +01:00
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
QVERIFY2(m_server->listen(QHostAddress::LocalHost, 8694), "Failed to create local tcp server");
|
2014-03-06 23:36:49 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
m_timer.setInterval(4000); // For second is more enough to send some data via local socket
|
2017-09-03 20:39:44 +01:00
|
|
|
m_timer.setSingleShot(true);
|
|
|
|
connect(&m_timer, &QTimer::timeout, &m_loop, &QEventLoop::quit);
|
2014-03-06 23:36:49 +00:00
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
m_conn = new QSslSocket(this);
|
|
|
|
m_conn->connectToHost(QHostAddress::LocalHost, 8694);
|
|
|
|
connect(m_conn, &QAbstractSocket::connected, &m_loop, &QEventLoop::quit);
|
|
|
|
m_timer.start();
|
|
|
|
m_loop.exec();
|
2014-03-06 23:36:49 +00:00
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
QVERIFY2(m_conn->isOpen(), "Could not connect to local tcp server");
|
2014-03-06 23:36:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestSocketLineReader::socketLineReader()
|
|
|
|
{
|
|
|
|
QList<QByteArray> dataToSend;
|
2022-09-10 22:23:52 +01:00
|
|
|
dataToSend << "foobar\n"
|
|
|
|
<< "barfoo\n"
|
|
|
|
<< "foobar?\n"
|
|
|
|
<< "\n"
|
|
|
|
<< "barfoo!\n"
|
|
|
|
<< "panda\n";
|
|
|
|
for (const QByteArray &line : qAsConst(dataToSend)) {
|
2017-09-03 20:39:44 +01:00
|
|
|
m_conn->write(line);
|
2014-03-06 23:36:49 +00:00
|
|
|
}
|
2017-09-03 20:39:44 +01:00
|
|
|
m_conn->flush();
|
2014-03-06 23:36:49 +00:00
|
|
|
|
|
|
|
int maxAttemps = 5;
|
2022-09-10 22:23:52 +01:00
|
|
|
while (!m_server->hasPendingConnections() && maxAttemps > 0) {
|
2014-03-06 23:36:49 +00:00
|
|
|
--maxAttemps;
|
|
|
|
QTest::qSleep(1000);
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QSslSocket *sock = m_server->nextPendingConnection();
|
2014-07-07 11:45:13 +01:00
|
|
|
|
2015-09-08 09:46:59 +01:00
|
|
|
QVERIFY2(sock != nullptr, "Could not open a connection to the client");
|
2014-07-07 11:45:13 +01:00
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
m_reader = new SocketLineReader(sock, this);
|
2018-03-04 19:48:51 +00:00
|
|
|
connect(m_reader, &SocketLineReader::readyRead, this, &TestSocketLineReader::newPacket);
|
2017-09-03 20:39:44 +01:00
|
|
|
m_timer.start();
|
|
|
|
m_loop.exec();
|
2014-03-06 23:36:49 +00:00
|
|
|
|
2014-04-12 22:07:40 +01:00
|
|
|
/* remove the empty line before compare */
|
|
|
|
dataToSend.removeOne("\n");
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QCOMPARE(m_packets.count(), 5); // We expect 5 Packets
|
|
|
|
for (int x = 0; x < 5; ++x) {
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(m_packets[x], dataToSend[x]);
|
2014-03-06 23:36:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-16 01:27:13 +01:00
|
|
|
void TestSocketLineReader::badData()
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
const QList<QByteArray> dataToSend = {"data1\n", "data"}; // does not end in a \n
|
|
|
|
for (const QByteArray &line : qAsConst(dataToSend)) {
|
2020-09-16 01:27:13 +01:00
|
|
|
m_conn->write(line);
|
|
|
|
}
|
|
|
|
m_conn->flush();
|
|
|
|
|
|
|
|
QSignalSpy spy(m_server, &QTcpServer::newConnection);
|
|
|
|
QVERIFY(m_server->hasPendingConnections() || spy.wait(1000));
|
2022-09-10 22:23:52 +01:00
|
|
|
QSslSocket *sock = m_server->nextPendingConnection();
|
2020-09-16 01:27:13 +01:00
|
|
|
|
|
|
|
QVERIFY2(sock != nullptr, "Could not open a connection to the client");
|
|
|
|
|
|
|
|
m_reader = new SocketLineReader(sock, this);
|
|
|
|
connect(m_reader, &SocketLineReader::readyRead, this, &TestSocketLineReader::newPacket);
|
|
|
|
m_timer.start();
|
|
|
|
m_loop.exec();
|
|
|
|
|
|
|
|
QCOMPARE(m_packets.count(), 1);
|
|
|
|
QCOMPARE(m_packets[0], dataToSend[0]);
|
|
|
|
}
|
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
void TestSocketLineReader::newPacket()
|
2014-03-06 23:36:49 +00:00
|
|
|
{
|
|
|
|
int maxLoops = 5;
|
2022-09-10 22:23:52 +01:00
|
|
|
while (m_reader->hasPacketsAvailable() && maxLoops > 0) {
|
2014-03-06 23:36:49 +00:00
|
|
|
--maxLoops;
|
2018-03-04 19:48:51 +00:00
|
|
|
const QByteArray packet = m_reader->readLine();
|
|
|
|
if (!packet.isEmpty()) {
|
|
|
|
m_packets.append(packet);
|
2014-03-06 23:36:49 +00:00
|
|
|
}
|
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
if (m_packets.count() == 5) {
|
2017-09-03 20:39:44 +01:00
|
|
|
m_loop.exit();
|
2014-03-06 23:36:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 17:24:16 +00:00
|
|
|
QTEST_GUILESS_MAIN(TestSocketLineReader)
|
2014-03-06 23:36:49 +00:00
|
|
|
|
2014-06-14 14:22:40 +01:00
|
|
|
#include "testsocketlinereader.moc"
|