AWS IoT Device SDK C: MQTT
MQTT 3.1.1 client library
Return to main page ↑
iot_tests_mqtt_platform.c File Reference

Tests interaction of MQTT with the lower layers, such as network and task pool. More...

#include "iot_config.h"
#include <string.h>
#include "platform/iot_clock.h"
#include "platform/iot_threads.h"
#include "iot_init.h"
#include "private/iot_mqtt_internal.h"
#include "iot_mqtt_protocol.h"
#include "iot_mqtt_lightweight.h"
#include "../src/private/iot_taskpool_internal.h"
#include "iot_test_access_mqtt.h"
#include "iot_tests_mqtt_mock.h"
#include <IOT_TEST_NETWORK_HEADER>
#include "unity_fixture.h"

Macros

#define TIMEOUT_MS   ( 400 )
 Timeout to use for the tests. This can be short, but should allow time for other threads to run.
 
#define CLIENT_IDENTIFIER   ( "test" )
 Client identifier.
 
#define CLIENT_IDENTIFIER_LENGTH   ( ( uint16_t ) ( sizeof( CLIENT_IDENTIFIER ) - 1 ) )
 Length of client identifier.
 
#define SUBSCRIPTION_CALLBACK_FUNCTION
 A non-NULL function pointer to use for subscription callback. This "function" should cause a crash if actually called. More...
 
#define PACKET_LENGTH   ( 1 )
 Length of an arbitrary packet for testing. A buffer will be allocated for it, but its contents don't matter.
 
#define SHORT_KEEP_ALIVE_MS   ( 100 )
 A short keep-alive interval to use for the keep-alive tests. It may be shorter than the minimum 1 second specified by the MQTT spec.
 
#define AWS_IOT_MQTT_SERVER   true
 Determine which MQTT server mode to test (AWS IoT or Mosquitto).
 
#define CLIENT_IDENTIFIER_MAX_LENGTH   ( 24 )
 The maximum length of an MQTT client identifier. More...
 
#define GENERATE_TOPIC_WITH_SUFFIX(bufferName, suffix)
 Generates a topic by suffixing the client identifier with a suffix. More...
 
#define TEST_TOPIC_NAME   ( "/test/topic" )
 An arbitrary topic name.
 
#define TEST_TOPIC_NAME_LENGTH   ( ( uint16_t ) ( sizeof( TEST_TOPIC_NAME ) - 1 ) )
 Length of topic name.
 
#define DUP_CHECK_RETRY_MS   ( 100 )
 When to start sending duplicate packets.
 
#define DUP_CHECK_RETRY_LIMIT   ( 3 )
 How many duplicate packets to send.
 
#define DUP_CHECK_TIMEOUT   ( 3000 )
 Total time allowed to send all duplicate packets. Duplicates are sent using an exponential backoff strategy.
 

Functions

static IotMqttError_t _mqttConnect (const IotMqttNetworkInfo_t *pNetworkInfo, const IotMqttConnectInfo_t *pConnectInfo, uint32_t timeoutMs, IotMqttConnection_t *const pMqttConnection)
 Establish an MQTT connection. Retry if enabled.
 
static IotNetworkError_t _networkCreate (IotNetworkServerInfo_t pServerInfo, IotNetworkCredentials_t pCredentialInfo, IotNetworkConnection_t *pConnection)
 Mocked network create function.
 
static IotNetworkError_t _networkSetReceiveCallback (IotNetworkConnection_t pConnection, IotNetworkReceiveCallback_t receiveCallback, void *pContext)
 Mocked network set receive callback function.
 
static size_t _networkSend (IotNetworkConnection_t pConnection, const uint8_t *pMessage, size_t messageLength)
 Mocked network send function.
 
static IotNetworkError_t _networkClose (IotNetworkConnection_t pConnection)
 Mocked network close function.
 
static IotNetworkError_t _networkDestroy (IotNetworkConnection_t pConnection)
 Mocked network destroy function.
 
static void _publishReceived (void *pArgument, IotMqttCallbackParam_t *pPublish)
 Subscription callback function. Checks for valid parameters and unblocks the main test thread.
 
static void _blockingCallback (void *pArgument, IotMqttCallbackParam_t *pPublish)
 A subscription callback function that blocks on a semaphore until signaled.
 
static void _reentrantCallback (void *pArgument, IotMqttCallbackParam_t *pOperation)
 Callback that makes additional MQTT API calls.
 
static IotMqttError_t _serializePuback (uint16_t packetIdentifier, uint8_t **pPubackPacket, size_t *pPacketSize)
 Serializer override for PUBACK that always fails.
 
static bool _waitForCount (IotMutex_t *pMutex, const int32_t *pReferenceCount, int32_t target)
 Wait for a reference count to reach a target value, subject to a timeout.
 
 TEST_GROUP (MQTT_Unit_Platform)
 Test group for MQTT platform tests.
 
 TEST_GROUP (MQTT_System_Platform)
 Test group for MQTT platform tests requiring the network.
 
 TEST_SETUP (MQTT_Unit_Platform)
 Test setup for MQTT platform tests.
 
 TEST_SETUP (MQTT_System_Platform)
 Test setup for MQTT system platform tests.
 
 TEST_TEAR_DOWN (MQTT_Unit_Platform)
 Test tear down for MQTT platform tests.
 
 TEST_TEAR_DOWN (MQTT_System_Platform)
 Test tear down for MQTT system platform tests.
 
 TEST_GROUP_RUNNER (MQTT_Unit_Platform)
 Test group runner for MQTT platform tests.
 
 TEST_GROUP_RUNNER (MQTT_System_Platform)
 Test group runner for MQTT system platform tests.
 
void TEST_MQTT_Unit_Platform_ConnectNetworkFailure_ (void)
 Tests the behavior of IotMqtt_Connect when the network fails.
 
void TEST_MQTT_Unit_Platform_ConnectScheduleFailure_ (void)
 Tests the behavior of IotMqtt_Connect when the keep-alive job fails to schedule.
 
void TEST_MQTT_Unit_Platform_DisconnectNetworkFailure_ (void)
 Tests the behavior of IotMqtt_Disconnect when the network fails.
 
void TEST_MQTT_Unit_Platform_PingreqSendFailure_ (void)
 Tests the behavior when a PINGREQ cannot be sent.
 
void TEST_MQTT_Unit_Platform_PublishScheduleFailure_ (void)
 Tests the behavior of IotMqtt_PublishAsync when scheduling fails.
 
void TEST_MQTT_Unit_Platform_PublishRetryScheduleFailure_ (void)
 Tests the behavior when a client-to-server PUBLISH retry fails to schedule.
 
void TEST_MQTT_Unit_Platform_PubackScheduleSerializeFailure_ (void)
 Tests the behavior of the client-to-server PUBACK when scheduling and serializing fail.
 
void TEST_MQTT_Unit_Platform_SubscriptionScheduleFailure_ (void)
 Tests the behavior of IotMqtt_SubscribeAsync and IotMqtt_UnsubscribeAsync when scheduling fails.
 
void TEST_MQTT_Unit_Platform_NotifyScheduleFailure_ (void)
 Tests the behavior of _IotMqtt_Notify when scheduling fails.
 
void TEST_MQTT_Unit_Platform_SingleThreaded_ (void)
 Test that MQTT can work in a single thread without the task pool.
 
void TEST_MQTT_Unit_Platform_SubscriptionReferences_ (void)
 Tests that subscriptions are properly reference counted.
 
void TEST_MQTT_Unit_Platform_SubscriptionListTooLarge_ (void)
 Test the behavior when the subscription list exceeds the size of an MQTT packet. Requires a large amount of memory not available on smaller systems.
 
void TEST_MQTT_Unit_Platform_LongUserName_ (void)
 Test the behavior when the maximum length user name. Requires a large amount of memory not available on smaller systems.
 
void TEST_MQTT_System_Platform_SubscribeCompleteReentrancy_ (void)
 Test that API functions can be invoked from a callback for a completed subscription operation.
 
void TEST_MQTT_System_Platform_IncomingPublishReentrancy_ (void)
 Test that API functions can be invoked from a callback for an incoming PUBLISH.
 

Variables

static IotMqttNetworkInfo_t _networkInfo = IOT_MQTT_NETWORK_INFO_INITIALIZER
 An IotMqttNetworkInfo_t to share among the tests.
 
static IotNetworkInterface_t _networkInterface = { 0 }
 An IotNetworkInterface_t to share among the tests.
 
static IotMqttConnection_t _mqttConnection = IOT_MQTT_CONNECTION_INITIALIZER
 An MQTT connection to share among the tests.
 
static IotMqttNetworkInfo_t _reentrantNetworkInfo = IOT_MQTT_NETWORK_INFO_INITIALIZER
 An IotMqttNetworkInfo_t to use for the re-entrancy tests.
 
static const struct IotNetworkServerInfo _serverInfo = IOT_TEST_NETWORK_SERVER_INFO_INITIALIZER
 Network server info to use for the re-entrancy tests.
 
static const IotMqttSerializer_t _mqttSerializer
 Network credential info to use for the re-entrancy tests. More...
 
static const IotMqttSerializer_t_pMqttSerializer = &_mqttSerializer
 The MQTT serializers to use in these tests.
 
static IotNetworkError_t _createStatus = IOT_NETWORK_SUCCESS
 Return value for _networkCreate.
 
static IotNetworkError_t _setReceiveCallbackStatus = IOT_NETWORK_SUCCESS
 Return value for _networkSetReceiveCallback.
 
static IotNetworkError_t _sendStatus = IOT_NETWORK_SUCCESS
 Return value for _networkSend.
 
static IotNetworkError_t _closeStatus = IOT_NETWORK_SUCCESS
 Return value for _networkClose.
 
static IotNetworkError_t _destroyStatus = IOT_NETWORK_SUCCESS
 Return value for _networkDestroy.
 
static const char _pSamplePayload []
 Filler text to publish. More...
 
static const size_t _samplePayloadLength = sizeof( _pSamplePayload ) - 1
 Length of _pSamplePayload.
 
static char _pClientIdentifier [CLIENT_IDENTIFIER_MAX_LENGTH] = { 0 }
 Buffer holding the client identifier used for the tests.
 

Detailed Description

Tests interaction of MQTT with the lower layers, such as network and task pool.

Macro Definition Documentation

◆ SUBSCRIPTION_CALLBACK_FUNCTION

#define SUBSCRIPTION_CALLBACK_FUNCTION
Value:
( ( void ( * )( void *, \
IotMqttCallbackParam_t * ) ) 0x1 )

A non-NULL function pointer to use for subscription callback. This "function" should cause a crash if actually called.

◆ CLIENT_IDENTIFIER_MAX_LENGTH

#define CLIENT_IDENTIFIER_MAX_LENGTH   ( 24 )

The maximum length of an MQTT client identifier.

When IOT_TEST_MQTT_CLIENT_IDENTIFIER is defined, this value must accommodate the length of IOT_TEST_MQTT_CLIENT_IDENTIFIER plus 4 to accommodate the Last Will and Testament test. Otherwise, this value is set to 24, which is the longest client identifier length an MQTT server is obligated to accept plus a NULL terminator.

◆ GENERATE_TOPIC_WITH_SUFFIX

#define GENERATE_TOPIC_WITH_SUFFIX (   bufferName,
  suffix 
)
Value:
char bufferName[ CLIENT_IDENTIFIER_MAX_LENGTH + sizeof( suffix ) ] = { 0 }; \
( void ) snprintf( bufferName, \
CLIENT_IDENTIFIER_MAX_LENGTH + sizeof( suffix ), \
"%s%s", \
_pClientIdentifier, \
suffix );
#define CLIENT_IDENTIFIER_MAX_LENGTH
The maximum length of an MQTT client identifier.
Definition: iot_tests_mqtt_platform.c:145

Generates a topic by suffixing the client identifier with a suffix.

Parameters
[in]bufferNameThe name of the buffer for the topic.
[in]suffixThe suffix to place at the end of the client identifier.

Variable Documentation

◆ _mqttSerializer

const IotMqttSerializer_t _mqttSerializer
static
Initial value:
=
{
.getPacketType = _IotMqtt_GetPacketType,
.getRemainingLength = _IotMqtt_GetRemainingLength,
.freePacket = _IotMqtt_FreePacket,
.serialize =
{
.publishSetDup = _IotMqtt_PublishSetDup,
},
.deserialize =
{
}
}
IotMqttError_t _IotMqtt_SerializePingreq(uint8_t **pPingreqPacket, size_t *pPacketSize)
Generate a PINGREQ packet.
Definition: iot_mqtt_serialize.c:936
IotMqttError_t _IotMqtt_SerializeDisconnect(uint8_t **pDisconnectPacket, size_t *pPacketSize)
Generate a DISCONNECT packet.
Definition: iot_mqtt_serialize.c:997
IotMqttError_t _IotMqtt_SerializePublish(const IotMqttPublishInfo_t *pPublishInfo, uint8_t **pPublishPacket, size_t *pPacketSize, uint16_t *pPacketIdentifier, uint8_t **pPacketIdentifierHigh)
Generate a PUBLISH packet from the given parameters.
Definition: iot_mqtt_serialize.c:445
IotMqttError_t _IotMqtt_SerializeUnsubscribe(const IotMqttSubscription_t *pSubscriptionList, size_t subscriptionCount, uint8_t **pUnsubscribePacket, size_t *pPacketSize, uint16_t *pPacketIdentifier)
Generate an UNSUBSCRIBE packet from the given parameters.
Definition: iot_mqtt_serialize.c:825
void _IotMqtt_FreePacket(uint8_t *pPacket)
Free a packet generated by the serializer.
Definition: iot_mqtt_serialize.c:1024
IotMqttError_t _IotMqtt_DeserializeSuback(_mqttPacket_t *pSuback)
Deserialize a SUBACK packet.
Definition: iot_mqtt_serialize.c:776
IotMqttError_t _IotMqtt_DeserializePublish(_mqttPacket_t *pPublish)
Deserialize a PUBLISH packet received from the server.
Definition: iot_mqtt_serialize.c:538
IotMqttError_t _IotMqtt_DeserializePuback(_mqttPacket_t *pPuback)
Deserialize a PUBACK packet.
Definition: iot_mqtt_serialize.c:666
void _IotMqtt_PublishSetDup(uint8_t *pPublishPacket, uint8_t *pPacketIdentifierHigh, uint16_t *pNewPacketIdentifier)
Set the DUP bit in a QoS 1 PUBLISH packet.
Definition: iot_mqtt_serialize.c:503
IotMqttError_t _IotMqtt_DeserializeUnsuback(_mqttPacket_t *pUnsuback)
Deserialize a UNSUBACK packet.
Definition: iot_mqtt_serialize.c:886
uint8_t _IotMqtt_GetPacketType(IotNetworkConnection_t pNetworkConnection, const IotNetworkInterface_t *pNetworkInterface)
Get the MQTT packet type from a stream of bytes off the network.
Definition: iot_mqtt_serialize.c:210
IotMqttError_t _IotMqtt_SerializeSubscribe(const IotMqttSubscription_t *pSubscriptionList, size_t subscriptionCount, uint8_t **pSubscribePacket, size_t *pPacketSize, uint16_t *pPacketIdentifier)
Generate a SUBSCRIBE packet from the given parameters.
Definition: iot_mqtt_serialize.c:715
IotMqttError_t _IotMqtt_DeserializePingresp(_mqttPacket_t *pPingresp)
Deserialize a PINGRESP packet.
Definition: iot_mqtt_serialize.c:963
size_t _IotMqtt_GetRemainingLength(IotNetworkConnection_t pNetworkConnection, const IotNetworkInterface_t *pNetworkInterface)
Get the remaining length from a stream of bytes off the network.
Definition: iot_mqtt_serialize.c:225
IotMqttError_t _IotMqtt_SerializePuback(uint16_t packetIdentifier, uint8_t **pPubackPacket, size_t *pPacketSize)
Generate a PUBACK packet for the given packet identifier.
Definition: iot_mqtt_serialize.c:630
IotMqttError_t _IotMqtt_DeserializeConnack(_mqttPacket_t *pConnack)
Deserialize a CONNACK packet.
Definition: iot_mqtt_serialize.c:331
IotMqttError_t _IotMqtt_SerializeConnect(const IotMqttConnectInfo_t *pConnectInfo, uint8_t **pConnectPacket, size_t *pPacketSize)
Generate a CONNECT packet from the given parameters.
Definition: iot_mqtt_serialize.c:281

Network credential info to use for the re-entrancy tests.

Function pointers to the default MQTT serializers.

◆ _pSamplePayload

const char _pSamplePayload[]
static
Initial value:
=
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor"
" incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis "
"nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. "
"Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu"
" fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in"
" culpa qui officia deserunt mollit anim id est laborum."
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor"
" incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis "
"nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. "
"Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu"
" fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in"
" culpa qui officia deserunt mollit anim id est laborum."

Filler text to publish.