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
|
|
|
*************************************************************************************/
|
|
|
|
|
2014-06-14 14:22:40 +01:00
|
|
|
#include "../core/backends/lan/socketlinereader.h"
|
2015-07-05 14:23:53 +01:00
|
|
|
#include "../core/backends/lan/server.h"
|
2019-12-09 22:14:19 +00:00
|
|
|
#include "../core/qtcompat_p.h"
|
2014-03-06 23:36:49 +00:00
|
|
|
|
|
|
|
#include <QTest>
|
2015-07-05 14:23:53 +01:00
|
|
|
#include <QSslSocket>
|
2014-03-06 23:36:49 +00:00
|
|
|
#include <QProcess>
|
|
|
|
#include <QEventLoop>
|
|
|
|
#include <QTimer>
|
|
|
|
|
|
|
|
class TestSocketLineReader : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public Q_SLOTS:
|
|
|
|
void initTestCase();
|
2018-03-04 19:48:51 +00:00
|
|
|
void newPacket();
|
2014-03-06 23:36:49 +00:00
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
void socketLineReader();
|
|
|
|
|
|
|
|
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;
|
2017-09-03 20:39:44 +01:00
|
|
|
Server* m_server;
|
|
|
|
QSslSocket* m_conn;
|
|
|
|
SocketLineReader* m_reader;
|
2014-03-06 23:36:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void TestSocketLineReader::initTestCase()
|
|
|
|
{
|
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
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
m_timer.setInterval(4000);//For second is more enough to send some data via local socket
|
|
|
|
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;
|
2014-03-07 00:04:09 +00:00
|
|
|
dataToSend << "foobar\n" << "barfoo\n" << "foobar?\n" << "\n" << "barfoo!\n" << "panda\n";
|
2018-11-25 22:58:24 +00:00
|
|
|
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;
|
2017-09-03 20:39:44 +01:00
|
|
|
while(!m_server->hasPendingConnections() && maxAttemps > 0) {
|
2014-03-06 23:36:49 +00:00
|
|
|
--maxAttemps;
|
|
|
|
QTest::qSleep(1000);
|
|
|
|
}
|
|
|
|
|
2017-09-03 20:39:44 +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");
|
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(m_packets.count(), 5);//We expect 5 Packets
|
2014-03-06 23:36:49 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
void TestSocketLineReader::newPacket()
|
2014-03-06 23:36:49 +00:00
|
|
|
{
|
|
|
|
int maxLoops = 5;
|
2020-09-16 01:16:06 +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"
|