Simplify and shorten unit test code - part 1

Summary: related to T12015

Reviewers: ivica

Reviewed By: ivica

Subscribers: miljen, iljazovic

Differential Revision: https://repo.mireo.local/D27488
This commit is contained in:
Korina Šimičević
2024-01-22 12:41:04 +01:00
parent 28bf23947d
commit 9b92f7fca2
7 changed files with 200 additions and 243 deletions

View File

@ -104,7 +104,7 @@ jobs:
- name: Run tests - name: Run tests
run: | run: |
.\\test\\build\\${{ matrix.build-type }}\\mqtt-test.exe --log_level=test_suite .\\test\\build\\${{ matrix.build-type }}\\mqtt-test.exe
posix: posix:
name: "${{ matrix.toolset }} std=c++${{ matrix.cxxstd }} ${{ matrix.build-type }}" name: "${{ matrix.toolset }} std=c++${{ matrix.cxxstd }} ${{ matrix.build-type }}"
@ -220,4 +220,4 @@ jobs:
cmake --build test/build -j 4 cmake --build test/build -j 4
- name: Run tests - name: Run tests
run: ./test/build/mqtt-test --log_level=test_suite run: ./test/build/mqtt-test

View File

@ -8,7 +8,7 @@ env:
jobs: jobs:
posix: posix:
name: "coverage ${{matrix.compiler}} -std=c++${{matrix.cxxstd}}" name: "coverage ${{ matrix.compiler }} -std=c++${{ matrix.cxxstd }}"
defaults: defaults:
run: run:
shell: bash shell: bash
@ -66,7 +66,7 @@ jobs:
cmake --build test/build -j 4 cmake --build test/build -j 4
- name: Run tests - name: Run tests
run: ./test/build/mqtt-test --log_level=test_suite run: ./test/build/mqtt-test
- name: Generate Coverage Report - name: Generate Coverage Report
run: | run: |

23
codecov.yml Normal file
View File

@ -0,0 +1,23 @@
codecov:
require_ci_to_pass: true
comment:
behavior: default
layout: reach,diff,flags,tree,reach
show_carryforward_flags: false
coverage:
precision: 2
round: down
status:
changes: false
default_rules:
flag_coverage_not_uploaded_behavior: include
patch:
default:
informational: true
project:
default:
informational: true
github_checks:
annotations: true

View File

@ -1,8 +1,5 @@
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
#include <boost/asio/prepend.hpp>
#include <boost/asio/post.hpp>
#include <async_mqtt5/mqtt_client.hpp> #include <async_mqtt5/mqtt_client.hpp>
#include "test_common/message_exchange.hpp" #include "test_common/message_exchange.hpp"
@ -13,12 +10,12 @@ using namespace async_mqtt5;
BOOST_AUTO_TEST_SUITE(read_message/*, *boost::unit_test::disabled()*/) BOOST_AUTO_TEST_SUITE(read_message/*, *boost::unit_test::disabled()*/)
using test::after;
using namespace std::chrono;
void test_receive_malformed_packet( void test_receive_malformed_packet(
std::string malformed_packet, std::string reason_string std::string malformed_packet, std::string reason_string
) { ) {
using test::after;
using std::chrono_literals::operator ""ms;
// packets // packets
auto connect = encoders::encode_connect( auto connect = encoders::encode_connect(
"", std::nullopt, std::nullopt, 10, false, {}, std::nullopt "", std::nullopt, std::nullopt, 10, false, {}, std::nullopt
@ -102,10 +99,16 @@ BOOST_AUTO_TEST_CASE(receive_malformed_publish) {
); );
} }
BOOST_AUTO_TEST_CASE(receive_disconnect) { struct shared_test_data {
using test::after; error_code success {};
using std::chrono_literals::operator ""ms;
const std::string connect = encoders::encode_connect(
"", std::nullopt, std::nullopt, 10, false, {}, std::nullopt
);
const std::string connack = encoders::encode_connack(false, uint8_t(0x00), {});
};
BOOST_FIXTURE_TEST_CASE(receive_disconnect, shared_test_data) {
// packets // packets
auto connect = encoders::encode_connect( auto connect = encoders::encode_connect(
"", std::nullopt, std::nullopt, 10, false, {}, std::nullopt "", std::nullopt, std::nullopt, 10, false, {}, std::nullopt
@ -147,10 +150,7 @@ BOOST_AUTO_TEST_CASE(receive_disconnect) {
} }
BOOST_AUTO_TEST_CASE(receive_pingresp) { BOOST_FIXTURE_TEST_CASE(receive_pingresp, shared_test_data) {
using test::after;
using std::chrono_literals::operator ""ms;
// packets // packets
auto connect = encoders::encode_connect( auto connect = encoders::encode_connect(
"", std::nullopt, std::nullopt, 10, false, {}, std::nullopt "", std::nullopt, std::nullopt, 10, false, {}, std::nullopt
@ -189,10 +189,7 @@ BOOST_AUTO_TEST_CASE(receive_pingresp) {
} }
BOOST_AUTO_TEST_CASE(send_byte_by_byte) { BOOST_FIXTURE_TEST_CASE(receive_byte_by_byte, shared_test_data) {
using test::after;
using std::chrono_literals::operator ""ms;
constexpr int expected_handlers_called = 1; constexpr int expected_handlers_called = 1;
int handlers_called = 0; int handlers_called = 0;

View File

@ -105,7 +105,6 @@ BOOST_FIXTURE_TEST_CASE(connection_refused, shared_test_data) {
run_unit_test(std::move(broker_side), std::move(handler)); run_unit_test(std::move(broker_side), std::move(handler));
} }
BOOST_FIXTURE_TEST_CASE(access_denied, shared_test_data) { BOOST_FIXTURE_TEST_CASE(access_denied, shared_test_data) {
auto denied_connack = encoders::encode_connack( auto denied_connack = encoders::encode_connack(
true, reason_codes::bad_username_or_password.value(), {} true, reason_codes::bad_username_or_password.value(), {}
@ -137,7 +136,6 @@ BOOST_FIXTURE_TEST_CASE(fail_to_send_connect, shared_test_data) {
run_unit_test(std::move(broker_side), std::move(handler)); run_unit_test(std::move(broker_side), std::move(handler));
} }
BOOST_FIXTURE_TEST_CASE(receive_wrong_packet, shared_test_data) { BOOST_FIXTURE_TEST_CASE(receive_wrong_packet, shared_test_data) {
// packets // packets
auto unexpected_packet = encoders::encode_puback(1, uint8_t(0x00), {}); auto unexpected_packet = encoders::encode_puback(1, uint8_t(0x00), {});

View File

@ -17,49 +17,50 @@ using namespace async_mqtt5;
BOOST_AUTO_TEST_SUITE(disconnect_op/*, *boost::unit_test::disabled()*/) BOOST_AUTO_TEST_SUITE(disconnect_op/*, *boost::unit_test::disabled()*/)
void run_malformed_props_test(const disconnect_props& dprops) {
BOOST_AUTO_TEST_CASE(test_malformed_packet) { constexpr int expected_handlers_called = 1;
std::string malformed_str = std::string{ 0x01 };
disconnect_props invalid_user_props;
invalid_user_props[prop::user_property].push_back(malformed_str);
disconnect_props invalid_reason_string;
invalid_reason_string[prop::reason_string] = malformed_str;
std::vector<disconnect_props> testing_props = {
invalid_user_props, invalid_reason_string
};
int expected_handlers_called = static_cast<int>(testing_props.size());
int handlers_called = 0; int handlers_called = 0;
asio::io_context ioc; asio::io_context ioc;
using client_service_type = test::test_service<asio::ip::tcp::socket>; using client_service_type = test::test_service<asio::ip::tcp::socket>;
auto svc_ptr = std::make_shared<client_service_type>(ioc.get_executor()); auto svc_ptr = std::make_shared<client_service_type>(ioc.get_executor());
for (const auto& props : testing_props) {
auto handler = [&handlers_called](error_code ec) {
++handlers_called;
BOOST_CHECK(ec == client::error::malformed_packet);
};
detail::disconnect_ctx ctx; auto handler = [&handlers_called](error_code ec) {
ctx.props = props; ++handlers_called;
BOOST_CHECK(ec == client::error::malformed_packet);
};
detail::disconnect_op< detail::disconnect_ctx ctx;
client_service_type, detail::disconnect_ctx ctx.props = dprops;
> { svc_ptr, std::move(ctx), std::move(handler) }
.perform(); 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)); ioc.run_for(std::chrono::milliseconds(500));
BOOST_CHECK_EQUAL(handlers_called, expected_handlers_called); BOOST_CHECK_EQUAL(handlers_called, expected_handlers_called);
} }
BOOST_AUTO_TEST_CASE(test_omitting_props) {
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 test::after;
using std::chrono_literals::operator ""ms; using namespace std::chrono;
constexpr int expected_handlers_called = 1; constexpr int expected_handlers_called = 1;
int handlers_called = 0; int handlers_called = 0;
@ -106,18 +107,18 @@ BOOST_AUTO_TEST_CASE(test_omitting_props) {
.async_run(asio::detached); .async_run(asio::detached);
asio::steady_timer timer(c.get_executor()); asio::steady_timer timer(c.get_executor());
timer.expires_after(std::chrono::milliseconds(200)); timer.expires_after(std::chrono::milliseconds(50));
timer.async_wait([&](auto) { timer.async_wait([&](auto) {
c.async_disconnect( c.async_disconnect(
disconnect_rc_e::normal_disconnection, props, disconnect_rc_e::normal_disconnection, props,
[&](error_code ec) { [&handlers_called](error_code ec) {
handlers_called++; handlers_called++;
BOOST_CHECK(!ec); BOOST_CHECK(!ec);
} }
); );
}); });
ioc.run_for(std::chrono::seconds(5)); ioc.run_for(std::chrono::seconds(2));
BOOST_CHECK_EQUAL(handlers_called, expected_handlers_called); BOOST_CHECK_EQUAL(handlers_called, expected_handlers_called);
BOOST_CHECK(broker.received_all_expected()); BOOST_CHECK(broker.received_all_expected());
} }

View File

@ -14,8 +14,7 @@ using namespace async_mqtt5;
BOOST_AUTO_TEST_SUITE(publish_send_op/*, *boost::unit_test::disabled()*/) BOOST_AUTO_TEST_SUITE(publish_send_op/*, *boost::unit_test::disabled()*/)
BOOST_AUTO_TEST_CASE(pid_overrun) {
BOOST_AUTO_TEST_CASE(test_pid_overrun) {
constexpr int expected_handlers_called = 1; constexpr int expected_handlers_called = 1;
int handlers_called = 0; int handlers_called = 0;
@ -40,228 +39,167 @@ BOOST_AUTO_TEST_CASE(test_pid_overrun) {
BOOST_CHECK_EQUAL(handlers_called, expected_handlers_called); BOOST_CHECK_EQUAL(handlers_called, expected_handlers_called);
} }
BOOST_AUTO_TEST_CASE(test_invalid_topic_names) { void run_test(
std::vector<std::string> invalid_topics = { error_code expected_ec,
"", "+", "#", const std::string& topic_name, const std::string& payload = {},
"invalid+", "invalid#", "invalid/#", "invalid/+" const publish_props& pprops = {}, const connack_props& cprops = {}
}; ) {
int expected_handlers_called = static_cast<int>(invalid_topics.size()); constexpr int expected_handlers_called = 1;
int handlers_called = 0; int handlers_called = 0;
asio::io_context ioc; asio::io_context ioc;
using client_service_type = test::test_service<asio::ip::tcp::socket>; using client_service_type = test::test_service<asio::ip::tcp::socket>;
auto svc_ptr = std::make_shared<client_service_type>(ioc.get_executor()); auto svc_ptr = std::make_shared<client_service_type>(ioc.get_executor(), cprops);
for (const auto& topic: invalid_topics) { auto handler = [&handlers_called, expected_ec](error_code ec, reason_code rc, puback_props) {
auto handler = [&handlers_called](error_code ec) { ++handlers_called;
++handlers_called;
BOOST_CHECK(ec == client::error::invalid_topic);
};
detail::publish_send_op< BOOST_CHECK(ec == expected_ec);
client_service_type, decltype(handler), qos_e::at_most_once BOOST_CHECK_EQUAL(rc, reason_codes::empty);
> { svc_ptr, std::move(handler) } };
.perform(topic, "payload", retain_e::no, {});
} detail::publish_send_op<
client_service_type, decltype(handler), qos_e::at_least_once
> { svc_ptr, std::move(handler) }
.perform(topic_name, payload, retain_e::yes, pprops);
ioc.run_for(std::chrono::milliseconds(500)); ioc.run_for(std::chrono::milliseconds(500));
BOOST_CHECK_EQUAL(handlers_called, expected_handlers_called); BOOST_CHECK_EQUAL(handlers_called, expected_handlers_called);
} }
BOOST_AUTO_TEST_CASE(test_malformed_packet) { BOOST_AUTO_TEST_CASE(invalid_topic_name_1) {
uint16_t topic_alias_max = 10; run_test(client::error::invalid_topic, "");
std::string malformed_str = std::string { 0x01 }; }
BOOST_AUTO_TEST_CASE(invalid_topic_name_2) {
run_test(client::error::invalid_topic, "+");
}
BOOST_AUTO_TEST_CASE(invalid_topic_name_3) {
run_test(client::error::invalid_topic, "#");
}
BOOST_AUTO_TEST_CASE(invalid_topic_name_4) {
run_test(client::error::invalid_topic, "invalid+");
}
BOOST_AUTO_TEST_CASE(invalid_topic_name_5) {
run_test(client::error::invalid_topic, "invalid#");
}
BOOST_AUTO_TEST_CASE(invalid_topic_name_6) {
run_test(client::error::invalid_topic, "invalid/#");
}
BOOST_AUTO_TEST_CASE(invalid_topic_name_7) {
run_test(client::error::invalid_topic, "invalid/+");
}
void run_malformed_props_test(
const publish_props& pprops, const connack_props& cprops = {}
) {
return run_test(client::error::malformed_packet, "topic", "payload", pprops, cprops);
}
BOOST_AUTO_TEST_CASE(malformed_props_1) {
publish_props pprops;
pprops[prop::response_topic] = "response#topic";
run_malformed_props_test(pprops);
}
BOOST_AUTO_TEST_CASE(malformed_props_2) {
publish_props pprops;
pprops[prop::user_property].push_back(std::string { 0x01 });
run_malformed_props_test(pprops);
}
BOOST_AUTO_TEST_CASE(malformed_props_3) {
publish_props pprops;
pprops[prop::content_type] = std::string { 0x01 };
run_malformed_props_test(pprops);
}
BOOST_AUTO_TEST_CASE(malformed_props_4) {
connack_props cprops; connack_props cprops;
cprops[prop::topic_alias_maximum] = topic_alias_max; cprops[prop::topic_alias_maximum] = uint16_t(10);
publish_props malfored_response_topic_props; publish_props pprops;
malfored_response_topic_props[prop::response_topic] = "response#topic"; pprops[prop::topic_alias] = uint16_t(0);
publish_props utf8_payload_props; run_malformed_props_test(pprops, cprops);
utf8_payload_props[prop::payload_format_indicator] = uint8_t(1);
publish_props invalid_user_props;
invalid_user_props[prop::user_property].push_back(malformed_str);
publish_props malformed_content_type_props;
malformed_content_type_props[prop::content_type] = malformed_str;
publish_props zero_topic_alias_props;
zero_topic_alias_props[prop::topic_alias] = uint16_t(0);
publish_props out_of_range_subid_props;
out_of_range_subid_props[prop::subscription_identifier] = 300'000'000;
std::vector<publish_props> testing_props = {
malfored_response_topic_props, utf8_payload_props,
invalid_user_props, malformed_content_type_props,
zero_topic_alias_props, out_of_range_subid_props
};
int expected_handlers_called = static_cast<int>(testing_props.size());
int handlers_called = 0;
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(), std::move(cprops));
BOOST_ASSERT(svc_ptr->connack_property(prop::topic_alias_maximum) == topic_alias_max);
for (const auto& props: testing_props) {
auto handler = [&handlers_called](error_code ec) {
++handlers_called;
BOOST_CHECK(ec == client::error::malformed_packet);
};
detail::publish_send_op<
client_service_type, decltype(handler), qos_e::at_most_once
> { svc_ptr, std::move(handler) }
.perform("topic", malformed_str, retain_e::no, props);
}
ioc.run_for(std::chrono::milliseconds(500));
BOOST_CHECK_EQUAL(handlers_called, expected_handlers_called);
} }
BOOST_AUTO_TEST_CASE(test_packet_too_large) { BOOST_AUTO_TEST_CASE(malformed_props_5) {
int max_packet_sz = 10; publish_props pprops;
pprops[prop::subscription_identifier] = 300'000'000;
connack_props props; run_malformed_props_test(pprops);
props[prop::maximum_packet_size] = max_packet_sz;
constexpr int expected_handlers_called = 1;
int handlers_called = 0;
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(), std::move(props)
);
BOOST_ASSERT(svc_ptr->connack_property(prop::maximum_packet_size) == max_packet_sz);
auto handler = [&handlers_called](error_code ec, reason_code rc, puback_props) {
++handlers_called;
BOOST_CHECK(ec == client::error::packet_too_large);
BOOST_CHECK_EQUAL(rc, reason_codes::empty);
};
detail::publish_send_op<
client_service_type, decltype(handler), qos_e::at_least_once
> { svc_ptr, std::move(handler) }
.perform(
"test", "payload", retain_e::no, {}
);
ioc.run_for(std::chrono::milliseconds(500));
BOOST_CHECK_EQUAL(handlers_called, expected_handlers_called);
} }
BOOST_AUTO_TEST_CASE(test_qos_not_supported) { BOOST_AUTO_TEST_CASE(malformed_utf8_payload) {
uint8_t max_qos = 0; publish_props pprops;
pprops[prop::payload_format_indicator] = uint8_t(1);
connack_props props; run_test(client::error::malformed_packet, "topic", std::string { 0x01 }, pprops);
props[prop::maximum_qos] = max_qos;
constexpr int expected_handlers_called = 1;
int handlers_called = 0;
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(), std::move(props)
);
BOOST_ASSERT(svc_ptr->connack_property(prop::maximum_qos) == max_qos);
auto handler = [&handlers_called](error_code ec, reason_code rc, puback_props) {
++handlers_called;
BOOST_CHECK(ec == client::error::qos_not_supported);
BOOST_CHECK_EQUAL(rc, reason_codes::empty);
};
detail::publish_send_op<
client_service_type, decltype(handler), qos_e::at_least_once
> { svc_ptr, std::move(handler) }
.perform(
"test", "payload", retain_e::no, {}
);
ioc.run_for(std::chrono::milliseconds(500));
BOOST_CHECK_EQUAL(handlers_called, expected_handlers_called);
} }
BOOST_AUTO_TEST_CASE(test_retain_not_available) { BOOST_AUTO_TEST_CASE(packet_too_large) {
uint8_t retain = 0; connack_props cprops;
cprops[prop::maximum_packet_size] = 10;
connack_props props; run_test(
props[prop::retain_available] = retain; client::error::packet_too_large,
"very large topic", "very large payload",
constexpr int expected_handlers_called = 1; publish_props {}, cprops
int handlers_called = 0;
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(), std::move(props)
); );
BOOST_ASSERT(svc_ptr->connack_property(prop::retain_available) == retain);
auto handler = [&handlers_called](error_code ec, reason_code rc, puback_props) {
++handlers_called;
BOOST_CHECK(ec == client::error::retain_not_available);
BOOST_CHECK_EQUAL(rc, reason_codes::empty);
};
detail::publish_send_op<
client_service_type, decltype(handler), qos_e::at_least_once
> { svc_ptr, std::move(handler) }
.perform(
"test", "payload", retain_e::yes, {}
);
ioc.run_for(std::chrono::milliseconds(500));
BOOST_CHECK_EQUAL(handlers_called, expected_handlers_called);
} }
BOOST_AUTO_TEST_CASE(test_topic_alias_maximum) { BOOST_AUTO_TEST_CASE(qos_not_supported) {
uint16_t max_topic_alias = 10; connack_props cprops;
cprops[prop::maximum_qos] = uint8_t(0);
connack_props ta_allowed_props; run_test(
ta_allowed_props[prop::topic_alias_maximum] = max_topic_alias; client::error::qos_not_supported,
"topic", "payload", publish_props {}, cprops
);
}
std::vector<connack_props> test_props = { BOOST_AUTO_TEST_CASE(retain_not_available) {
ta_allowed_props, connack_props {} /* not allowed */ connack_props cprops;
}; cprops[prop::retain_available] = uint8_t(0);
int expected_handlers_called = static_cast<int>(test_props.size()); run_test(
int handlers_called = 0; client::error::retain_not_available,
"topic", "payload", publish_props {}, cprops
);
}
asio::io_context ioc; BOOST_AUTO_TEST_CASE(topic_alias_maximum_reached_1) {
connack_props cprops;
cprops[prop::topic_alias_maximum] = uint16_t(10);
for (const auto& ca_props: test_props) { publish_props pprops;
using client_service_type = test::test_service<asio::ip::tcp::socket>; pprops[prop::topic_alias] = uint16_t(12);
auto svc_ptr = std::make_shared<client_service_type>(
ioc.get_executor(), ca_props
);
auto handler = [&handlers_called](error_code ec) { run_test(
++handlers_called; client::error::topic_alias_maximum_reached,
BOOST_CHECK(ec == client::error::topic_alias_maximum_reached); "topic", "payload", pprops, cprops
}; );
}
publish_props props; BOOST_AUTO_TEST_CASE(topic_alias_maximum_reached_2) {
props[prop::topic_alias] = uint16_t(12); connack_props cprops; /* not allowed */
detail::publish_send_op< publish_props pprops;
client_service_type, decltype(handler), qos_e::at_most_once pprops[prop::topic_alias] = uint16_t(12);
> { svc_ptr, std::move(handler) }
.perform(
"test", "payload", retain_e::yes, props
);
}
ioc.run_for(std::chrono::milliseconds(500)); run_test(
BOOST_CHECK_EQUAL(handlers_called, expected_handlers_called); client::error::topic_alias_maximum_reached,
"topic", "payload", pprops, cprops
);
} }
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()