From bd7a25e8ba8735b1e176ad89ce38518404e0df0e Mon Sep 17 00:00:00 2001 From: Alex Robenko Date: Thu, 7 Dec 2023 19:19:53 +1000 Subject: [PATCH] More bare metal testing. --- client/test/unit/CMakeLists.txt | 2 + client/test/unit/UnitTestBmPublish.th | 90 +++++++++++++ client/test/unit/UnitTestBmReceive.th | 181 ++++++++++++++++++++++++++ 3 files changed, 273 insertions(+) create mode 100644 client/test/unit/UnitTestBmPublish.th create mode 100644 client/test/unit/UnitTestBmReceive.th diff --git a/client/test/unit/CMakeLists.txt b/client/test/unit/CMakeLists.txt index cc9a210..04d7478 100644 --- a/client/test/unit/CMakeLists.txt +++ b/client/test/unit/CMakeLists.txt @@ -68,4 +68,6 @@ if (TARGET cc::cc_mqtt5_bm_client) cc_mqtt5_client_add_unit_test(UnitTestBmClient ${BM_BASE_LIB_NAME}) cc_mqtt5_client_add_unit_test(UnitTestBmConnect ${BM_BASE_LIB_NAME}) + cc_mqtt5_client_add_unit_test(UnitTestBmPublish ${BM_BASE_LIB_NAME}) + cc_mqtt5_client_add_unit_test(UnitTestBmReceive ${BM_BASE_LIB_NAME}) endif () diff --git a/client/test/unit/UnitTestBmPublish.th b/client/test/unit/UnitTestBmPublish.th new file mode 100644 index 0000000..48bdec5 --- /dev/null +++ b/client/test/unit/UnitTestBmPublish.th @@ -0,0 +1,90 @@ +#include "UnitTestBmBase.h" +#include "UnitTestPropsHandler.h" +#include "UnitTestProtocolDefs.h" + +#include + +class UnitTestBmPublish : public CxxTest::TestSuite, public UnitTestBmBase +{ +public: + void test1(); + +private: + virtual void setUp() override + { + unitTestSetUp(); + } + + virtual void tearDown() override + { + unitTestTearDown(); + } +}; + +void UnitTestBmPublish::test1() +{ + // Qos0 publish with properties + auto* client = unitTestAllocAndInitClient(); + unitTestPerformPubTopicAliasConnect(client, __FUNCTION__, 10); + TS_ASSERT(unitTestIsConnected(client)); + + const std::string Topic("some/topic"); + auto ec = unitTestPubTopicAliasAlloc(client, Topic.c_str(), 1U); + TS_ASSERT_EQUALS(ec, CC_Mqtt5ErrorCode_NotSupported); + + auto* publish = unitTestPublishPrepare(client, nullptr); + TS_ASSERT_DIFFERS(publish, nullptr); + + const UnitTestData Data = { 0x1, 0x2, 0x3, 0x4, 0x5}; + const CC_Mqtt5QoS Qos = CC_Mqtt5QoS_AtMostOnceDelivery; + const bool Retain = true; + + auto config = CC_Mqtt5PublishBasicConfig(); + unitTestPublishInitConfigBasic(&config); + + config.m_topic = Topic.c_str(); + config.m_data = &Data[0]; + config.m_dataLen = static_cast(Data.size()); + config.m_qos = Qos; + TS_ASSERT_EQUALS(config.m_topicAliasPref, CC_Mqtt5TopicAliasPreference_UseAliasIfAvailable); + config.m_retain = Retain; + + ec = unitTestPublishConfigBasic(publish, &config); + TS_ASSERT_EQUALS(ec, CC_Mqtt5ErrorCode_Success); + + const std::string PubUserPropKey1 = "Key1"; + const std::string PubUserPropVal1 = "Val1"; + auto userProp1 = CC_Mqtt5UserProp(); + userProp1.m_key = PubUserPropKey1.c_str(); + userProp1.m_value = PubUserPropVal1.c_str(); + ec = unitTestPublishAddUserProp(publish, &userProp1); + TS_ASSERT_EQUALS(ec, CC_Mqtt5ErrorCode_NotSupported); + + const std::string ContentType("ContentType"); + const std::string ResponseTopic("ResponseTopic"); + const UnitTestData CorrelationData = {0x11, 0x22, 0x33, 0x44}; + const unsigned MsgExpiry = 10; + const CC_Mqtt5PayloadFormat Format = CC_Mqtt5PayloadFormat_Utf8; + + auto extra = CC_Mqtt5PublishExtraConfig(); + unitTestPublishInitConfigExtra(&extra); + extra.m_contentType = ContentType.c_str(); + extra.m_responseTopic = ResponseTopic.c_str(); + extra.m_correlationData = &CorrelationData[0]; + extra.m_correlationDataLen = static_cast(CorrelationData.size()); + extra.m_messageExpiryInterval = MsgExpiry; + extra.m_format = Format; + ec = unitTestPublishConfigExtra(publish, &extra); + TS_ASSERT_EQUALS(ec, CC_Mqtt5ErrorCode_Success); + + ec = unitTestSendPublish(publish); + TS_ASSERT_EQUALS(ec, CC_Mqtt5ErrorCode_Success); + + TS_ASSERT(unitTestIsPublishComplete()); + auto& pubackInfo = unitTestPublishResponseInfo(); + TS_ASSERT_EQUALS(pubackInfo.m_status, CC_Mqtt5AsyncOpStatus_Complete); + unitTestPopPublishResponseInfo(); + + auto sentMsg = unitTestGetSentMessage(); + TS_ASSERT(sentMsg); +} diff --git a/client/test/unit/UnitTestBmReceive.th b/client/test/unit/UnitTestBmReceive.th new file mode 100644 index 0000000..4c21eed --- /dev/null +++ b/client/test/unit/UnitTestBmReceive.th @@ -0,0 +1,181 @@ +#include "UnitTestBmBase.h" +#include "UnitTestPropsHandler.h" +#include "UnitTestProtocolDefs.h" + +#include + +class UnitTestBmReceive : public CxxTest::TestSuite, public UnitTestBmBase +{ +public: + void test1(); + void test2(); + void test3(); + +private: + virtual void setUp() override + { + unitTestSetUp(); + } + + virtual void tearDown() override + { + unitTestTearDown(); + } +}; + +void UnitTestBmReceive::test1() +{ + // Simple receive of Qos0 + auto* client = unitTestAllocAndInitClient(); + unitTestPerformBasicConnect(client, __FUNCTION__); + TS_ASSERT(unitTestIsConnected(client)); + + unitTestPerformBasicSubscribe(client, "#"); + unitTestTick(1000); + + const std::string Topic = "some/topic"; + const UnitTestData Data = {'h', 'e', 'l', 'l', 'o'}; + + UnitTestPublishMsg publishMsg; + publishMsg.field_topic().value() = Topic; + publishMsg.field_payload().value() = Data; + publishMsg.doRefresh(); + unitTestReceiveMessage(publishMsg); + + TS_ASSERT(unitTestHasMessageRecieved()); + auto& msgInfo = unitTestReceivedMessageInfo(); + TS_ASSERT_EQUALS(msgInfo.m_topic, Topic); + TS_ASSERT_EQUALS(msgInfo.m_data, Data); + TS_ASSERT(msgInfo.m_subIds.empty()); + TS_ASSERT_EQUALS(msgInfo.m_qos, CC_Mqtt5QoS_AtMostOnceDelivery); + unitTestPopReceivedMessageInfo(); +} + +void UnitTestBmReceive::test2() +{ + // Testing ignore of the user properties + auto* client = unitTestAllocAndInitClient(); + unitTestPerformBasicConnect(client, __FUNCTION__); + TS_ASSERT(unitTestIsConnected(client)); + + unitTestPerformBasicSubscribe(client, "#"); + unitTestTick(1000); + + const std::string Topic = "some/topic"; + const UnitTestData Data = {'h', 'e', 'l', 'l', 'o'}; + const std::string UserPropKey1("Key1"); + const std::string UserPropVal1("Val1"); + + UnitTestPublishMsg publishMsg; + publishMsg.field_topic().value() = Topic; + publishMsg.field_payload().value() = Data; + + auto& propsVec = publishMsg.field_properties().value(); + do { + propsVec.resize(propsVec.size() + 1U); + auto& field = propsVec.back().initField_userProperty(); + field.field_value().field_first().setValue(UserPropKey1); + field.field_value().field_second().setValue(UserPropVal1); + } while (false); + + publishMsg.doRefresh(); + unitTestReceiveMessage(publishMsg); + + TS_ASSERT(unitTestHasMessageRecieved()); + auto& msgInfo = unitTestReceivedMessageInfo(); + TS_ASSERT_EQUALS(msgInfo.m_topic, Topic); + TS_ASSERT_EQUALS(msgInfo.m_data, Data); + TS_ASSERT(msgInfo.m_userProps.empty()); + TS_ASSERT(msgInfo.m_subIds.empty()); + TS_ASSERT_EQUALS(msgInfo.m_qos, CC_Mqtt5QoS_AtMostOnceDelivery); + unitTestPopReceivedMessageInfo(); +} + +void UnitTestBmReceive::test3() +{ + // Testing ignore of the too many properties + auto* client = unitTestAllocAndInitClient(); + unitTestPerformBasicConnect(client, __FUNCTION__); + TS_ASSERT(unitTestIsConnected(client)); + + unitTestPerformBasicSubscribe(client, "#"); + unitTestTick(1000); + + const std::string Topic = "some/topic"; + const UnitTestData Data = {'h', 'e', 'l', 'l', 'o'}; + const unsigned MessageExpiryInterval = 10; + const std::string ResponseTopic = "ResponseTopic"; + const UnitTestData CorrelationData = {0x21, 0x32, 0x43, 0x54}; + const CC_Mqtt5PayloadFormat Format = CC_Mqtt5PayloadFormat_Utf8; + const bool Retain = true; + const std::string ContentType("ContentType"); + const std::string UserPropKey1("Key1"); + const std::string UserPropVal1("Val1"); + const unsigned SubId = 1; + + UnitTestPublishMsg publishMsg; + publishMsg.transportField_flags().field_retain().setBitValue_bit(Retain); + publishMsg.field_topic().value() = Topic; + publishMsg.field_payload().value() = Data; + + auto& propsVec = publishMsg.field_properties().value(); + do { + propsVec.resize(propsVec.size() + 1U); + auto& field = propsVec.back().initField_payloadFormatIndicator(); + field.field_value().setValue(Format); + } while (false); + + do { + propsVec.resize(propsVec.size() + 1U); + auto& field = propsVec.back().initField_messageExpiryInterval(); + field.field_value().setValue(MessageExpiryInterval); + } while (false); + + do { + propsVec.resize(propsVec.size() + 1U); + auto& field = propsVec.back().initField_responseTopic(); + field.field_value().setValue(ResponseTopic); + } while (false); + + do { + propsVec.resize(propsVec.size() + 1U); + auto& field = propsVec.back().initField_correlationData(); + field.field_value().setValue(CorrelationData); + } while (false); + + do { + propsVec.resize(propsVec.size() + 1U); + auto& field = propsVec.back().initField_contentType(); + field.field_value().setValue(ContentType); + } while (false); + + do { + propsVec.resize(propsVec.size() + 1U); + auto& field = propsVec.back().initField_subscriptionId(); + field.field_value().setValue(SubId); + } while (false); + + do { + propsVec.resize(propsVec.size() + 1U); + auto& field = propsVec.back().initField_userProperty(); + field.field_value().field_first().setValue(UserPropKey1); + field.field_value().field_second().setValue(UserPropVal1); + } while (false); + + publishMsg.doRefresh(); + unitTestReceiveMessage(publishMsg); + + TS_ASSERT(unitTestHasMessageRecieved()); + auto& msgInfo = unitTestReceivedMessageInfo(); + TS_ASSERT_EQUALS(msgInfo.m_topic, Topic); + TS_ASSERT_EQUALS(msgInfo.m_data, Data); + TS_ASSERT_EQUALS(msgInfo.m_format, Format); + TS_ASSERT_EQUALS(msgInfo.m_messageExpiryInterval, MessageExpiryInterval); + TS_ASSERT_EQUALS(msgInfo.m_responseTopic, ResponseTopic); + TS_ASSERT_EQUALS(msgInfo.m_correlationData, CorrelationData); + TS_ASSERT(msgInfo.m_userProps.empty()); // expected to be ignored + TS_ASSERT(msgInfo.m_subIds.empty()); // expected to be ignored + TS_ASSERT_EQUALS(msgInfo.m_qos, CC_Mqtt5QoS_AtMostOnceDelivery); + TS_ASSERT_EQUALS(msgInfo.m_retained, Retain); + unitTestPopReceivedMessageInfo(); +}