2024-05-27 10:35:06 +02:00
|
|
|
//
|
2025-02-14 13:42:23 +01:00
|
|
|
// Copyright (c) 2023-2025 Ivica Siladic, Bruno Iljazovic, Korina Simicevic
|
2024-05-27 10:35:06 +02:00
|
|
|
//
|
|
|
|
|
// Distributed under the Boost Software License, Version 1.0.
|
|
|
|
|
// (See accompanying file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
|
//
|
|
|
|
|
|
2025-11-13 16:40:18 +01:00
|
|
|
#include "test_common/test_service.hpp"
|
|
|
|
|
|
2025-01-13 16:11:41 +01:00
|
|
|
#include <boost/mqtt5/types.hpp>
|
2024-01-11 15:18:03 +01:00
|
|
|
|
2025-01-13 16:11:41 +01:00
|
|
|
#include <boost/mqtt5/impl/disconnect_op.hpp>
|
2024-10-08 09:59:35 +02:00
|
|
|
|
2024-01-11 15:18:03 +01:00
|
|
|
#include <boost/asio/io_context.hpp>
|
2024-10-08 09:59:35 +02:00
|
|
|
#include <boost/asio/ip/tcp.hpp>
|
2025-01-13 16:11:41 +01:00
|
|
|
#include <boost/test/unit_test.hpp>
|
2024-01-11 15:18:03 +01:00
|
|
|
|
2025-01-13 16:11:41 +01:00
|
|
|
#include <chrono>
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <string>
|
2024-01-11 15:18:03 +01:00
|
|
|
|
2025-01-13 16:11:41 +01:00
|
|
|
using namespace boost::mqtt5;
|
2024-01-11 15:18:03 +01:00
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE(disconnect_op/*, *boost::unit_test::disabled()*/)
|
|
|
|
|
|
2024-01-22 12:41:04 +01:00
|
|
|
void run_malformed_props_test(const disconnect_props& dprops) {
|
2025-01-13 16:11:41 +01:00
|
|
|
constexpr int expected_handlers_called = 1;
|
|
|
|
|
int handlers_called = 0;
|
2024-01-11 15:18:03 +01:00
|
|
|
|
2025-01-13 16:11:41 +01:00
|
|
|
asio::io_context ioc;
|
|
|
|
|
using client_service_type = test::test_service<asio::ip::tcp::socket>;
|
|
|
|
|
auto svc_ptr = std::make_shared<client_service_type>(ioc.get_executor());
|
2024-01-11 15:18:03 +01:00
|
|
|
|
2025-01-13 16:11:41 +01:00
|
|
|
auto handler = [&handlers_called](error_code ec) {
|
|
|
|
|
++handlers_called;
|
|
|
|
|
BOOST_TEST(ec == client::error::malformed_packet);
|
|
|
|
|
};
|
2024-01-22 12:41:04 +01:00
|
|
|
|
2025-01-13 16:11:41 +01:00
|
|
|
detail::disconnect_ctx ctx;
|
|
|
|
|
ctx.props = dprops;
|
2024-01-11 15:18:03 +01:00
|
|
|
|
2025-01-13 16:11:41 +01:00
|
|
|
detail::disconnect_op<
|
|
|
|
|
client_service_type, detail::disconnect_ctx
|
|
|
|
|
> { svc_ptr, std::move(ctx), std::move(handler) }
|
|
|
|
|
.perform();
|
2024-01-11 15:18:03 +01:00
|
|
|
|
2025-11-13 16:40:18 +01:00
|
|
|
ioc.poll();
|
2025-01-13 16:11:41 +01:00
|
|
|
BOOST_TEST(handlers_called == expected_handlers_called);
|
2024-01-11 15:18:03 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-22 12:41:04 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(malformed_reason_string) {
|
2025-01-13 16:11:41 +01:00
|
|
|
disconnect_props dprops;
|
|
|
|
|
dprops[prop::reason_string] = std::string { 0x01 };
|
2024-01-22 12:41:04 +01:00
|
|
|
|
2025-01-13 16:11:41 +01:00
|
|
|
run_malformed_props_test(dprops);
|
2024-01-22 12:41:04 +01:00
|
|
|
}
|
|
|
|
|
|
2024-02-16 10:36:26 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(malformed_user_property_key) {
|
2025-01-13 16:11:41 +01:00
|
|
|
disconnect_props dprops;
|
|
|
|
|
dprops[prop::user_property].emplace_back(std::string { 0x01 }, "value");
|
2024-02-16 10:36:26 +01:00
|
|
|
|
2025-01-13 16:11:41 +01:00
|
|
|
run_malformed_props_test(dprops);
|
2024-02-16 10:36:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(malformed_user_property_value) {
|
2025-01-13 16:11:41 +01:00
|
|
|
disconnect_props dprops;
|
|
|
|
|
dprops[prop::user_property].emplace_back("key", std::string { 0x01 });
|
2024-01-22 12:41:04 +01:00
|
|
|
|
2025-01-13 16:11:41 +01:00
|
|
|
run_malformed_props_test(dprops);
|
2024-01-22 12:41:04 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-11 15:18:03 +01:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|