Skip to content

Commit 989e3a6

Browse files
authored
MQTT Test: Add FRTest_GenerateRandInt and MqttTestGetTimeMs. (#51)
* Add pGetTimeMs in MqttTestParam_t and add FRTest_GenerateRandInt in platform_function.h * Add MQTT_TEST_CLIENT_IDENTIFIER for mqtt test.
1 parent 0b2b857 commit 989e3a6

File tree

4 files changed

+33
-25
lines changed

4 files changed

+33
-25
lines changed

config_template/test_param_config_template.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@
5050
* #define MQTT_SERVER_PORT (8883)
5151
*/
5252

53+
/**
54+
* @brief The client identifier for MQTT test.
55+
*
56+
* #define MQTT_TEST_CLIENT_IDENTIFIER "PLACE_HOLDER"
57+
*/
58+
59+
/**
60+
* @brief Network buffer size specified in bytes. Must be large enough to hold the maximum
61+
* anticipated MQTT payload.
62+
*
63+
* #define MQTT_TEST_NETWORK_BUFFER_SIZE ( 5000 )
64+
*/
65+
5366
/**
5467
* @brief Root certificate of the IoT Core.
5568
*

src/common/platform_function.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,11 @@ void * FRTest_MemoryAlloc( size_t size );
8585
*/
8686
void FRTest_MemoryFree( void * ptr );
8787

88+
/**
89+
* @brief To generate random number in INT format.
90+
*
91+
* @return A random number.
92+
*/
93+
int FRTest_GenerateRandInt();
94+
8895
#endif /* PLATFORM_FUNCTION_H */

src/mqtt/mqtt_test.c

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@
3333
#include <string.h>
3434
#include <time.h>
3535
#include <unistd.h>
36-
#include "demo_config.h"
36+
#include <stdio.h>
3737
#include "core_mqtt.h"
3838
#include "core_mqtt_state.h"
39-
#include "clock.h"
4039
#include "unity.h"
4140
#include "unity_fixture.h"
4241

@@ -90,12 +89,12 @@
9089
/**
9190
* @brief Sample topic filter to subscribe to.
9291
*/
93-
#define TEST_MQTT_TOPIC CLIENT_IDENTIFIER "/iot/integration/test"
92+
#define TEST_MQTT_TOPIC MQTT_TEST_CLIENT_IDENTIFIER "/iot/integration/test"
9493

9594
/**
9695
* @brief Sample topic filter to test MQTT retainted message.
9796
*/
98-
#define TEST_MQTT_RETAIN_TOPIC CLIENT_IDENTIFIER "/iot/integration/testretain"
97+
#define TEST_MQTT_RETAIN_TOPIC MQTT_TEST_CLIENT_IDENTIFIER "/iot/integration/testretain"
9998

10099
/**
101100
* @brief Length of sample topic filter.
@@ -105,27 +104,22 @@
105104
/**
106105
* @brief Sample topic filter to subscribe to.
107106
*/
108-
#define TEST_MQTT_LWT_TOPIC CLIENT_IDENTIFIER "/iot/integration/test/lwt"
107+
#define TEST_MQTT_LWT_TOPIC MQTT_TEST_CLIENT_IDENTIFIER "/iot/integration/test/lwt"
109108

110109
/**
111110
* @brief Length of sample topic filter.
112111
*/
113112
#define TEST_MQTT_LWT_TOPIC_LENGTH ( sizeof( TEST_MQTT_LWT_TOPIC ) - 1 )
114113

115-
/**
116-
* @brief Client identifier for MQTT session in the tests.
117-
*/
118-
#define TEST_CLIENT_IDENTIFIER CLIENT_IDENTIFIER
119-
120114
/**
121115
* @brief Length of the client identifier.
122116
*/
123-
#define TEST_CLIENT_IDENTIFIER_LENGTH ( sizeof( TEST_CLIENT_IDENTIFIER ) - 1u )
117+
#define TEST_CLIENT_IDENTIFIER_LENGTH ( sizeof( MQTT_TEST_CLIENT_IDENTIFIER ) - 1u )
124118

125119
/**
126120
* @brief Client identifier for use in LWT tests.
127121
*/
128-
#define TEST_CLIENT_IDENTIFIER_LWT CLIENT_IDENTIFIER "-LWT"
122+
#define TEST_CLIENT_IDENTIFIER_LWT MQTT_TEST_CLIENT_IDENTIFIER "-LWT"
129123

130124
/**
131125
* @brief Length of LWT client identifier.
@@ -378,7 +372,7 @@ static void establishMqttSession( MQTTContext_t * pContext,
378372
assert( pContext != NULL );
379373

380374
/* The network buffer must remain valid for the lifetime of the MQTT context. */
381-
static uint8_t buffer[ NETWORK_BUFFER_SIZE ];
375+
static uint8_t buffer[ MQTT_TEST_NETWORK_BUFFER_SIZE ];
382376

383377
/* Buffer for storing client ID with random integer.
384378
* Note: Size value is chosen to accommodate both LWT and non-LWT client ID
@@ -388,7 +382,7 @@ static void establishMqttSession( MQTTContext_t * pContext,
388382

389383
/* Fill the values for network buffer. */
390384
networkBuffer.pBuffer = buffer;
391-
networkBuffer.size = NETWORK_BUFFER_SIZE;
385+
networkBuffer.size = MQTT_TEST_NETWORK_BUFFER_SIZE;
392386

393387
transport.pNetworkContext = pNetworkContext;
394388
transport.send = testParam.pTransport->send;
@@ -400,7 +394,7 @@ static void establishMqttSession( MQTTContext_t * pContext,
400394
/* Initialize MQTT library. */
401395
TEST_ASSERT_EQUAL( MQTTSuccess, MQTT_Init( pContext,
402396
&transport,
403-
Clock_GetTimeMs,
397+
testParam.pGetTimeMs,
404398
eventCallback,
405399
&networkBuffer ) );
406400
}
@@ -426,7 +420,7 @@ static void establishMqttSession( MQTTContext_t * pContext,
426420
snprintf( clientIdBuffer,
427421
sizeof( clientIdBuffer ),
428422
"%d%s", clientIdRandNumber,
429-
TEST_CLIENT_IDENTIFIER );
423+
MQTT_TEST_CLIENT_IDENTIFIER );
430424
connectInfo.pClientIdentifier = clientIdBuffer;
431425
}
432426

@@ -749,8 +743,6 @@ static MQTTStatus_t publishToTopic( MQTTContext_t * pContext,
749743
*/
750744
TEST_SETUP( MqttTest )
751745
{
752-
struct timespec tp;
753-
754746
/* Reset file-scoped global variables. */
755747
receivedSubAck = false;
756748
receivedUnsubAck = false;
@@ -764,14 +756,8 @@ TEST_SETUP( MqttTest )
764756
packetTypeForDisconnection = MQTT_PACKET_TYPE_INVALID;
765757
memset( &incomingInfo, 0u, sizeof( MQTTPublishInfo_t ) );
766758

767-
/* Get current time to seed pseudo random number generator. */
768-
( void ) clock_gettime( CLOCK_REALTIME, &tp );
769-
770-
/* Seed pseudo random number generator with nanoseconds. */
771-
srand( tp.tv_nsec );
772-
773759
/* Generate a random number to use in the client identifier. */
774-
clientIdRandNumber = ( rand() % ( MAX_RAND_NUMBER_FOR_CLIENT_ID + 1u ) );
760+
clientIdRandNumber = ( FRTest_GenerateRandInt() % ( MAX_RAND_NUMBER_FOR_CLIENT_ID + 1u ) );
775761

776762
/* Establish a TCP connection with the server endpoint, then
777763
* establish TLS session on top of TCP connection. */

src/mqtt/mqtt_test.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include "transport_interface.h"
3131
#include "network_connection.h"
32+
#include "core_mqtt.h"
3233

3334
typedef struct MqttTestParam
3435
{
@@ -38,6 +39,7 @@ typedef struct MqttTestParam
3839
void * pNetworkCredentials;
3940
void * pNetworkContext;
4041
void * pSecondNetworkContext;
42+
MQTTGetCurrentTimeFunc_t pGetTimeMs; /**< @brief The getTimeFunction for MQTT_Init API. */
4143
} MqttTestParam_t;
4244

4345
/**

0 commit comments

Comments
 (0)