2014-03-06 23:36:49 +00:00
|
|
|
/*************************************************************************************
|
|
|
|
* Copyright (C) 2014 by Alejandro Fiestas Olivares <afiestas@kde.org> *
|
|
|
|
* *
|
|
|
|
* This library is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU Lesser General Public *
|
|
|
|
* License as published by the Free Software Foundation; either *
|
|
|
|
* version 2.1 of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This library is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
|
|
|
|
* Lesser General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public *
|
|
|
|
* License along with this library; if not, write to the Free Software *
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
|
|
|
|
*************************************************************************************/
|
|
|
|
|
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"
|
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>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
class TestSocketLineReader : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public Q_SLOTS:
|
|
|
|
void initTestCase();
|
|
|
|
void newPackage();
|
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
void socketLineReader();
|
|
|
|
|
|
|
|
private:
|
|
|
|
QTimer mTimer;
|
|
|
|
QEventLoop mLoop;
|
|
|
|
QList<QByteArray> mPackages;
|
2015-07-05 14:23:53 +01:00
|
|
|
Server *mServer;
|
|
|
|
QSslSocket *mConn;
|
2014-03-06 23:36:49 +00:00
|
|
|
SocketLineReader *mReader;
|
|
|
|
};
|
|
|
|
|
|
|
|
void TestSocketLineReader::initTestCase()
|
|
|
|
{
|
2015-07-05 14:23:53 +01:00
|
|
|
mServer = new Server(this);
|
|
|
|
|
2014-03-06 23:36:49 +00:00
|
|
|
QVERIFY2(mServer->listen(QHostAddress::LocalHost, 8694), "Failed to create local tcp server");
|
|
|
|
|
|
|
|
mTimer.setInterval(4000);//For second is more enough to send some data via local socket
|
|
|
|
mTimer.setSingleShot(true);
|
|
|
|
connect(&mTimer, SIGNAL(timeout()), &mLoop, SLOT(quit()));
|
|
|
|
|
2015-07-05 14:23:53 +01:00
|
|
|
mConn = new QSslSocket(this);
|
2014-03-06 23:36:49 +00:00
|
|
|
mConn->connectToHost(QHostAddress::LocalHost, 8694);
|
|
|
|
connect(mConn, SIGNAL(connected()), &mLoop, SLOT(quit()));
|
|
|
|
mTimer.start();
|
|
|
|
mLoop.exec();
|
|
|
|
|
|
|
|
QVERIFY2(mConn->isOpen(), "Could not connect to local tcp server");
|
|
|
|
}
|
|
|
|
|
|
|
|
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";
|
2014-03-06 23:36:49 +00:00
|
|
|
Q_FOREACH(const QByteArray &line, dataToSend) {
|
|
|
|
mConn->write(line);
|
|
|
|
}
|
|
|
|
mConn->flush();
|
|
|
|
|
|
|
|
int maxAttemps = 5;
|
|
|
|
while(!mServer->hasPendingConnections() && maxAttemps > 0) {
|
|
|
|
--maxAttemps;
|
|
|
|
QTest::qSleep(1000);
|
|
|
|
}
|
|
|
|
|
2015-07-05 14:23:53 +01:00
|
|
|
QSslSocket *sock = (QSslSocket*) mServer->nextPendingConnection();
|
2014-07-07 11:45:13 +01:00
|
|
|
|
|
|
|
QVERIFY2(sock != 0, "Could not open a connection to the client");
|
|
|
|
|
2014-03-06 23:36:49 +00:00
|
|
|
mReader = new SocketLineReader(sock, this);
|
|
|
|
connect(mReader, SIGNAL(readyRead()), SLOT(newPackage()));
|
|
|
|
mTimer.start();
|
|
|
|
mLoop.exec();
|
|
|
|
|
2014-04-12 22:07:40 +01:00
|
|
|
/* remove the empty line before compare */
|
|
|
|
dataToSend.removeOne("\n");
|
|
|
|
|
2014-03-06 23:36:49 +00:00
|
|
|
QCOMPARE(mPackages.count(), 5);//We expect 5 Packages
|
|
|
|
for(int x = 0;x < 5; ++x) {
|
|
|
|
QCOMPARE(mPackages[x], dataToSend[x]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestSocketLineReader::newPackage()
|
|
|
|
{
|
|
|
|
if (!mReader->bytesAvailable()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int maxLoops = 5;
|
|
|
|
while(mReader->bytesAvailable() > 0 && maxLoops > 0) {
|
|
|
|
--maxLoops;
|
|
|
|
const QByteArray package = mReader->readLine();
|
|
|
|
if (!package.isEmpty()) {
|
|
|
|
mPackages.append(package);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mPackages.count() == 5) {
|
|
|
|
mLoop.exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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"
|