Commit c6c22b1663e18fae02e1012e34b0ecb0395abd3a

Authored by Wiebe Cazemier
1 parent 58550eac

Add test for incoming topic aliases

FlashMQTests/tst_maintests.cpp
... ... @@ -128,6 +128,8 @@ private slots:
128 128 void testMqtt5DelayedWill();
129 129 void testMqtt5DelayedWillAlwaysOnSessionEnd();
130 130  
  131 + void testIncomingTopicAlias();
  132 +
131 133 };
132 134  
133 135 MainTests::MainTests()
... ... @@ -1575,6 +1577,44 @@ void MainTests::testMqtt5DelayedWillAlwaysOnSessionEnd()
1575 1577 QCOMPARE(pubPack.getPublishData().qos, 0);
1576 1578 }
1577 1579  
  1580 +void MainTests::testIncomingTopicAlias()
  1581 +{
  1582 + FlashMQTestClient receiver;
  1583 + receiver.start();
  1584 + receiver.connectClient(ProtocolVersion::Mqtt5);
  1585 +
  1586 + receiver.subscribe("just/a/path", 0);
  1587 +
  1588 + FlashMQTestClient sender;
  1589 + sender.start();
  1590 + sender.connectClient(ProtocolVersion::Mqtt5);
  1591 +
  1592 + {
  1593 + Publish pub("just/a/path", "AAAAA", 0);
  1594 + pub.constructPropertyBuilder();
  1595 + pub.propertyBuilder->writeTopicAlias(1);
  1596 + sender.publish(pub);
  1597 + }
  1598 +
  1599 + {
  1600 + Publish pub2("", "BBBBB", 0);
  1601 + pub2.constructPropertyBuilder();
  1602 + pub2.propertyBuilder->writeTopicAlias(1);
  1603 + sender.publish(pub2);
  1604 + }
  1605 +
  1606 + receiver.waitForMessageCount(2);
  1607 +
  1608 + const MqttPacket &pack1 = receiver.receivedPublishes.at(0);
  1609 + const MqttPacket &pack2 = receiver.receivedPublishes.at(1);
  1610 +
  1611 + QCOMPARE(pack1.getTopic(), "just/a/path");
  1612 + QCOMPARE(pack1.getPayloadCopy(), "AAAAA");
  1613 +
  1614 + QCOMPARE(pack2.getTopic(), "just/a/path");
  1615 + QCOMPARE(pack2.getPayloadCopy(), "BBBBB");
  1616 +}
  1617 +
1578 1618  
1579 1619 int main(int argc, char *argv[])
1580 1620 {
... ...
flashmqtestclient.cpp
... ... @@ -186,19 +186,18 @@ void FlashMQTestClient::subscribe(const std::string topic, char qos)
186 186 }
187 187 }
188 188  
189   -void FlashMQTestClient::publish(const std::string &topic, const std::string &payload, char qos)
  189 +void FlashMQTestClient::publish(Publish &pub)
190 190 {
191 191 clearReceivedLists();
192 192  
193 193 const uint16_t packet_id = 77;
194 194  
195   - Publish pub(topic, payload, qos);
196 195 MqttPacket pubPack(client->getProtocolVersion(), pub);
197   - if (qos > 0)
  196 + if (pub.qos > 0)
198 197 pubPack.setPacketId(packet_id);
199 198 client->writeMqttPacketAndBlameThisClient(pubPack);
200 199  
201   - if (qos == 1)
  200 + if (pub.qos == 1)
202 201 {
203 202 waitForCondition([&]() {
204 203 return this->receivedPackets.size() == 1;
... ... @@ -213,7 +212,7 @@ void FlashMQTestClient::publish(const std::string &topic, const std::string &pay
213 212 if (pubAckPack.getPacketId() != packet_id || this->receivedPackets.size() != 1)
214 213 throw std::runtime_error("Packet ID mismatch on QoS 1 publish or packet count wrong.");
215 214 }
216   - else if (qos == 2)
  215 + else if (pub.qos == 2)
217 216 {
218 217 waitForCondition([&]() {
219 218 return this->receivedPackets.size() >= 2;
... ... @@ -235,6 +234,12 @@ void FlashMQTestClient::publish(const std::string &topic, const std::string &pay
235 234 }
236 235 }
237 236  
  237 +void FlashMQTestClient::publish(const std::string &topic, const std::string &payload, char qos)
  238 +{
  239 + Publish pub(topic, payload, qos);
  240 + publish(pub);
  241 +}
  242 +
238 243 void FlashMQTestClient::waitForQuit()
239 244 {
240 245 testServerWorkerThreadData->queueQuit();
... ...
flashmqtestclient.h
... ... @@ -26,8 +26,8 @@ class FlashMQTestClient
26 26  
27 27  
28 28 public:
29   - std::list<MqttPacket> receivedPackets;
30   - std::list<MqttPacket> receivedPublishes;
  29 + std::vector<MqttPacket> receivedPackets;
  30 + std::vector<MqttPacket> receivedPublishes;
31 31  
32 32 FlashMQTestClient();
33 33 ~FlashMQTestClient();
... ... @@ -37,6 +37,7 @@ public:
37 37 void connectClient(ProtocolVersion protocolVersion, bool clean_start, uint32_t session_expiry_interval);
38 38 void subscribe(const std::string topic, char qos);
39 39 void publish(const std::string &topic, const std::string &payload, char qos);
  40 + void publish(Publish &pub);
40 41 void clearReceivedLists();
41 42 void setWill(std::shared_ptr<WillPublish> &will);
42 43 void disconnect(ReasonCodes reason);
... ...