Skip to content

Commit

Permalink
unit-testing: Refactor mocks
Browse files Browse the repository at this point in the history
Makes CMakeLists.txt use `BUILD_TESTING` as well as
`CMAKE_CROSSCOMPILING`.

Signed-off-by: Reuben Cartwright <[email protected]>
  • Loading branch information
RC-Repositories authored and aggarg committed Sep 20, 2024
1 parent 1679f0c commit 78c58e1
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 52 deletions.
84 changes: 48 additions & 36 deletions components/aws_iot/coremqtt_agent/integration/src/mqtt_agent_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,54 @@ extern SubscriptionElement_t xGlobalSubscriptionList[ SUBSCRIPTION_MANAGER_MAX_S

/*-----------------------------------------------------------*/

/**
* @brief Retry logic to establish a connection to the MQTT broker.
*
* If the connection fails, keep retrying with exponentially increasing
* timeout value, until max retries, max timeout or successful connect.
*
* @param[in] pNetworkContext Network context to connect on.
* @return int pdFALSE if connection failed after retries.
*/
STATIC BaseType_t prvSocketConnect( NetworkContext_t * pxNetworkContext );

/**
* @brief Initializes an MQTT context, including transport interface and
* network buffer.
*
* @return `MQTTSuccess` if the initialization succeeds, else `MQTTBadParameter`.
*/
STATIC MQTTStatus_t prvMQTTInit( void );

/**
* @brief Sends an MQTT Connect packet over the already connected TCP socket.
* This function takes no inputs, but does rely on the global 'xConnectInfo'
* variable, which contains information such as passwords and user identifiers,
* and whether to start a clean MQTT session.
*
* @return `MQTTSuccess` if connection succeeds, else appropriate error code
* from MQTT_Connect.
*/
STATIC MQTTStatus_t prvMQTTConnect( void );

/**
* @brief Disconnects from the MQTT broker.
* Initiates an MQTT disconnect and then teardown underlying TCP connection.
*/
STATIC void prvDisconnectFromMQTTBroker( void );

/**
* @brief Task for MQTT agent.
* Task runs MQTT agent command loop, which returns only when the user disconnects
* MQTT, terminates agent, or the MQTT connection is broken. If the MQTT connection is broken, the task
* tries to reconnect to the broker.
*
* @param[in] pParam Can be used to pass down functionality to the agent task
*/
STATIC void prvMQTTAgentTask( void * pParam );

/*-----------------------------------------------------------*/

STATIC uint32_t prvGetTimeMs( void )
{
TickType_t xTickCount = 0;
Expand Down Expand Up @@ -243,15 +291,6 @@ STATIC UBaseType_t prvGetRandomNumber( void )
return uxRandomValue;
}

/**
* @brief Retry logic to establish a connection to the MQTT broker.
*
* If the connection fails, keep retrying with exponentially increasing
* timeout value, until max retries, max timeout or successful connect.
*
* @param[in] pNetworkContext Network context to connect on.
* @return int pdFALSE if connection failed after retries.
*/
STATIC BaseType_t prvSocketConnect( NetworkContext_t * pxNetworkContext )
{
BaseType_t xConnected = pdFAIL;
Expand Down Expand Up @@ -391,12 +430,6 @@ STATIC void prvReSubscriptionCommandCallback( MQTTAgentCommandContext_t * pxComm
}
}

/**
* @brief Initializes an MQTT context, including transport interface and
* network buffer.
*
* @return `MQTTSuccess` if the initialization succeeds, else `MQTTBadParameter`.
*/
STATIC MQTTStatus_t prvMQTTInit( void )
{
TransportInterface_t xTransport = { 0 };
Expand Down Expand Up @@ -512,15 +545,6 @@ STATIC MQTTStatus_t prvHandleResubscribe( void )
return xResult;
}

/**
* @brief Sends an MQTT Connect packet over the already connected TCP socket.
*
* @param[in] pxMQTTContext MQTT context pointer.
* @param[in] xCleanSession If a clean session should be established.
*
* @return `MQTTSuccess` if connection succeeds, else appropriate error code
* from MQTT_Connect.
*/
STATIC MQTTStatus_t prvMQTTConnect( void )
{
MQTTStatus_t xResult;
Expand Down Expand Up @@ -593,10 +617,6 @@ STATIC void prvDisconnectCommandCallback( MQTTAgentCommandContext_t * pxCommandC
}
}

/**
* @brief Disconnects from the MQTT broker.
* Initiates an MQTT disconnect and then teardown underlying TCP connection.
*/
STATIC void prvDisconnectFromMQTTBroker( void )
{
static MQTTAgentCommandContext_t xCommandContext = { 0 };
Expand Down Expand Up @@ -629,14 +649,6 @@ STATIC void prvDisconnectFromMQTTBroker( void )
}
}

/**
* @brief Task for MQTT agent.
* Task runs MQTT agent command loop, which returns only when the user disconnects
* MQTT, terminates agent, or the mqtt connection is broken. If the mqtt connection is broken, the task
* tries to reconnect to the broker.
*
* @param[in] pParam Can be used to pass down functionality to the agent task
*/
STATIC void prvMQTTAgentTask( void * pParam )
{
BaseType_t xResult;
Expand Down
4 changes: 3 additions & 1 deletion components/connectivity/iot_socket/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
# <[email protected]>
# SPDX-License-Identifier: MIT

if(CMAKE_CROSSCOMPILING)
if(BUILD_TESTING AND NOT CMAKE_CROSSCOMPILING)
# left empty for future mocks.
else ()
set(iot_socket_SOURCE_DIR
${CMAKE_CURRENT_LIST_DIR}/library
CACHE INTERNAL
Expand Down
22 changes: 11 additions & 11 deletions components/freertos_kernel/library_mocks/inc/FreeRTOS.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,20 @@

typedef int StaticQueue_t;

/*
Definitions found in FreeTOSConfig.h.
Because for testing `freertos_command_pool.c` we cannot
directly include FreeRTOSConfig.h (this causes build failure),
nor can we prototype the configAssert macro from within the file.
*/
/*
* Definitions found in FreeTOSConfig.h.
* Because for testing `freertos_command_pool.c` we cannot
* directly include FreeRTOSConfig.h (this causes build failure),
* nor can we prototype the configAssert macro from within the file.
*/

#ifndef FREERTOS_CONFIG_H
#define FREERTOS_CONFIG_H
#define FREERTOS_CONFIG_H

DECLARE_FAKE_VOID_FUNC( vAssertCalled,
const char *,
unsigned long );
#define configASSERT( x ) if( ( x ) == 0 ) vAssertCalled( __FILE__, __LINE__ );
DECLARE_FAKE_VOID_FUNC( vAssertCalled,
const char *,
unsigned long );
#define configASSERT( x ) if( ( x ) == 0 ) vAssertCalled( __FILE__, __LINE__ );

#endif /* FREERTOS_CONFIG_H */

Expand Down
10 changes: 6 additions & 4 deletions components/security/freertos_pkcs11_psa/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
# <[email protected]>
# SPDX-License-Identifier: MIT

if(CMAKE_CROSSCOMPILING)
if(BUILD_TESTING AND NOT CMAKE_CROSSCOMPILING)
# left empty for future mocks.
else ()
set(freertos_pkcs11_psa_SOURCE_DIR
${CMAKE_CURRENT_LIST_DIR}/library
CACHE INTERNAL
"Path to FreeRTOS PKCS#11 to PSA shim layer source code"
${CMAKE_CURRENT_LIST_DIR}/library
CACHE INTERNAL
"Path to FreeRTOS PKCS#11 to PSA shim layer source code"
)

add_subdirectory(integration)
Expand Down

0 comments on commit 78c58e1

Please sign in to comment.