Merge branch 'richardliebscher/kdeconnect-kde-fix-payloadTransferInfo-existence'
This commit is contained in:
commit
a6f9d90b3a
3 changed files with 55 additions and 21 deletions
|
@ -71,32 +71,17 @@ void NetworkPacket::createIdentityPacket(NetworkPacket* np)
|
|||
//qCDebug(KDECONNECT_CORE) << "createIdentityPacket" << np->serialize();
|
||||
}
|
||||
|
||||
template<class T>
|
||||
QVariantMap qobject2qvariant(const T* object)
|
||||
{
|
||||
QVariantMap map;
|
||||
auto metaObject = T::staticMetaObject;
|
||||
for(int i = metaObject.propertyOffset(); i < metaObject.propertyCount(); ++i) {
|
||||
QMetaProperty prop = metaObject.property(i);
|
||||
map.insert(QString::fromLatin1(prop.name()), prop.readOnGadget(object));
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
QByteArray NetworkPacket::serialize() const
|
||||
{
|
||||
//Object -> QVariant
|
||||
//QVariantMap variant;
|
||||
//variant["id"] = mId;
|
||||
//variant["type"] = mType;
|
||||
//variant["body"] = mBody;
|
||||
QVariantMap variant = qobject2qvariant(this);
|
||||
QVariantMap variant;
|
||||
variant.insert(QStringLiteral("id"), m_id);
|
||||
variant.insert(QStringLiteral("type"), m_type);
|
||||
variant.insert(QStringLiteral("body"), m_body);
|
||||
|
||||
if (hasPayload()) {
|
||||
//qCDebug(KDECONNECT_CORE) << "Serializing payloadTransferInfo";
|
||||
variant[QStringLiteral("payloadSize")] = payloadSize();
|
||||
variant[QStringLiteral("payloadTransferInfo")] = m_payloadTransferInfo;
|
||||
variant.insert(QStringLiteral("payloadSize"), m_payloadSize);
|
||||
variant.insert(QStringLiteral("payloadTransferInfo"), m_payloadTransferInfo);
|
||||
}
|
||||
|
||||
//QVariant -> json
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
#include "core/networkpacket.h"
|
||||
|
||||
#include <QtTest>
|
||||
#include <QBuffer>
|
||||
|
||||
QTEST_GUILESS_MAIN(NetworkPacketTests);
|
||||
|
||||
|
@ -78,6 +79,53 @@ void NetworkPacketTests::networkPacketIdentityTest()
|
|||
|
||||
}
|
||||
|
||||
void NetworkPacketTests::networkPacketPayloadTest()
|
||||
{
|
||||
QByteArray json;
|
||||
NetworkPacket np;
|
||||
|
||||
// empty package
|
||||
np = NetworkPacket(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));
|
||||
np = NetworkPacket(QStringLiteral("com.test"));
|
||||
np.setPayload(payload, buffer.size());
|
||||
|
||||
json = np.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));
|
||||
np = NetworkPacket(QStringLiteral("com.test"));
|
||||
np.setPayload(emptyPayload, 0);
|
||||
|
||||
json = np.serialize();
|
||||
qDebug() << json;
|
||||
QVERIFY(!json.contains("\"payloadSize\""));
|
||||
QVERIFY(!json.contains("\"payloadTransferInfo\""));
|
||||
|
||||
// incoming package without payload
|
||||
np = NetworkPacket();
|
||||
QVERIFY(NetworkPacket::unserialize(
|
||||
"{\"body\":{},\"id\":\"1578136807254\",\"type\":\"com.test\"}\n", &np));
|
||||
QVERIFY(!np.hasPayload());
|
||||
|
||||
// incoming package without payload (but with payload keys)
|
||||
np = NetworkPacket();
|
||||
QVERIFY(NetworkPacket::unserialize(
|
||||
"{\"body\":{},\"id\":\"1578136807254\",\"payloadSize\":0,\"payloadTransferInfo\":{},\"type\":\"com.test\"}\n", &np));
|
||||
QVERIFY(!np.hasPayload());
|
||||
}
|
||||
|
||||
void NetworkPacketTests::cleanupTestCase()
|
||||
{
|
||||
// Called after the last testfunction was executed
|
||||
|
|
|
@ -32,6 +32,7 @@ private Q_SLOTS:
|
|||
|
||||
void networkPacketTest();
|
||||
void networkPacketIdentityTest();
|
||||
void networkPacketPayloadTest();
|
||||
//void networkPacketEncryptionTest();
|
||||
|
||||
void cleanupTestCase();
|
||||
|
|
Loading…
Reference in a new issue