2023-10-05 13:59:32 +02:00
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
2023-12-06 08:25:12 +01:00
|
|
|
#include <boost/asio/use_awaitable.hpp>
|
|
|
|
#ifdef BOOST_ASIO_HAS_CO_AWAIT
|
|
|
|
|
2023-11-23 15:36:06 +01:00
|
|
|
#include <boost/asio/as_tuple.hpp>
|
2023-10-05 13:59:32 +02:00
|
|
|
#include <boost/asio/co_spawn.hpp>
|
|
|
|
#include <boost/asio/detached.hpp>
|
|
|
|
#include <boost/asio/io_context.hpp>
|
|
|
|
#include <boost/asio/steady_timer.hpp>
|
2023-12-07 15:41:31 +01:00
|
|
|
#include <boost/asio/ssl.hpp>
|
2023-10-05 13:59:32 +02:00
|
|
|
|
|
|
|
#include <boost/beast/websocket.hpp>
|
|
|
|
|
2023-10-06 11:51:04 +02:00
|
|
|
#include <async_mqtt5.hpp>
|
|
|
|
|
2023-12-07 15:41:31 +01:00
|
|
|
namespace boost::beast::websocket {
|
|
|
|
|
|
|
|
template <typename TeardownHandler>
|
|
|
|
void async_teardown(
|
2024-01-02 11:40:53 +01:00
|
|
|
boost::beast::role_type /* role */,
|
2023-12-07 15:41:31 +01:00
|
|
|
asio::ssl::stream<asio::ip::tcp::socket>& stream,
|
|
|
|
TeardownHandler&& handler
|
|
|
|
) {
|
|
|
|
return stream.async_shutdown(std::forward<TeardownHandler>(handler));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace boost::beast::websocket
|
|
|
|
|
|
|
|
namespace async_mqtt5 {
|
|
|
|
|
|
|
|
template <typename StreamBase>
|
|
|
|
struct tls_handshake_type<asio::ssl::stream<StreamBase>> {
|
|
|
|
static constexpr auto client = asio::ssl::stream_base::client;
|
|
|
|
static constexpr auto server = asio::ssl::stream_base::server;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename StreamBase>
|
|
|
|
void assign_tls_sni(
|
|
|
|
const authority_path& ap,
|
2024-01-02 11:40:53 +01:00
|
|
|
asio::ssl::context& /* ctx */,
|
2023-12-07 15:41:31 +01:00
|
|
|
asio::ssl::stream<StreamBase>& stream
|
|
|
|
) {
|
|
|
|
SSL_set_tlsext_host_name(stream.native_handle(), ap.host.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace async_mqtt5
|
|
|
|
|
2023-10-05 13:59:32 +02:00
|
|
|
BOOST_AUTO_TEST_SUITE(coroutine/*, *boost::unit_test::disabled()*/)
|
|
|
|
|
|
|
|
using namespace async_mqtt5;
|
|
|
|
namespace asio = boost::asio;
|
|
|
|
|
2023-11-23 15:36:06 +01:00
|
|
|
constexpr auto use_nothrow_awaitable = asio::as_tuple(asio::use_awaitable);
|
|
|
|
|
2023-10-05 13:59:32 +02:00
|
|
|
template<typename StreamType, typename TlsContext>
|
|
|
|
asio::awaitable<void> sanity_check(mqtt_client<StreamType, TlsContext>& c) {
|
2023-11-23 15:36:06 +01:00
|
|
|
auto [ec_0] = co_await c.template async_publish<qos_e::at_most_once>(
|
|
|
|
"test/mqtt-test", "hello world with qos0!", retain_e::yes, publish_props {},
|
|
|
|
use_nothrow_awaitable
|
2023-10-05 13:59:32 +02:00
|
|
|
);
|
2023-11-23 15:36:06 +01:00
|
|
|
BOOST_CHECK(!ec_0);
|
2023-10-05 13:59:32 +02:00
|
|
|
|
2023-11-23 15:36:06 +01:00
|
|
|
auto [ec_1, puback_rc, puback_props] = co_await c.template async_publish<qos_e::at_least_once>(
|
2023-10-05 13:59:32 +02:00
|
|
|
"test/mqtt-test", "hello world with qos1!",
|
2023-11-23 15:36:06 +01:00
|
|
|
retain_e::yes, publish_props {},
|
|
|
|
use_nothrow_awaitable
|
2023-10-05 13:59:32 +02:00
|
|
|
);
|
2023-11-23 15:36:06 +01:00
|
|
|
BOOST_CHECK(!ec_1);
|
2023-10-05 13:59:32 +02:00
|
|
|
BOOST_CHECK(!puback_rc);
|
|
|
|
|
2023-11-23 15:36:06 +01:00
|
|
|
auto [ec_2, pubcomp_rc, pubcomp_props] = co_await c.template async_publish<qos_e::exactly_once>(
|
2023-10-05 13:59:32 +02:00
|
|
|
"test/mqtt-test", "hello world with qos2!",
|
2023-11-23 15:36:06 +01:00
|
|
|
retain_e::yes, publish_props {},
|
|
|
|
use_nothrow_awaitable
|
2023-10-05 13:59:32 +02:00
|
|
|
);
|
2023-11-23 15:36:06 +01:00
|
|
|
BOOST_CHECK(!ec_2);
|
2023-10-05 13:59:32 +02:00
|
|
|
BOOST_CHECK(!pubcomp_rc);
|
|
|
|
|
2024-01-10 12:37:55 +01:00
|
|
|
subscribe_topic sub_topic = subscribe_topic {
|
|
|
|
"test/mqtt-test", async_mqtt5::subscribe_options {
|
2023-10-05 13:59:32 +02:00
|
|
|
qos_e::exactly_once,
|
|
|
|
subscribe_options::no_local_e::no,
|
|
|
|
subscribe_options::retain_as_published_e::retain,
|
|
|
|
subscribe_options::retain_handling_e::send
|
|
|
|
}
|
2024-01-10 12:37:55 +01:00
|
|
|
};
|
2023-10-05 13:59:32 +02:00
|
|
|
|
2023-11-23 15:36:06 +01:00
|
|
|
auto [sub_ec, sub_codes, sub_props] = co_await c.async_subscribe(
|
2024-01-10 12:37:55 +01:00
|
|
|
sub_topic, subscribe_props {}, use_nothrow_awaitable
|
2023-10-05 13:59:32 +02:00
|
|
|
);
|
2023-11-23 15:36:06 +01:00
|
|
|
BOOST_CHECK(!sub_ec);
|
2023-10-05 13:59:32 +02:00
|
|
|
BOOST_CHECK(!sub_codes[0]);
|
2023-11-23 15:36:06 +01:00
|
|
|
auto [rec, topic, payload, publish_props] = co_await c.async_receive(use_nothrow_awaitable);
|
2023-10-05 13:59:32 +02:00
|
|
|
|
2023-11-23 15:36:06 +01:00
|
|
|
auto [unsub_ec, unsub_codes, unsub_props] = co_await c.async_unsubscribe(
|
2024-01-10 12:37:55 +01:00
|
|
|
"test/mqtt-test", unsubscribe_props {},
|
2023-11-23 15:36:06 +01:00
|
|
|
use_nothrow_awaitable
|
2023-10-05 13:59:32 +02:00
|
|
|
);
|
2023-11-23 15:36:06 +01:00
|
|
|
BOOST_CHECK(!unsub_ec);
|
2023-10-05 13:59:32 +02:00
|
|
|
BOOST_CHECK(!unsub_codes[0]);
|
|
|
|
|
2023-11-23 15:36:06 +01:00
|
|
|
co_await c.async_disconnect(use_nothrow_awaitable);
|
2023-10-05 13:59:32 +02:00
|
|
|
co_return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(tcp_client_check) {
|
|
|
|
asio::io_context ioc;
|
|
|
|
|
|
|
|
using stream_type = asio::ip::tcp::socket;
|
|
|
|
using client_type = mqtt_client<stream_type>;
|
|
|
|
client_type c(ioc, "");
|
|
|
|
|
|
|
|
c.credentials("tcp-tester", "", "")
|
2024-01-10 12:37:55 +01:00
|
|
|
.brokers("broker.hivemq.com", 1883)
|
2023-11-29 11:50:07 +01:00
|
|
|
.will({ "test/mqtt-test", "Client disconnected!", qos_e::at_least_once })
|
2023-10-05 13:59:32 +02:00
|
|
|
.run();
|
|
|
|
|
|
|
|
asio::steady_timer timer(ioc);
|
2023-12-07 15:41:31 +01:00
|
|
|
timer.expires_after(std::chrono::seconds(5));
|
2023-10-05 13:59:32 +02:00
|
|
|
|
|
|
|
timer.async_wait(
|
|
|
|
[&](boost::system::error_code ec) {
|
|
|
|
BOOST_CHECK_MESSAGE(ec, "Failed to receive all the expected replies!");
|
|
|
|
c.cancel();
|
|
|
|
ioc.stop();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
co_spawn(ioc,
|
|
|
|
[&]() -> asio::awaitable<void> {
|
|
|
|
co_await sanity_check(c);
|
|
|
|
timer.cancel();
|
|
|
|
},
|
|
|
|
asio::detached
|
|
|
|
);
|
|
|
|
|
|
|
|
ioc.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(websocket_tcp_client_check) {
|
|
|
|
asio::io_context ioc;
|
|
|
|
|
|
|
|
using stream_type = boost::beast::websocket::stream<
|
|
|
|
asio::ip::tcp::socket
|
|
|
|
>;
|
|
|
|
|
|
|
|
using client_type = mqtt_client<stream_type>;
|
|
|
|
client_type c(ioc, "");
|
|
|
|
|
2024-01-16 10:25:05 +01:00
|
|
|
c.brokers("broker.hivemq.com/mqtt", 8000)
|
2023-11-29 11:50:07 +01:00
|
|
|
.will({ "test/mqtt-test", "Client disconnected!", qos_e::at_least_once })
|
2023-10-05 13:59:32 +02:00
|
|
|
.run();
|
|
|
|
|
|
|
|
asio::steady_timer timer(ioc);
|
2023-12-07 15:41:31 +01:00
|
|
|
timer.expires_after(std::chrono::seconds(5));
|
2023-10-05 13:59:32 +02:00
|
|
|
|
|
|
|
timer.async_wait(
|
|
|
|
[&](boost::system::error_code ec) {
|
|
|
|
BOOST_CHECK_MESSAGE(ec, "Failed to receive all the expected replies!");
|
|
|
|
c.cancel();
|
|
|
|
ioc.stop();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2023-11-24 11:50:47 +01:00
|
|
|
co_spawn(ioc,
|
2023-10-05 13:59:32 +02:00
|
|
|
[&]() -> asio::awaitable<void> {
|
|
|
|
co_await sanity_check(c);
|
|
|
|
timer.cancel();
|
|
|
|
},
|
|
|
|
asio::detached
|
|
|
|
);
|
|
|
|
|
|
|
|
ioc.run();
|
|
|
|
}
|
|
|
|
|
2023-12-06 08:25:12 +01:00
|
|
|
|
2023-12-07 15:41:31 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(openssl_tls_client_check) {
|
|
|
|
asio::io_context ioc;
|
|
|
|
|
|
|
|
using stream_type = asio::ssl::stream<asio::ip::tcp::socket>;
|
|
|
|
asio::ssl::context tls_context(asio::ssl::context::tls_client);
|
|
|
|
|
|
|
|
using client_type = mqtt_client<stream_type, decltype(tls_context)>;
|
|
|
|
client_type c(ioc, "", std::move(tls_context));
|
|
|
|
|
2024-01-16 10:25:05 +01:00
|
|
|
c.brokers("broker.hivemq.com", 8883)
|
2023-12-07 15:41:31 +01:00
|
|
|
.will({ "test/mqtt-test", "Client disconnected!", qos_e::at_least_once })
|
|
|
|
.run();
|
|
|
|
|
|
|
|
asio::steady_timer timer(ioc);
|
|
|
|
timer.expires_after(std::chrono::seconds(5));
|
|
|
|
|
|
|
|
timer.async_wait(
|
|
|
|
[&](boost::system::error_code ec) {
|
|
|
|
BOOST_CHECK_MESSAGE(ec, "Failed to receive all the expected replies!");
|
|
|
|
c.cancel();
|
|
|
|
ioc.stop();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
co_spawn(ioc,
|
|
|
|
[&]() -> asio::awaitable<void> {
|
|
|
|
co_await sanity_check(c);
|
|
|
|
timer.cancel();
|
|
|
|
},
|
|
|
|
asio::detached
|
|
|
|
);
|
|
|
|
|
|
|
|
ioc.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(websocket_tls_client_check) {
|
|
|
|
asio::io_context ioc;
|
|
|
|
|
|
|
|
using stream_type = boost::beast::websocket::stream<
|
|
|
|
asio::ssl::stream<asio::ip::tcp::socket>
|
|
|
|
>;
|
|
|
|
|
|
|
|
asio::ssl::context tls_context(asio::ssl::context::tls_client);
|
|
|
|
|
|
|
|
using client_type = mqtt_client<stream_type, decltype(tls_context)>;
|
|
|
|
client_type c(ioc, "", std::move(tls_context));
|
|
|
|
|
2024-01-16 10:25:05 +01:00
|
|
|
c.brokers("broker.hivemq.com/mqtt", 8884)
|
2023-12-07 15:41:31 +01:00
|
|
|
.will({ "test/mqtt-test", "Client disconnected!", qos_e::at_least_once })
|
|
|
|
.run();
|
|
|
|
|
|
|
|
asio::steady_timer timer(ioc);
|
|
|
|
timer.expires_after(std::chrono::seconds(5));
|
|
|
|
|
|
|
|
timer.async_wait(
|
|
|
|
[&](boost::system::error_code ec) {
|
|
|
|
BOOST_CHECK_MESSAGE(ec, "Failed to receive all the expected replies!");
|
|
|
|
c.cancel();
|
|
|
|
ioc.stop();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
co_spawn(ioc,
|
|
|
|
[&]() -> asio::awaitable<void> {
|
|
|
|
co_await sanity_check(c);
|
|
|
|
timer.cancel();
|
|
|
|
},
|
|
|
|
asio::detached
|
|
|
|
);
|
|
|
|
|
|
|
|
ioc.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
2023-12-06 08:25:12 +01:00
|
|
|
|
|
|
|
#endif
|