2013-08-20 13:05:22 +01:00
|
|
|
/**
|
|
|
|
* Copyright 2013 Albert Vaca <albertvaka@gmail.com>
|
2013-06-06 04:57:06 +01:00
|
|
|
*
|
2013-08-20 13:05:22 +01:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License or (at your option) version 3 or any later version
|
|
|
|
* accepted by the membership of KDE e.V. (or its successor approved
|
|
|
|
* by the membership of KDE e.V.), which shall act as a proxy
|
|
|
|
* defined in Section 14 of version 3 of the license.
|
2013-06-06 04:57:06 +01:00
|
|
|
*
|
2013-08-20 13:05:22 +01:00
|
|
|
* This program 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 General Public License for more details.
|
2013-06-06 04:57:06 +01:00
|
|
|
*
|
2013-08-20 13:05:22 +01:00
|
|
|
* You should have received a copy of the GNU General Public License
|
2019-03-23 16:29:26 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2013-06-06 04:57:06 +01:00
|
|
|
*/
|
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
#include "networkpackettests.h"
|
2013-06-06 04:57:06 +01:00
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
#include "core/networkpacket.h"
|
2013-07-04 02:34:35 +01:00
|
|
|
|
|
|
|
#include <QtTest>
|
2020-01-03 16:15:51 +00:00
|
|
|
#include <QBuffer>
|
2013-06-06 04:57:06 +01:00
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
QTEST_GUILESS_MAIN(NetworkPacketTests);
|
2013-06-06 04:57:06 +01:00
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
void NetworkPacketTests::initTestCase()
|
2013-06-06 04:57:06 +01:00
|
|
|
{
|
|
|
|
// Called before the first testfunction is executed
|
|
|
|
}
|
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
void NetworkPacketTests::networkPacketTest()
|
2013-07-04 02:34:35 +01:00
|
|
|
{
|
2018-03-04 19:48:51 +00:00
|
|
|
NetworkPacket np(QStringLiteral("com.test"));
|
2013-07-04 02:34:35 +01:00
|
|
|
|
2019-06-10 15:40:28 +01:00
|
|
|
np.set(QStringLiteral("hello"), QStringLiteral("hola"));
|
|
|
|
QCOMPARE( (np.get<QString>(QStringLiteral("hello"), QStringLiteral("bye"))) , QStringLiteral("hola") );
|
2013-07-04 02:34:35 +01:00
|
|
|
|
2019-06-10 15:40:28 +01:00
|
|
|
np.set(QStringLiteral("hello"), QString());
|
|
|
|
QCOMPARE((np.get<QString>(QStringLiteral("hello"), QStringLiteral("bye"))) , QString());
|
2013-07-04 02:34:35 +01:00
|
|
|
|
2016-11-26 14:38:08 +00:00
|
|
|
np.body().remove(QStringLiteral("hello"));
|
2019-06-10 15:40:28 +01:00
|
|
|
QCOMPARE((np.get<QString>(QStringLiteral("hello"), QStringLiteral("bye"))) , QStringLiteral("bye"));
|
2013-07-04 02:34:35 +01:00
|
|
|
|
2019-06-10 15:40:28 +01:00
|
|
|
np.set(QStringLiteral("foo"), QStringLiteral("bar"));
|
2013-07-04 02:34:35 +01:00
|
|
|
QByteArray ba = np.serialize();
|
2018-03-04 19:48:51 +00:00
|
|
|
//qDebug() << "Serialized packet:" << ba;
|
|
|
|
NetworkPacket np2(QLatin1String(""));
|
|
|
|
NetworkPacket::unserialize(ba,&np2);
|
2013-07-04 02:34:35 +01:00
|
|
|
|
|
|
|
QCOMPARE( np.id(), np2.id() );
|
|
|
|
QCOMPARE( np.type(), np2.type() );
|
|
|
|
QCOMPARE( np.body(), np2.body() );
|
|
|
|
|
2014-09-12 23:49:56 +01:00
|
|
|
QByteArray json("{\"id\":\"123\",\"type\":\"test\",\"body\":{\"testing\":true}}");
|
2013-07-04 02:34:35 +01:00
|
|
|
//qDebug() << json;
|
2018-03-04 19:48:51 +00:00
|
|
|
NetworkPacket::unserialize(json,&np2);
|
2019-06-10 15:40:28 +01:00
|
|
|
QCOMPARE( np2.id(), QStringLiteral("123") );
|
|
|
|
QCOMPARE( (np2.get<bool>(QStringLiteral("testing"))), true );
|
|
|
|
QCOMPARE( (np2.get<bool>(QStringLiteral("not_testing"))), false );
|
|
|
|
QCOMPARE( (np2.get<bool>(QStringLiteral("not_testing"),true)), true );
|
2013-07-04 02:34:35 +01:00
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
//NetworkPacket::unserialize("this is not json",&np2);
|
2013-07-04 02:34:35 +01:00
|
|
|
//QtTest::ignoreMessage(QtSystemMsg, "json_parser - syntax error found, forcing abort, Line 1 Column 0");
|
|
|
|
//QtTest::ignoreMessage(QtDebugMsg, "Unserialization error: 1 \"syntax error, unexpected string\"");
|
2013-09-01 21:13:03 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
void NetworkPacketTests::networkPacketIdentityTest()
|
2013-09-03 01:14:55 +01:00
|
|
|
{
|
2018-03-04 19:48:51 +00:00
|
|
|
NetworkPacket np(QLatin1String(""));
|
|
|
|
NetworkPacket::createIdentityPacket(&np);
|
2013-09-03 01:14:55 +01:00
|
|
|
|
2019-06-10 15:40:28 +01:00
|
|
|
QCOMPARE( np.get<int>(QStringLiteral("protocolVersion"), -1) , NetworkPacket::s_protocolVersion );
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE( np.type() , PACKET_TYPE_IDENTITY );
|
2013-09-03 01:14:55 +01:00
|
|
|
|
|
|
|
}
|
2013-07-04 02:34:35 +01:00
|
|
|
|
2020-01-03 16:15:51 +00:00
|
|
|
void NetworkPacketTests::networkPacketPayloadTest()
|
|
|
|
{
|
|
|
|
QByteArray json;
|
|
|
|
|
|
|
|
// empty package
|
|
|
|
NetworkPacket np(QStringLiteral("com.test"));
|
|
|
|
json = np.serialize();
|
|
|
|
qDebug() << json;
|
|
|
|
QVERIFY(!json.contains("\"payloadSize\""));
|
|
|
|
QVERIFY(!json.contains("\"payloadTransferInfo\""));
|
|
|
|
|
|
|
|
// package with payload
|
|
|
|
QByteArray buffer("test data");
|
|
|
|
auto payload = QSharedPointer<QIODevice>(new QBuffer(&buffer, this));
|
|
|
|
NetworkPacket payloadNp(QStringLiteral("com.test"));
|
|
|
|
payloadNp.setPayload(payload, buffer.size());
|
|
|
|
|
|
|
|
json = payloadNp.serialize();
|
|
|
|
qDebug() << json;
|
|
|
|
QVERIFY(json.contains("\"payloadSize\":9"));
|
|
|
|
QVERIFY(json.contains("\"payloadTransferInfo\""));
|
|
|
|
|
|
|
|
// package with empty payload
|
|
|
|
QByteArray emptyBuffer("test data");
|
|
|
|
auto emptyPayload = QSharedPointer<QIODevice>(new QBuffer(&emptyBuffer, this));
|
|
|
|
NetworkPacket emptyPayloadNp(QStringLiteral("com.test"));
|
|
|
|
emptyPayloadNp.setPayload(emptyPayload, 0);
|
|
|
|
|
|
|
|
json = emptyPayloadNp.serialize();
|
|
|
|
qDebug() << json;
|
|
|
|
QVERIFY(!json.contains("\"payloadSize\""));
|
|
|
|
QVERIFY(!json.contains("\"payloadTransferInfo\""));
|
|
|
|
}
|
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
void NetworkPacketTests::cleanupTestCase()
|
2013-06-06 04:57:06 +01:00
|
|
|
{
|
|
|
|
// Called after the last testfunction was executed
|
|
|
|
}
|
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
void NetworkPacketTests::init()
|
2013-06-06 04:57:06 +01:00
|
|
|
{
|
|
|
|
// Called before each testfunction is executed
|
|
|
|
}
|
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
void NetworkPacketTests::cleanup()
|
2013-06-06 04:57:06 +01:00
|
|
|
{
|
|
|
|
// Called after every testfunction
|
|
|
|
}
|
|
|
|
|