diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 702cc4f82..dd2bb79f0 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -236,6 +236,7 @@ if(NOT BYO_CRYPTO) add_net_test_case(Mqtt5to3AdapterWithIoTConnectionThroughMqtt5) add_net_test_case(Mqtt5to3AdapterDirectConnectionWithMutualTLSThroughMqtt5) add_net_test_case(Mqtt5to3AdapterOperations) + add_net_test_case(Mqtt5to3AdapterNullPubAck) add_net_test_case(Mqtt5to3AdapterMultipleAdapters) endif() diff --git a/tests/Mqtt5ClientTest.cpp b/tests/Mqtt5ClientTest.cpp index d4510156e..0d3ae46fd 100644 --- a/tests/Mqtt5ClientTest.cpp +++ b/tests/Mqtt5ClientTest.cpp @@ -3166,6 +3166,50 @@ static int s_TestMqtt5to3AdapterOperations(Aws::Crt::Allocator *allocator, void } AWS_TEST_CASE(Mqtt5to3AdapterOperations, s_TestMqtt5to3AdapterOperations) +/* + * [Mqtt5to3Adapter-UC11] Test s_TestMqtt5to3AdapterNullPubAck + * The unit test would have memory leak if the callback data for incomplete publish was not released. + */ +static int s_TestMqtt5to3AdapterNullPubAck(Aws::Crt::Allocator *allocator, void *) +{ + Mqtt5TestEnvVars mqtt5TestVars(allocator, MQTT5CONNECT_IOT_CORE); + if (!mqtt5TestVars) + { + printf("Environment Variables are not set for the test, skip the test"); + return AWS_OP_SKIP; + } + + ApiHandle apiHandle(allocator); + + Aws::Iot::Mqtt5ClientBuilder *builder = Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath( + mqtt5TestVars.m_hostname_string, + mqtt5TestVars.m_certificate_path_string.c_str(), + mqtt5TestVars.m_private_key_path_string.c_str(), + allocator); + ASSERT_TRUE(builder); + + String testUUID = Aws::Crt::UUID().ToString(); + String testTopic = "test/MQTT5to3Adapter_" + testUUID; + ByteBuf testPayload = Aws::Crt::ByteBufFromCString("PUBLISH ME!"); + + std::shared_ptr mqtt5Client = builder->Build(); + ASSERT_TRUE(mqtt5Client); + // Created a Mqtt311 Connection from mqtt5Client. The options are setup by the builder. + std::shared_ptr mqttConnection = + Mqtt::MqttConnection::NewConnectionFromMqtt5Client(mqtt5Client); + ASSERT_TRUE(mqttConnection); + + // Publish an offline message to create an incomplete publish operation + mqttConnection->Publish(testTopic.c_str(), Mqtt::QOS::AWS_MQTT_QOS_AT_LEAST_ONCE, false, testPayload, NULL); + + delete builder; + + // If the incomplete operation callback was not called, there would be a memory leak as the callback data was not + // released + return AWS_OP_SUCCESS; +} +AWS_TEST_CASE(Mqtt5to3AdapterNullPubAck, s_TestMqtt5to3AdapterNullPubAck) + /* * [Mqtt5to3Adapter-UC11] Test one mqtt5 client with multiple adapters */