From 5c0ad3fb853ea37bc3c5cc8a80e742e9d9e50397 Mon Sep 17 00:00:00 2001 From: Richard Liebscher Date: Fri, 3 Jan 2020 15:48:48 +0100 Subject: [PATCH 1/4] Remove payload keys when no payload exists --- core/networkpacket.cpp | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/core/networkpacket.cpp b/core/networkpacket.cpp index 5da65afa8..82d8c09db 100644 --- a/core/networkpacket.cpp +++ b/core/networkpacket.cpp @@ -87,16 +87,11 @@ QVariantMap qobject2qvariant(const T* object) QByteArray NetworkPacket::serialize() const { //Object -> QVariant - //QVariantMap variant; - //variant["id"] = mId; - //variant["type"] = mType; - //variant["body"] = mBody; QVariantMap variant = qobject2qvariant(this); - if (hasPayload()) { - //qCDebug(KDECONNECT_CORE) << "Serializing payloadTransferInfo"; - variant[QStringLiteral("payloadSize")] = payloadSize(); - variant[QStringLiteral("payloadTransferInfo")] = m_payloadTransferInfo; + if (!hasPayload()) { + variant.remove(QStringLiteral("payloadSize")); + variant.remove(QStringLiteral("payloadTransferInfo")); } //QVariant -> json From f9f1eb6ed076a679c48028352f24190c2a85dbd8 Mon Sep 17 00:00:00 2001 From: Richard Liebscher Date: Fri, 3 Jan 2020 17:15:51 +0100 Subject: [PATCH 2/4] Added test --- CMakeLists.txt | 2 +- tests/networkpackettests.cpp | 35 +++++++++++++++++++++++++++++++++++ tests/networkpackettests.h | 1 + 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 51662bac1..1fcfdc4f0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ if (SAILFISHOS) set(KF5_MIN_VERSION "5.36.0") set(QT_MIN_VERSION "5.6.0") else() - set(KF5_MIN_VERSION "5.64.0") + set(KF5_MIN_VERSION "5.62.0") set(QT_MIN_VERSION "5.10.0") endif() set(QCA_MIN_VERSION "2.1.0") diff --git a/tests/networkpackettests.cpp b/tests/networkpackettests.cpp index 09d57d789..7cad0bf57 100644 --- a/tests/networkpackettests.cpp +++ b/tests/networkpackettests.cpp @@ -23,6 +23,7 @@ #include "core/networkpacket.h" #include +#include QTEST_GUILESS_MAIN(NetworkPacketTests); @@ -78,6 +79,40 @@ void NetworkPacketTests::networkPacketIdentityTest() } +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(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(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\"")); +} + void NetworkPacketTests::cleanupTestCase() { // Called after the last testfunction was executed diff --git a/tests/networkpackettests.h b/tests/networkpackettests.h index 0d91d6dde..931161ce0 100644 --- a/tests/networkpackettests.h +++ b/tests/networkpackettests.h @@ -32,6 +32,7 @@ private Q_SLOTS: void networkPacketTest(); void networkPacketIdentityTest(); + void networkPacketPayloadTest(); //void networkPacketEncryptionTest(); void cleanupTestCase(); From 10d2a6b912aaabace8729facb1375ebcd7099a6d Mon Sep 17 00:00:00 2001 From: Richard Liebscher Date: Fri, 3 Jan 2020 16:18:17 +0000 Subject: [PATCH 3/4] Unintended change --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1fcfdc4f0..51662bac1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ if (SAILFISHOS) set(KF5_MIN_VERSION "5.36.0") set(QT_MIN_VERSION "5.6.0") else() - set(KF5_MIN_VERSION "5.62.0") + set(KF5_MIN_VERSION "5.64.0") set(QT_MIN_VERSION "5.10.0") endif() set(QCA_MIN_VERSION "2.1.0") From 456d4830fe80651109869ef818f66b76341b1329 Mon Sep 17 00:00:00 2001 From: Richard Liebscher Date: Sat, 4 Jan 2020 13:34:48 +0100 Subject: [PATCH 4/4] Made code more explicit and additional tests. --- core/networkpacket.cpp | 24 +++++++----------------- tests/networkpackettests.cpp | 27 ++++++++++++++++++++------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/core/networkpacket.cpp b/core/networkpacket.cpp index 82d8c09db..ced10f816 100644 --- a/core/networkpacket.cpp +++ b/core/networkpacket.cpp @@ -71,27 +71,17 @@ void NetworkPacket::createIdentityPacket(NetworkPacket* np) //qCDebug(KDECONNECT_CORE) << "createIdentityPacket" << np->serialize(); } -template -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 = qobject2qvariant(this); + QVariantMap variant; + variant.insert(QStringLiteral("id"), m_id); + variant.insert(QStringLiteral("type"), m_type); + variant.insert(QStringLiteral("body"), m_body); - if (!hasPayload()) { - variant.remove(QStringLiteral("payloadSize")); - variant.remove(QStringLiteral("payloadTransferInfo")); + if (hasPayload()) { + variant.insert(QStringLiteral("payloadSize"), m_payloadSize); + variant.insert(QStringLiteral("payloadTransferInfo"), m_payloadTransferInfo); } //QVariant -> json diff --git a/tests/networkpackettests.cpp b/tests/networkpackettests.cpp index 7cad0bf57..e4e8082ab 100644 --- a/tests/networkpackettests.cpp +++ b/tests/networkpackettests.cpp @@ -82,9 +82,10 @@ void NetworkPacketTests::networkPacketIdentityTest() void NetworkPacketTests::networkPacketPayloadTest() { QByteArray json; + NetworkPacket np; // empty package - NetworkPacket np(QStringLiteral("com.test")); + np = NetworkPacket(QStringLiteral("com.test")); json = np.serialize(); qDebug() << json; QVERIFY(!json.contains("\"payloadSize\"")); @@ -93,10 +94,10 @@ void NetworkPacketTests::networkPacketPayloadTest() // package with payload QByteArray buffer("test data"); auto payload = QSharedPointer(new QBuffer(&buffer, this)); - NetworkPacket payloadNp(QStringLiteral("com.test")); - payloadNp.setPayload(payload, buffer.size()); + np = NetworkPacket(QStringLiteral("com.test")); + np.setPayload(payload, buffer.size()); - json = payloadNp.serialize(); + json = np.serialize(); qDebug() << json; QVERIFY(json.contains("\"payloadSize\":9")); QVERIFY(json.contains("\"payloadTransferInfo\"")); @@ -104,13 +105,25 @@ void NetworkPacketTests::networkPacketPayloadTest() // package with empty payload QByteArray emptyBuffer("test data"); auto emptyPayload = QSharedPointer(new QBuffer(&emptyBuffer, this)); - NetworkPacket emptyPayloadNp(QStringLiteral("com.test")); - emptyPayloadNp.setPayload(emptyPayload, 0); + np = NetworkPacket(QStringLiteral("com.test")); + np.setPayload(emptyPayload, 0); - json = emptyPayloadNp.serialize(); + 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()