Files

265 lines
8.3 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/mqtt_client.hpp>
#include <boost/mqtt5/types.hpp>
#include <boost/asio/detached.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/steady_timer.hpp>
2024-01-19 09:26:54 +01:00
#include <boost/test/unit_test.hpp>
2024-10-08 09:59:35 +02:00
#include <chrono>
#include <cstdint>
#include <string>
#include <variant>
#include <vector>
2024-01-19 09:26:54 +01:00
#include "test_common/message_exchange.hpp"
#include "test_common/packet_util.hpp"
2024-01-19 09:26:54 +01:00
#include "test_common/test_authenticators.hpp"
#include "test_common/test_broker.hpp"
2024-01-19 09:26:54 +01:00
#include "test_common/test_stream.hpp"
2025-01-13 16:11:41 +01:00
using namespace boost::mqtt5;
2024-01-19 09:26:54 +01:00
BOOST_AUTO_TEST_SUITE(re_authentication/*, *boost::unit_test::disabled()*/)
2024-01-19 09:26:54 +01:00
struct shared_test_data {
2025-01-13 16:11:41 +01:00
error_code success {};
error_code fail = asio::error::not_connected;
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
const std::string connect = encoders::encode_connect(
"", std::nullopt, std::nullopt, 60, false, init_connect_props(), std::nullopt
);
const std::string connack = encoders::encode_connack(
true, reason_codes::success.value(), {}
);
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
const std::string auth_challenge = encoders::encode_auth(
reason_codes::reauthenticate.value(), init_auth_props()
);
const std::string auth_response = encoders::encode_auth(
reason_codes::continue_authentication.value(), init_auth_props()
);
const std::string auth_success = encoders::encode_auth(
reason_codes::success.value(), init_auth_props()
);
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
connect_props init_connect_props() {
connect_props cprops;
cprops[prop::authentication_method] = "method";
cprops[prop::authentication_data] = "";
return cprops;
}
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
auth_props init_auth_props() {
auth_props aprops;
aprops[prop::authentication_method] = "method";
aprops[prop::authentication_data] = "";
return aprops;
}
2024-01-19 09:26:54 +01:00
};
using test::after;
2024-10-08 09:59:35 +02:00
using namespace std::chrono_literals;
2024-01-19 09:26:54 +01:00
template <typename Authenticator = std::monostate>
void run_test(
2025-01-13 16:11:41 +01:00
test::msg_exchange broker_side, Authenticator&& authenticator = {}
2024-01-19 09:26:54 +01:00
) {
2025-01-13 16:11:41 +01:00
asio::io_context ioc;
auto executor = ioc.get_executor();
auto& broker = asio::make_service<test::test_broker>(
ioc, executor, std::move(broker_side)
);
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
using client_type = mqtt_client<test::test_stream>;
client_type c(executor);
c.brokers("127.0.0.1");
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
if constexpr (!std::is_same_v<Authenticator, std::monostate>)
c.authenticator(std::forward<Authenticator>(authenticator));
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
c.async_run(asio::detached);
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
asio::steady_timer timer(c.get_executor());
// wait until the connection is established
timer.expires_after(20ms);
timer.async_wait([&](error_code) {
c.re_authenticate();
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
timer.expires_after(150ms);
timer.async_wait([&c](error_code) { c.cancel(); });
});
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
ioc.run();
BOOST_TEST(broker.received_all_expected());
2024-01-19 09:26:54 +01:00
}
BOOST_FIXTURE_TEST_CASE(successful_re_auth, shared_test_data) {
2025-01-13 16:11:41 +01:00
test::msg_exchange broker_side;
broker_side
.expect(connect)
.complete_with(success, after(0ms))
.reply_with(connack, after(1ms))
.expect(auth_challenge)
.complete_with(success, after(2ms))
.reply_with(auth_success, after(4ms));
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
run_test(std::move(broker_side), test::test_authenticator());
2024-01-19 09:26:54 +01:00
}
BOOST_FIXTURE_TEST_CASE(successful_re_auth_multi_step, shared_test_data) {
2025-01-13 16:11:41 +01:00
test::msg_exchange broker_side;
broker_side
.expect(connect)
.complete_with(success, after(0ms))
.reply_with(connack, after(1ms))
.expect(auth_challenge)
.complete_with(success, after(2ms))
.reply_with(auth_response, after(4ms))
.expect(auth_response)
.complete_with(success, after(2ms))
.reply_with(auth_success, after(4ms));
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
run_test(std::move(broker_side), test::test_authenticator());
2024-01-19 09:26:54 +01:00
}
BOOST_FIXTURE_TEST_CASE(malformed_auth_rc, shared_test_data) {
2025-01-13 16:11:41 +01:00
auto disconnect = encoders::encode_disconnect(
reason_codes::malformed_packet.value(),
test::dprops_with_reason_string("Malformed AUTH received: bad reason code")
);
auto malformed_auth = encoders::encode_auth(
reason_codes::administrative_action.value(), init_auth_props()
);
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
test::msg_exchange broker_side;
broker_side
.expect(connect)
.complete_with(success, after(0ms))
.reply_with(connack, after(1ms))
.expect(auth_challenge)
.complete_with(success, after(2ms))
.reply_with(malformed_auth, after(4ms))
.expect(disconnect)
.complete_with(success, after(2ms));
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
run_test(std::move(broker_side), test::test_authenticator());
2024-01-19 09:26:54 +01:00
}
BOOST_FIXTURE_TEST_CASE(mismatched_auth_method, shared_test_data) {
2025-01-13 16:11:41 +01:00
auth_props aprops;
aprops[prop::authentication_method] = "wrong method";
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
auto mismatched_auth_response = encoders::encode_auth(
reason_codes::continue_authentication.value(), aprops
);
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
auto disconnect = encoders::encode_disconnect(
reason_codes::protocol_error.value(),
test::dprops_with_reason_string("Malformed AUTH received: wrong authentication method")
);
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
test::msg_exchange broker_side;
broker_side
.expect(connect)
.complete_with(success, after(0ms))
.reply_with(connack, after(1ms))
.expect(auth_challenge)
.complete_with(success, after(2ms))
.reply_with(mismatched_auth_response, after(4ms))
.expect(disconnect)
.complete_with(success, after(2ms));
2025-01-13 16:11:41 +01:00
run_test(std::move(broker_side), test::test_authenticator());
}
BOOST_FIXTURE_TEST_CASE(malformed_auth_received, shared_test_data) {
2025-01-13 16:11:41 +01:00
auto malformed_auth = std::string { -16, 3, 24, 15, 1, 0 };
auto disconnect = encoders::encode_disconnect(
reason_codes::malformed_packet.value(),
test::dprops_with_reason_string("Malformed AUTH received: cannot decode")
);
2025-01-13 16:11:41 +01:00
test::msg_exchange broker_side;
broker_side
.expect(connect)
.complete_with(success, after(0ms))
.reply_with(connack, after(1ms))
.expect(auth_challenge)
.complete_with(success, after(2ms))
.reply_with(malformed_auth, after(4ms))
.expect(disconnect)
.complete_with(success, after(2ms));
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
run_test(std::move(broker_side), test::test_authenticator());
2024-01-19 09:26:54 +01:00
}
BOOST_FIXTURE_TEST_CASE(async_auth_fail, shared_test_data) {
2025-01-13 16:11:41 +01:00
auto disconnect = encoders::encode_disconnect(
reason_codes::unspecified_error.value(),
test::dprops_with_reason_string("Re-authentication: authentication fail")
);
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
test::msg_exchange broker_side;
broker_side
.expect(connect)
.complete_with(success, after(0ms))
.reply_with(connack, after(1ms))
.expect(auth_challenge)
.complete_with(success, after(2ms))
.reply_with(auth_response, after(4ms))
.expect(disconnect)
.complete_with(success, after(2ms));
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
run_test(
std::move(broker_side),
test::fail_test_authenticator<auth_step_e::server_challenge>()
);
2024-01-19 09:26:54 +01:00
}
2024-01-19 15:38:07 +01:00
BOOST_FIXTURE_TEST_CASE(unexpected_auth, shared_test_data) {
2025-01-13 16:11:41 +01:00
auto connect_no_auth = encoders::encode_connect(
"", std::nullopt, std::nullopt, 60, false, {}, std::nullopt
);
auto disconnect = encoders::encode_disconnect(
reason_codes::protocol_error.value(),
test::dprops_with_reason_string("Unexpected AUTH received")
);
2024-01-19 15:38:07 +01:00
2025-01-13 16:11:41 +01:00
test::msg_exchange broker_side;
broker_side
.expect(connect_no_auth)
.complete_with(success, after(0ms))
.reply_with(connack, after(1ms))
.send(auth_challenge, after(50ms))
.expect(disconnect)
.complete_with(success, after(2ms));
2024-01-19 15:38:07 +01:00
2025-01-13 16:11:41 +01:00
run_test(
std::move(broker_side)
);
2024-01-19 15:38:07 +01:00
}
2024-01-19 09:26:54 +01:00
BOOST_FIXTURE_TEST_CASE(re_auth_without_authenticator, shared_test_data) {
2025-01-13 16:11:41 +01:00
auto connect_no_auth = encoders::encode_connect(
"", std::nullopt, std::nullopt, 60, false, {}, std::nullopt
);
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
test::msg_exchange broker_side;
broker_side
.expect(connect_no_auth)
.complete_with(success, after(0ms))
.reply_with(connack, after(1ms));
2024-01-19 09:26:54 +01:00
2025-01-13 16:11:41 +01:00
run_test(std::move(broker_side));
2024-01-19 09:26:54 +01:00
}
BOOST_AUTO_TEST_SUITE_END();