#include #include #include #include #include #include #include "test_common/message_exchange.hpp" #include "test_common/test_service.hpp" #include "test_common/test_stream.hpp" using namespace async_mqtt5; BOOST_AUTO_TEST_SUITE(disconnect_op/*, *boost::unit_test::disabled()*/) void run_malformed_props_test(const disconnect_props& dprops) { constexpr int expected_handlers_called = 1; int handlers_called = 0; asio::io_context ioc; using client_service_type = test::test_service; auto svc_ptr = std::make_shared(ioc.get_executor()); auto handler = [&handlers_called](error_code ec) { ++handlers_called; BOOST_CHECK(ec == client::error::malformed_packet); }; detail::disconnect_ctx ctx; ctx.props = dprops; detail::disconnect_op< client_service_type, detail::disconnect_ctx > { svc_ptr, std::move(ctx), std::move(handler) } .perform(); ioc.run_for(std::chrono::milliseconds(500)); BOOST_CHECK_EQUAL(handlers_called, expected_handlers_called); } BOOST_AUTO_TEST_CASE(malformed_reason_string) { disconnect_props dprops; dprops[prop::reason_string] = std::string { 0x01 }; run_malformed_props_test(dprops); } BOOST_AUTO_TEST_CASE(malformed_user_property) { disconnect_props dprops; dprops[prop::user_property].push_back(std::string { 0x01 }); run_malformed_props_test(dprops); } BOOST_AUTO_TEST_CASE(omit_props) { using test::after; using namespace std::chrono; constexpr int expected_handlers_called = 1; int handlers_called = 0; connack_props co_props; co_props[prop::maximum_packet_size] = 20; // packets auto connect = encoders::encode_connect( "", std::nullopt, std::nullopt, 10, false, {}, std::nullopt ); auto connack = encoders::encode_connack( false, reason_codes::success.value(), co_props ); disconnect_props props; props[prop::user_property].push_back(std::string(50, 'a')); auto disconnect = encoders::encode_disconnect( reason_codes::normal_disconnection.value(), props ); auto disconnect_no_props = encoders::encode_disconnect( reason_codes::normal_disconnection.value(), disconnect_props{} ); test::msg_exchange broker_side; error_code success {}; broker_side .expect(connect) .complete_with(success, after(0ms)) .reply_with(connack, after(0ms)) .expect(disconnect_no_props) .complete_with(success, after(0ms)); asio::io_context ioc; auto executor = ioc.get_executor(); auto& broker = asio::make_service( ioc, executor, std::move(broker_side) ); using client_type = mqtt_client; client_type c(executor, ""); c.brokers("127.0.0.1") .async_run(asio::detached); asio::steady_timer timer(c.get_executor()); timer.expires_after(std::chrono::milliseconds(50)); timer.async_wait([&](auto) { c.async_disconnect( disconnect_rc_e::normal_disconnection, props, [&handlers_called](error_code ec) { handlers_called++; BOOST_CHECK(!ec); } ); }); ioc.run_for(std::chrono::seconds(2)); BOOST_CHECK_EQUAL(handlers_called, expected_handlers_called); BOOST_CHECK(broker.received_all_expected()); } BOOST_AUTO_TEST_SUITE_END()