Files

74 lines
2.0 KiB
C++
Raw Permalink Normal View History

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-01-13 16:11:41 +01:00
#include <boost/mqtt5/types.hpp>
2025-01-13 16:11:41 +01:00
#include <boost/mqtt5/impl/disconnect_op.hpp>
2024-10-08 09:59:35 +02:00
#include <boost/asio/io_context.hpp>
#include <boost/asio/ip/tcp.hpp>
2025-01-13 16:11:41 +01:00
#include <boost/test/unit_test.hpp>
2024-10-08 09:59:35 +02:00
2025-01-13 16:11:41 +01:00
#include <chrono>
#include <cstdint>
#include <memory>
#include <string>
#include "test_common/test_service.hpp"
2025-01-13 16:11:41 +01:00
using namespace boost::mqtt5;
BOOST_AUTO_TEST_SUITE(disconnect_op/*, *boost::unit_test::disabled()*/)
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;
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());
2025-01-13 16:11:41 +01:00
auto handler = [&handlers_called](error_code ec) {
++handlers_called;
BOOST_TEST(ec == client::error::malformed_packet);
};
2025-01-13 16:11:41 +01:00
detail::disconnect_ctx ctx;
ctx.props = dprops;
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();
2025-01-13 16:11:41 +01:00
ioc.run_for(std::chrono::milliseconds(500));
BOOST_TEST(handlers_called == expected_handlers_called);
}
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 };
2025-01-13 16:11:41 +01:00
run_malformed_props_test(dprops);
}
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");
2025-01-13 16:11:41 +01:00
run_malformed_props_test(dprops);
}
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 });
2025-01-13 16:11:41 +01:00
run_malformed_props_test(dprops);
}
BOOST_AUTO_TEST_SUITE_END()