2024-01-15 08:48:34 +01:00
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
|
|
|
|
|
#include <boost/asio/io_context.hpp>
|
|
|
|
|
|
|
|
|
|
#include <async_mqtt5/impl/unsubscribe_op.hpp>
|
|
|
|
|
|
|
|
|
|
#include "test_common/test_service.hpp"
|
|
|
|
|
|
|
|
|
|
using namespace async_mqtt5;
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE(unsubscribe_op/*, *boost::unit_test::disabled()*/)
|
|
|
|
|
|
2024-01-23 08:04:37 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(pid_overrun) {
|
2024-01-15 08:48:34 +01:00
|
|
|
constexpr int expected_handlers_called = 1;
|
|
|
|
|
int handlers_called = 0;
|
|
|
|
|
|
|
|
|
|
asio::io_context ioc;
|
|
|
|
|
using client_service_type = test::overrun_client<asio::ip::tcp::socket>;
|
2024-02-14 07:59:27 +01:00
|
|
|
auto svc_ptr = std::make_shared<client_service_type>(ioc.get_executor());
|
2024-01-15 08:48:34 +01:00
|
|
|
|
2024-01-23 08:04:37 +01:00
|
|
|
auto handler = [&handlers_called](error_code ec, std::vector<reason_code> rcs, unsuback_props) {
|
2024-01-15 08:48:34 +01:00
|
|
|
++handlers_called;
|
|
|
|
|
BOOST_CHECK(ec == client::error::pid_overrun);
|
|
|
|
|
BOOST_ASSERT(rcs.size() == 1);
|
|
|
|
|
BOOST_CHECK_EQUAL(rcs[0], reason_codes::empty);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
detail::unsubscribe_op<
|
|
|
|
|
client_service_type, decltype(handler)
|
|
|
|
|
> { svc_ptr, std::move(handler) }
|
2024-01-23 08:04:37 +01:00
|
|
|
.perform({ "topic" }, unsubscribe_props {});
|
2024-01-15 08:48:34 +01:00
|
|
|
|
|
|
|
|
ioc.run_for(std::chrono::milliseconds(500));
|
|
|
|
|
BOOST_CHECK_EQUAL(handlers_called, expected_handlers_called);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-23 08:04:37 +01:00
|
|
|
void run_test(
|
2024-01-26 09:39:01 +01:00
|
|
|
error_code expected_ec, const std::vector<std::string>& topics,
|
2024-01-23 08:04:37 +01:00
|
|
|
const unsubscribe_props& uprops = {}, const connack_props& cprops = {}
|
|
|
|
|
) {
|
|
|
|
|
constexpr int expected_handlers_called = 1;
|
2024-01-15 08:48:34 +01:00
|
|
|
int handlers_called = 0;
|
|
|
|
|
|
|
|
|
|
asio::io_context ioc;
|
|
|
|
|
using client_service_type = test::test_service<asio::ip::tcp::socket>;
|
2024-01-23 08:04:37 +01:00
|
|
|
auto svc_ptr = std::make_shared<client_service_type>(ioc.get_executor(), cprops);
|
|
|
|
|
|
2024-01-26 09:39:01 +01:00
|
|
|
auto handler = [&handlers_called, expected_ec, num_tp = topics.size()]
|
2024-01-23 08:04:37 +01:00
|
|
|
(error_code ec, std::vector<reason_code> rcs, unsuback_props) {
|
|
|
|
|
++handlers_called;
|
|
|
|
|
|
|
|
|
|
BOOST_CHECK(ec == expected_ec);
|
2024-01-26 09:39:01 +01:00
|
|
|
BOOST_ASSERT(rcs.size() == num_tp);
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < rcs.size(); ++i)
|
|
|
|
|
BOOST_CHECK_EQUAL(rcs[i], reason_codes::empty);
|
2024-01-23 08:04:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
detail::unsubscribe_op<
|
|
|
|
|
client_service_type, decltype(handler)
|
|
|
|
|
> { svc_ptr, std::move(handler) }
|
2024-01-26 09:39:01 +01:00
|
|
|
.perform(topics, uprops);
|
2024-01-15 08:48:34 +01:00
|
|
|
|
|
|
|
|
ioc.run_for(std::chrono::milliseconds(500));
|
|
|
|
|
BOOST_CHECK_EQUAL(handlers_called, expected_handlers_called);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-26 09:39:01 +01:00
|
|
|
void run_test(
|
|
|
|
|
error_code expected_ec, const std::string& topic,
|
|
|
|
|
const unsubscribe_props& uprops = {}, const connack_props& cprops = {}
|
|
|
|
|
) {
|
|
|
|
|
return run_test(
|
|
|
|
|
expected_ec, std::vector<std::string> { topic }, uprops, cprops
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-23 08:04:37 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(invalid_topic_filter_1) {
|
|
|
|
|
run_test(client::error::invalid_topic, "");
|
|
|
|
|
}
|
2024-01-15 08:48:34 +01:00
|
|
|
|
2024-01-23 08:04:37 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(invalid_topic_filter_2) {
|
|
|
|
|
run_test(client::error::invalid_topic, "+topic");
|
|
|
|
|
}
|
2024-01-15 08:48:34 +01:00
|
|
|
|
2024-01-23 08:04:37 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(invalid_topic_filter_3) {
|
|
|
|
|
run_test(client::error::invalid_topic, "topic+");
|
|
|
|
|
}
|
2024-01-15 08:48:34 +01:00
|
|
|
|
2024-01-23 08:04:37 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(invalid_topic_filter_4) {
|
|
|
|
|
run_test(client::error::invalid_topic, "#topic");
|
|
|
|
|
}
|
2024-01-15 08:48:34 +01:00
|
|
|
|
2024-01-23 08:04:37 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(invalid_topic_filter_5) {
|
|
|
|
|
run_test(client::error::invalid_topic, "some/#/topic");
|
|
|
|
|
}
|
2024-01-15 08:48:34 +01:00
|
|
|
|
2024-01-23 08:04:37 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(invalid_topic_filter_6) {
|
|
|
|
|
run_test(client::error::invalid_topic, "some/topic#");
|
|
|
|
|
}
|
2024-01-15 08:48:34 +01:00
|
|
|
|
2024-01-23 08:04:37 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(malformed_user_property_1) {
|
|
|
|
|
unsubscribe_props uprops;
|
|
|
|
|
uprops[prop::user_property].push_back(std::string(10, char(0x01)));
|
2024-01-15 08:48:34 +01:00
|
|
|
|
2024-01-23 08:04:37 +01:00
|
|
|
run_test(client::error::malformed_packet, "topic", uprops);
|
2024-01-15 08:48:34 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-23 08:04:37 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(malformed_user_property_2) {
|
|
|
|
|
unsubscribe_props uprops;
|
|
|
|
|
uprops[prop::user_property].push_back(std::string(75000, 'a'));
|
2024-01-15 08:48:34 +01:00
|
|
|
|
2024-01-23 08:04:37 +01:00
|
|
|
run_test(client::error::malformed_packet, "topic", uprops);
|
|
|
|
|
}
|
2024-01-15 08:48:34 +01:00
|
|
|
|
2024-01-23 08:04:37 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(packet_too_large) {
|
|
|
|
|
connack_props cprops;
|
|
|
|
|
cprops[prop::maximum_packet_size] = 10;
|
2024-01-15 08:48:34 +01:00
|
|
|
|
2024-01-23 08:04:37 +01:00
|
|
|
run_test(
|
|
|
|
|
client::error::packet_too_large, "very large topic",
|
|
|
|
|
unsubscribe_props {}, cprops
|
2024-01-15 08:48:34 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-26 09:39:01 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(zero_topic_filters) {
|
|
|
|
|
run_test(client::error::invalid_topic, std::vector<std::string> {});
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-15 08:48:34 +01:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|