Fix MqttPackdt payload assignment.

QByteArray(str) assumes that str is '\0'-terminated
which is not the case when sending binary data.
This commit is contained in:
Timon 2021-02-04 13:19:51 +01:00
parent a617e504fe
commit 718a495437
2 changed files with 18 additions and 2 deletions

View File

@ -522,8 +522,9 @@ int MqttPacket::parse(const QByteArray &buffer)
} }
memset(str, 0, MAX_STRLEN); memset(str, 0, MAX_STRLEN);
inputStream.readRawData(str, qMin(MAX_STRLEN, remainingLength)); qint16 payloadLen = qMin(MAX_STRLEN, remainingLength);
d_ptr->payload = QByteArray(str); inputStream.readRawData(str, payloadLen);
d_ptr->payload = QByteArray(str, payloadLen);
break; break;
} }
case TypePuback: case TypePuback:

View File

@ -82,6 +82,8 @@ private slots:
void testEmptyClientId(); void testEmptyClientId();
void testBinaryPaylaod();
private: private:
// Connects and waits for the MQTT CONNECT to be finished // Connects and waits for the MQTT CONNECT to be finished
MqttClient *connectAndWait(const QString &clientId, bool cleanSession = true, quint16 keepAlive = 300, const QString &willTopic = QString(), const QString &willMessage = QString(), Mqtt::QoS willQoS = Mqtt::QoS0, bool willRetain = false); MqttClient *connectAndWait(const QString &clientId, bool cleanSession = true, quint16 keepAlive = 300, const QString &willTopic = QString(), const QString &willMessage = QString(), Mqtt::QoS willQoS = Mqtt::QoS0, bool willRetain = false);
@ -820,6 +822,19 @@ void OperationTests::testEmptyClientId()
QTRY_VERIFY2(client3.first->isConnected() == false, "Connection should have been dropped"); QTRY_VERIFY2(client3.first->isConnected() == false, "Connection should have been dropped");
} }
void OperationTests::testBinaryPaylaod()
{
MqttClient *client = connectAndWait("");
QVERIFY2(client->isConnected(), "Client did not connect");
client->subscribe("#");
const char binData[] = {'\xA5', '\x20', '\x00', '\x04', '\x00', '\x52'};
QByteArray payload(QByteArray::fromRawData(binData, 6));
QSignalSpy publishReceivedSpy(client, &MqttClient::publishReceived);
client->publish("testtopic", payload);
QTRY_VERIFY2(publishReceivedSpy.count() == 1, "Did not receive publish message");
QCOMPARE(publishReceivedSpy.first().at(1).toByteArray(), payload);
}
#endif #endif
QTEST_MAIN(OperationTests) QTEST_MAIN(OperationTests)