2024-01-15 08:48:34 +01:00
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
|
2024-01-17 07:57:48 +01:00
|
|
|
#include <boost/asio/detached.hpp>
|
2024-01-15 08:48:34 +01:00
|
|
|
#include <boost/asio/io_context.hpp>
|
|
|
|
|
|
|
|
|
|
#include <async_mqtt5/mqtt_client.hpp>
|
|
|
|
|
|
|
|
|
|
#include "test_common/message_exchange.hpp"
|
2024-01-26 09:39:01 +01:00
|
|
|
#include "test_common/packet_util.hpp"
|
|
|
|
|
#include "test_common/test_broker.hpp"
|
2024-01-15 08:48:34 +01:00
|
|
|
#include "test_common/test_service.hpp"
|
|
|
|
|
#include "test_common/test_stream.hpp"
|
|
|
|
|
|
|
|
|
|
using namespace async_mqtt5;
|
|
|
|
|
|
2024-01-23 11:46:00 +01:00
|
|
|
BOOST_AUTO_TEST_SUITE(receive_publish/*, *boost::unit_test::disabled()*/)
|
2024-01-15 08:48:34 +01:00
|
|
|
|
2024-01-17 15:13:56 +01:00
|
|
|
struct shared_test_data {
|
|
|
|
|
error_code success {};
|
|
|
|
|
error_code fail = asio::error::not_connected;
|
2024-01-15 08:48:34 +01:00
|
|
|
|
2024-01-17 15:13:56 +01:00
|
|
|
const std::string connect = encoders::encode_connect(
|
2024-01-15 08:48:34 +01:00
|
|
|
"", std::nullopt, std::nullopt, 10, false, {}, std::nullopt
|
|
|
|
|
);
|
2024-01-17 15:13:56 +01:00
|
|
|
const std::string connack = encoders::encode_connack(
|
|
|
|
|
true, reason_codes::success.value(), {}
|
2024-01-15 08:48:34 +01:00
|
|
|
);
|
|
|
|
|
|
2024-01-17 15:13:56 +01:00
|
|
|
const std::string topic = "topic";
|
|
|
|
|
const std::string payload = "payload";
|
|
|
|
|
|
|
|
|
|
const std::string publish_qos0 = encoders::encode_publish(
|
|
|
|
|
0, topic, payload, qos_e::at_most_once, retain_e::no, dup_e::no, {}
|
|
|
|
|
);
|
|
|
|
|
const std::string publish_qos1 = encoders::encode_publish(
|
|
|
|
|
1, topic, payload, qos_e::at_least_once, retain_e::no, dup_e::no, {}
|
|
|
|
|
);
|
|
|
|
|
const std::string publish_qos2 = encoders::encode_publish(
|
|
|
|
|
1, topic, payload, qos_e::exactly_once, retain_e::no, dup_e::no, {}
|
2024-01-15 08:48:34 +01:00
|
|
|
);
|
|
|
|
|
|
2024-01-17 15:13:56 +01:00
|
|
|
const std::string puback = encoders::encode_puback(1, uint8_t(0x00), {});
|
2024-01-15 08:48:34 +01:00
|
|
|
|
2024-01-17 15:13:56 +01:00
|
|
|
const std::string pubrec = encoders::encode_pubrec(1, uint8_t(0x00), {});
|
|
|
|
|
const std::string pubrel = encoders::encode_pubrel(1, uint8_t(0x00), {});
|
|
|
|
|
const std::string pubcomp = encoders::encode_pubcomp(1, uint8_t(0x00), {});
|
|
|
|
|
};
|
2024-01-15 08:48:34 +01:00
|
|
|
|
2024-01-17 15:13:56 +01:00
|
|
|
using test::after;
|
2024-01-19 13:45:09 +01:00
|
|
|
using namespace std::chrono;
|
2024-01-15 08:48:34 +01:00
|
|
|
|
2024-01-17 15:13:56 +01:00
|
|
|
void run_test(test::msg_exchange broker_side) {
|
|
|
|
|
constexpr int expected_handlers_called = 1;
|
|
|
|
|
int handlers_called = 0;
|
2024-01-15 08:48:34 +01:00
|
|
|
|
|
|
|
|
asio::io_context ioc;
|
|
|
|
|
auto executor = ioc.get_executor();
|
|
|
|
|
auto& broker = asio::make_service<test::test_broker>(
|
|
|
|
|
ioc, executor, std::move(broker_side)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
using client_type = mqtt_client<test::test_stream>;
|
|
|
|
|
client_type c(executor, "");
|
2024-01-17 15:13:56 +01:00
|
|
|
c.brokers("127.0.0.1,127.0.0.1") // to avoid reconnect backoff
|
2024-01-16 13:04:21 +01:00
|
|
|
.async_run(asio::detached);
|
2024-01-15 08:48:34 +01:00
|
|
|
|
2024-01-23 11:46:00 +01:00
|
|
|
c.async_receive([&handlers_called, &c](
|
|
|
|
|
error_code ec, std::string rec_topic, std::string rec_payload, publish_props
|
|
|
|
|
){
|
2024-01-15 08:48:34 +01:00
|
|
|
++handlers_called;
|
|
|
|
|
|
2024-01-23 11:46:00 +01:00
|
|
|
auto data = shared_test_data();
|
2024-01-15 08:48:34 +01:00
|
|
|
BOOST_CHECK_MESSAGE(!ec, ec.message());
|
2024-01-23 11:46:00 +01:00
|
|
|
BOOST_CHECK_EQUAL(data.topic, rec_topic);
|
|
|
|
|
BOOST_CHECK_EQUAL(data.payload, rec_payload);
|
2024-01-15 08:48:34 +01:00
|
|
|
|
|
|
|
|
c.cancel();
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2024-01-23 11:46:00 +01:00
|
|
|
ioc.run_for(3s);
|
2024-01-15 08:48:34 +01:00
|
|
|
BOOST_CHECK_EQUAL(handlers_called, expected_handlers_called);
|
|
|
|
|
BOOST_CHECK(broker.received_all_expected());
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-23 11:46:00 +01:00
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_CASE(receive_publish_qos0, shared_test_data) {
|
2024-01-17 15:13:56 +01:00
|
|
|
test::msg_exchange broker_side;
|
|
|
|
|
broker_side
|
|
|
|
|
.expect(connect)
|
|
|
|
|
.complete_with(success, after(0ms))
|
|
|
|
|
.reply_with(connack, after(0ms))
|
|
|
|
|
.send(publish_qos0, after(10ms));
|
|
|
|
|
|
|
|
|
|
run_test(std::move(broker_side));
|
2024-01-15 08:48:34 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-23 11:46:00 +01:00
|
|
|
BOOST_FIXTURE_TEST_CASE(receive_publish_qos1, shared_test_data) {
|
2024-01-17 15:13:56 +01:00
|
|
|
test::msg_exchange broker_side;
|
|
|
|
|
broker_side
|
|
|
|
|
.expect(connect)
|
|
|
|
|
.complete_with(success, after(0ms))
|
|
|
|
|
.reply_with(connack, after(0ms))
|
|
|
|
|
.send(publish_qos1, after(10ms))
|
|
|
|
|
.expect(puback)
|
|
|
|
|
.complete_with(success, after(1ms));
|
|
|
|
|
|
|
|
|
|
run_test(std::move(broker_side));
|
2024-01-15 08:48:34 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-23 11:46:00 +01:00
|
|
|
BOOST_FIXTURE_TEST_CASE(receive_publish_qos2, shared_test_data) {
|
2024-01-17 15:13:56 +01:00
|
|
|
test::msg_exchange broker_side;
|
|
|
|
|
broker_side
|
|
|
|
|
.expect(connect)
|
|
|
|
|
.complete_with(success, after(0ms))
|
|
|
|
|
.reply_with(connack, after(0ms))
|
|
|
|
|
.send(publish_qos2, after(10ms))
|
|
|
|
|
.expect(pubrec)
|
|
|
|
|
.complete_with(success, after(1ms))
|
|
|
|
|
.reply_with(pubrel, after(2ms))
|
|
|
|
|
.expect(pubcomp)
|
|
|
|
|
.complete_with(success, after(1ms));
|
2024-01-15 08:48:34 +01:00
|
|
|
|
2024-01-17 15:13:56 +01:00
|
|
|
run_test(std::move(broker_side));
|
|
|
|
|
}
|
2024-01-15 08:48:34 +01:00
|
|
|
|
2024-01-23 11:46:00 +01:00
|
|
|
BOOST_FIXTURE_TEST_CASE(receive_malformed_publish, shared_test_data) {
|
|
|
|
|
// packets
|
|
|
|
|
auto malformed_publish = encoders::encode_publish(
|
|
|
|
|
1, "malformed topic", "malformed payload",
|
|
|
|
|
static_cast<qos_e>(0b11), retain_e::yes, dup_e::no, {}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
auto disconnect = encoders::encode_disconnect(
|
|
|
|
|
reason_codes::malformed_packet.value(),
|
2024-01-26 09:39:01 +01:00
|
|
|
test::dprops_with_reason_string("Malformed PUBLISH received: QoS bits set to 0b11")
|
2024-01-23 11:46:00 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
test::msg_exchange broker_side;
|
|
|
|
|
broker_side
|
|
|
|
|
.expect(connect)
|
|
|
|
|
.complete_with(success, after(0ms))
|
|
|
|
|
.reply_with(connack, after(0ms))
|
|
|
|
|
.send(malformed_publish, after(10ms))
|
|
|
|
|
.expect(disconnect)
|
|
|
|
|
.complete_with(success, after(1ms))
|
|
|
|
|
.expect(connect)
|
|
|
|
|
.complete_with(success, after(0ms))
|
|
|
|
|
.reply_with(connack, after(0ms))
|
|
|
|
|
.send(publish_qos0, after(50ms));
|
|
|
|
|
|
|
|
|
|
run_test(std::move(broker_side));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_CASE(receive_malformed_pubrel, shared_test_data) {
|
|
|
|
|
// packets
|
|
|
|
|
auto malformed_pubrel = encoders::encode_pubrel(1, uint8_t(0x04), {});
|
|
|
|
|
|
|
|
|
|
auto disconnect = encoders::encode_disconnect(
|
|
|
|
|
reason_codes::malformed_packet.value(),
|
2024-01-26 09:39:01 +01:00
|
|
|
test::dprops_with_reason_string("Malformed PUBREL received: invalid Reason Code")
|
2024-01-23 11:46:00 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
test::msg_exchange broker_side;
|
|
|
|
|
broker_side
|
|
|
|
|
.expect(connect)
|
|
|
|
|
.complete_with(success, after(0ms))
|
|
|
|
|
.reply_with(connack, after(0ms))
|
|
|
|
|
.send(publish_qos2, after(10ms))
|
|
|
|
|
.expect(pubrec)
|
|
|
|
|
.complete_with(success, after(1ms))
|
|
|
|
|
.reply_with(malformed_pubrel, after(2ms))
|
|
|
|
|
.expect(disconnect)
|
|
|
|
|
.complete_with(success, after(1ms))
|
|
|
|
|
.expect(connect)
|
|
|
|
|
.complete_with(success, after(0ms))
|
|
|
|
|
.reply_with(connack, after(0ms))
|
|
|
|
|
.send(pubrel, after(100ms))
|
|
|
|
|
.expect(pubcomp)
|
|
|
|
|
.complete_with(success, after(1ms));
|
|
|
|
|
|
|
|
|
|
run_test(std::move(broker_side));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_CASE(fail_to_send_puback, shared_test_data) {
|
|
|
|
|
// packets
|
|
|
|
|
auto publish_qos1_dup = encoders::encode_publish(
|
|
|
|
|
1, topic, payload, qos_e::at_least_once, retain_e::no, dup_e::yes, {}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
test::msg_exchange broker_side;
|
|
|
|
|
broker_side
|
|
|
|
|
.expect(connect)
|
|
|
|
|
.complete_with(success, after(0ms))
|
|
|
|
|
.reply_with(connack, after(0ms))
|
|
|
|
|
.send(publish_qos1, after(3ms))
|
|
|
|
|
.expect(puback)
|
|
|
|
|
.complete_with(fail, after(1ms))
|
|
|
|
|
.expect(connect)
|
|
|
|
|
.complete_with(success, after(1ms))
|
|
|
|
|
.reply_with(connack, after(2ms))
|
|
|
|
|
.send(publish_qos1_dup, after(100ms))
|
|
|
|
|
.expect(puback)
|
|
|
|
|
.complete_with(success, after(1ms));
|
|
|
|
|
|
|
|
|
|
run_test(std::move(broker_side));
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-17 15:13:56 +01:00
|
|
|
BOOST_FIXTURE_TEST_CASE(fail_to_send_pubrec, shared_test_data) {
|
2024-01-23 11:46:00 +01:00
|
|
|
// packets
|
|
|
|
|
auto publish_qos2_dup = encoders::encode_publish(
|
|
|
|
|
1, topic, payload, qos_e::exactly_once, retain_e::no, dup_e::yes, {}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
test::msg_exchange broker_side;
|
|
|
|
|
broker_side
|
|
|
|
|
.expect(connect)
|
|
|
|
|
.complete_with(success, after(0ms))
|
|
|
|
|
.reply_with(connack, after(0ms))
|
|
|
|
|
.send(publish_qos2, after(3ms))
|
|
|
|
|
.expect(pubrec)
|
|
|
|
|
.complete_with(fail, after(1ms))
|
|
|
|
|
.expect(connect)
|
|
|
|
|
.complete_with(success, after(0ms))
|
|
|
|
|
.reply_with(connack, after(0ms))
|
|
|
|
|
.send(publish_qos2_dup, after(100ms))
|
|
|
|
|
.expect(pubrec)
|
|
|
|
|
.complete_with(success, after(1ms))
|
|
|
|
|
.reply_with(pubrel, after(2ms))
|
|
|
|
|
.expect(pubcomp)
|
|
|
|
|
.complete_with(success, after(1ms));
|
|
|
|
|
|
|
|
|
|
run_test(std::move(broker_side));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_CASE(broker_fails_to_receive_pubrec, shared_test_data) {
|
|
|
|
|
// packets
|
2024-01-17 15:13:56 +01:00
|
|
|
auto publish_dup = encoders::encode_publish(
|
|
|
|
|
1, topic, payload, qos_e::exactly_once, retain_e::no, dup_e::yes, {}
|
2024-01-15 08:48:34 +01:00
|
|
|
);
|
|
|
|
|
|
2024-01-17 15:13:56 +01:00
|
|
|
test::msg_exchange broker_side;
|
|
|
|
|
broker_side
|
|
|
|
|
.expect(connect)
|
|
|
|
|
.complete_with(success, after(0ms))
|
|
|
|
|
.reply_with(connack, after(0ms))
|
|
|
|
|
.send(publish_qos2, after(3ms))
|
|
|
|
|
// write completed, but the broker did not actually
|
|
|
|
|
// receive pubrec, it will resend publish again
|
|
|
|
|
.expect(pubrec)
|
|
|
|
|
.complete_with(success, after(1ms))
|
|
|
|
|
.reply_with(fail, after(3ms))
|
|
|
|
|
.expect(connect)
|
|
|
|
|
.complete_with(success, after(0ms))
|
|
|
|
|
.reply_with(connack, after(0ms))
|
|
|
|
|
.send(publish_dup, after(100ms))
|
|
|
|
|
.expect(pubrec)
|
|
|
|
|
.complete_with(success, after(1ms))
|
|
|
|
|
.reply_with(pubrel, after(2ms))
|
|
|
|
|
.expect(pubcomp)
|
|
|
|
|
.complete_with(success, after(1ms));
|
2024-01-15 08:48:34 +01:00
|
|
|
|
2024-01-17 15:13:56 +01:00
|
|
|
run_test(std::move(broker_side));
|
|
|
|
|
}
|
2024-01-15 08:48:34 +01:00
|
|
|
|
2024-01-17 15:13:56 +01:00
|
|
|
BOOST_FIXTURE_TEST_CASE(fail_to_send_pubcomp, shared_test_data) {
|
2024-01-15 08:48:34 +01:00
|
|
|
test::msg_exchange broker_side;
|
|
|
|
|
broker_side
|
|
|
|
|
.expect(connect)
|
|
|
|
|
.complete_with(success, after(0ms))
|
|
|
|
|
.reply_with(connack, after(0ms))
|
2024-01-17 15:13:56 +01:00
|
|
|
.send(publish_qos2, after(10ms))
|
2024-01-15 08:48:34 +01:00
|
|
|
.expect(pubrec)
|
|
|
|
|
.complete_with(success, after(1ms))
|
|
|
|
|
.reply_with(pubrel, after(2ms))
|
|
|
|
|
.expect(pubcomp)
|
|
|
|
|
.complete_with(fail, after(1ms))
|
|
|
|
|
.expect(connect)
|
|
|
|
|
.complete_with(success, after(0ms))
|
|
|
|
|
.reply_with(connack, after(0ms))
|
|
|
|
|
.send(pubrel, after(10ms))
|
|
|
|
|
.expect(pubcomp)
|
|
|
|
|
.complete_with(success, after(1ms));
|
|
|
|
|
|
2024-01-17 15:13:56 +01:00
|
|
|
run_test(std::move(broker_side));
|
2024-01-15 08:48:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END();
|