From c6c22b1663e18fae02e1012e34b0ecb0395abd3a Mon Sep 17 00:00:00 2001 From: Wiebe Cazemier Date: Sun, 24 Jul 2022 20:28:41 +0200 Subject: [PATCH] Add test for incoming topic aliases --- FlashMQTests/tst_maintests.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ flashmqtestclient.cpp | 15 ++++++++++----- flashmqtestclient.h | 5 +++-- 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/FlashMQTests/tst_maintests.cpp b/FlashMQTests/tst_maintests.cpp index 208ced9..39c0b5d 100644 --- a/FlashMQTests/tst_maintests.cpp +++ b/FlashMQTests/tst_maintests.cpp @@ -128,6 +128,8 @@ private slots: void testMqtt5DelayedWill(); void testMqtt5DelayedWillAlwaysOnSessionEnd(); + void testIncomingTopicAlias(); + }; MainTests::MainTests() @@ -1575,6 +1577,44 @@ void MainTests::testMqtt5DelayedWillAlwaysOnSessionEnd() QCOMPARE(pubPack.getPublishData().qos, 0); } +void MainTests::testIncomingTopicAlias() +{ + FlashMQTestClient receiver; + receiver.start(); + receiver.connectClient(ProtocolVersion::Mqtt5); + + receiver.subscribe("just/a/path", 0); + + FlashMQTestClient sender; + sender.start(); + sender.connectClient(ProtocolVersion::Mqtt5); + + { + Publish pub("just/a/path", "AAAAA", 0); + pub.constructPropertyBuilder(); + pub.propertyBuilder->writeTopicAlias(1); + sender.publish(pub); + } + + { + Publish pub2("", "BBBBB", 0); + pub2.constructPropertyBuilder(); + pub2.propertyBuilder->writeTopicAlias(1); + sender.publish(pub2); + } + + receiver.waitForMessageCount(2); + + const MqttPacket &pack1 = receiver.receivedPublishes.at(0); + const MqttPacket &pack2 = receiver.receivedPublishes.at(1); + + QCOMPARE(pack1.getTopic(), "just/a/path"); + QCOMPARE(pack1.getPayloadCopy(), "AAAAA"); + + QCOMPARE(pack2.getTopic(), "just/a/path"); + QCOMPARE(pack2.getPayloadCopy(), "BBBBB"); +} + int main(int argc, char *argv[]) { diff --git a/flashmqtestclient.cpp b/flashmqtestclient.cpp index 1951263..3f56610 100644 --- a/flashmqtestclient.cpp +++ b/flashmqtestclient.cpp @@ -186,19 +186,18 @@ void FlashMQTestClient::subscribe(const std::string topic, char qos) } } -void FlashMQTestClient::publish(const std::string &topic, const std::string &payload, char qos) +void FlashMQTestClient::publish(Publish &pub) { clearReceivedLists(); const uint16_t packet_id = 77; - Publish pub(topic, payload, qos); MqttPacket pubPack(client->getProtocolVersion(), pub); - if (qos > 0) + if (pub.qos > 0) pubPack.setPacketId(packet_id); client->writeMqttPacketAndBlameThisClient(pubPack); - if (qos == 1) + if (pub.qos == 1) { waitForCondition([&]() { return this->receivedPackets.size() == 1; @@ -213,7 +212,7 @@ void FlashMQTestClient::publish(const std::string &topic, const std::string &pay if (pubAckPack.getPacketId() != packet_id || this->receivedPackets.size() != 1) throw std::runtime_error("Packet ID mismatch on QoS 1 publish or packet count wrong."); } - else if (qos == 2) + else if (pub.qos == 2) { waitForCondition([&]() { return this->receivedPackets.size() >= 2; @@ -235,6 +234,12 @@ void FlashMQTestClient::publish(const std::string &topic, const std::string &pay } } +void FlashMQTestClient::publish(const std::string &topic, const std::string &payload, char qos) +{ + Publish pub(topic, payload, qos); + publish(pub); +} + void FlashMQTestClient::waitForQuit() { testServerWorkerThreadData->queueQuit(); diff --git a/flashmqtestclient.h b/flashmqtestclient.h index 1bc046d..d5c7bfd 100644 --- a/flashmqtestclient.h +++ b/flashmqtestclient.h @@ -26,8 +26,8 @@ class FlashMQTestClient public: - std::list receivedPackets; - std::list receivedPublishes; + std::vector receivedPackets; + std::vector receivedPublishes; FlashMQTestClient(); ~FlashMQTestClient(); @@ -37,6 +37,7 @@ public: void connectClient(ProtocolVersion protocolVersion, bool clean_start, uint32_t session_expiry_interval); void subscribe(const std::string topic, char qos); void publish(const std::string &topic, const std::string &payload, char qos); + void publish(Publish &pub); void clearReceivedLists(); void setWill(std::shared_ptr &will); void disconnect(ReasonCodes reason); -- libgit2 0.21.4