Skip to content

Commit

Permalink
Unit test for removing callback severance on 5-to-3 adapter (#553)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiazhvera authored Oct 25, 2023
1 parent 8fe495c commit f098dcb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
44 changes: 44 additions & 0 deletions tests/Mqtt5ClientTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Aws::Crt::Mqtt5::Mqtt5Client> mqtt5Client = builder->Build();
ASSERT_TRUE(mqtt5Client);
// Created a Mqtt311 Connection from mqtt5Client. The options are setup by the builder.
std::shared_ptr<Aws::Crt::Mqtt::MqttConnection> 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
*/
Expand Down

0 comments on commit f098dcb

Please sign in to comment.