mirror of https://github.com/nymea/nymea-mqtt
Fix MqttPackdt payload assignment.
QByteArray(str) assumes that str is '\0'-terminated which is not the case when sending binary data.unit-test-for-connected
parent
a617e504fe
commit
718a495437
|
|
@ -522,8 +522,9 @@ int MqttPacket::parse(const QByteArray &buffer)
|
|||
}
|
||||
|
||||
memset(str, 0, MAX_STRLEN);
|
||||
inputStream.readRawData(str, qMin(MAX_STRLEN, remainingLength));
|
||||
d_ptr->payload = QByteArray(str);
|
||||
qint16 payloadLen = qMin(MAX_STRLEN, remainingLength);
|
||||
inputStream.readRawData(str, payloadLen);
|
||||
d_ptr->payload = QByteArray(str, payloadLen);
|
||||
break;
|
||||
}
|
||||
case TypePuback:
|
||||
|
|
|
|||
|
|
@ -82,6 +82,8 @@ private slots:
|
|||
|
||||
void testEmptyClientId();
|
||||
|
||||
void testBinaryPaylaod();
|
||||
|
||||
private:
|
||||
// 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);
|
||||
|
|
@ -820,6 +822,19 @@ void OperationTests::testEmptyClientId()
|
|||
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
|
||||
|
||||
QTEST_MAIN(OperationTests)
|
||||
|
|
|
|||
Loading…
Reference in New Issue