Skip to content

Commit

Permalink
Fix unit tests for deserialize publish
Browse files Browse the repository at this point in the history
  • Loading branch information
muneebahmed10 committed Sep 18, 2020
1 parent a9a3391 commit 432bf1c
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions test/unit-test/core_mqtt_serializer_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,15 @@
#define MQTT_TEST_PASSWORD "password"
#define MQTT_TEST_PASSWORD_LEN ( sizeof( MQTT_TEST_PASSWORD ) - 1 )

/**
* @brief Test-defined macro for MQTT topic.
*/
#define MQTT_TEST_TOPIC "topic"
#define MQTT_TEST_TOPIC_LEN ( sizeof( MQTT_TEST_TOPIC ) - 1 )

/**
* @brief Length of the client identifier.
*/
#define MQTT_CLIENT_IDENTIFIER_LEN ( sizeof( MQTT_CLIENT_IDENTIFIER ) - 1 )

/**
* @brief Payload for will info.
* @brief Sample payload.
*/
#define MQTT_SAMPLE_PAYLOAD "payload"
#define MQTT_SAMPLE_PAYLOAD "Hello World"
#define MQTT_SAMPLE_PAYLOAD_LEN ( sizeof( MQTT_SAMPLE_PAYLOAD ) - 1 )

/* MQTT CONNECT flags. */
Expand Down Expand Up @@ -1660,10 +1654,10 @@ void test_MQTT_DeserializePublish( void )

/* Create a PUBLISH packet to test. */
memset( &publishInfo, 0x00, sizeof( publishInfo ) );
publishInfo.pTopicName = "/test/topic";
publishInfo.topicNameLength = ( uint16_t ) strlen( publishInfo.pTopicName );
publishInfo.pPayload = "Hello World";
publishInfo.payloadLength = ( uint16_t ) strlen( publishInfo.pPayload );
publishInfo.pTopicName = TEST_TOPIC_NAME;
publishInfo.topicNameLength = TEST_TOPIC_NAME_LENGTH;
publishInfo.pPayload = MQTT_SAMPLE_PAYLOAD;
publishInfo.payloadLength = MQTT_SAMPLE_PAYLOAD_LEN;

/* Test serialization and deserialization of a QoS 0 PUBLISH. */
publishInfo.qos = MQTTQoS0;
Expand All @@ -1688,8 +1682,18 @@ void test_MQTT_DeserializePublish( void )
mqttPacketInfo.pRemainingData = &buffer[ 2 ];
status = MQTT_DeserializePublish( &mqttPacketInfo, &packetIdentifier, &publishInfo );
TEST_ASSERT_EQUAL_INT( MQTTSuccess, status );
TEST_ASSERT_EQUAL_INT( TEST_TOPIC_NAME_LENGTH, publishInfo.topicNameLength );
TEST_ASSERT_EQUAL_MEMORY( TEST_TOPIC_NAME, publishInfo.pTopicName, TEST_TOPIC_NAME_LENGTH );
TEST_ASSERT_EQUAL_INT( MQTT_SAMPLE_PAYLOAD_LEN, publishInfo.payloadLength );
TEST_ASSERT_EQUAL_MEMORY( MQTT_SAMPLE_PAYLOAD, publishInfo.pPayload, MQTT_SAMPLE_PAYLOAD_LEN );

memset( ( void * ) &mqttPacketInfo, 0x00, sizeof( mqttPacketInfo ) );
/* Reset publish info since its pointers now point to our serialized buffer. */
memset( &publishInfo, 0x00, sizeof( publishInfo ) );
publishInfo.pTopicName = TEST_TOPIC_NAME;
publishInfo.topicNameLength = TEST_TOPIC_NAME_LENGTH;
publishInfo.pPayload = MQTT_SAMPLE_PAYLOAD;
publishInfo.payloadLength = MQTT_SAMPLE_PAYLOAD_LEN;

/* Test serialization and deserialization of a QoS 1 PUBLISH. */
publishInfo.qos = MQTTQoS1;
Expand All @@ -1711,8 +1715,17 @@ void test_MQTT_DeserializePublish( void )
status = MQTT_DeserializePublish( &mqttPacketInfo, &packetIdentifier, &publishInfo );
TEST_ASSERT_EQUAL_INT( MQTTSuccess, status );
TEST_ASSERT_TRUE( publishInfo.dup );
TEST_ASSERT_EQUAL_INT( TEST_TOPIC_NAME_LENGTH, publishInfo.topicNameLength );
TEST_ASSERT_EQUAL_MEMORY( TEST_TOPIC_NAME, publishInfo.pTopicName, TEST_TOPIC_NAME_LENGTH );
TEST_ASSERT_EQUAL_INT( MQTT_SAMPLE_PAYLOAD_LEN, publishInfo.payloadLength );
TEST_ASSERT_EQUAL_MEMORY( MQTT_SAMPLE_PAYLOAD, publishInfo.pPayload, MQTT_SAMPLE_PAYLOAD_LEN );

/* QoS 2 PUBLISH. */
memset( &publishInfo, 0x00, sizeof( publishInfo ) );
publishInfo.pTopicName = TEST_TOPIC_NAME;
publishInfo.topicNameLength = TEST_TOPIC_NAME_LENGTH;
publishInfo.pPayload = MQTT_SAMPLE_PAYLOAD;
publishInfo.payloadLength = MQTT_SAMPLE_PAYLOAD_LEN;
publishInfo.qos = MQTTQoS2;
/* Remaining length and packet size should be same as before. */
status = MQTT_SerializePublish( &publishInfo,
Expand All @@ -1725,6 +1738,10 @@ void test_MQTT_DeserializePublish( void )
mqttPacketInfo.pRemainingData = &buffer[ 2 ];
status = MQTT_DeserializePublish( &mqttPacketInfo, &packetIdentifier, &publishInfo );
TEST_ASSERT_EQUAL_INT( MQTTSuccess, status );
TEST_ASSERT_EQUAL_INT( TEST_TOPIC_NAME_LENGTH, publishInfo.topicNameLength );
TEST_ASSERT_EQUAL_MEMORY( TEST_TOPIC_NAME, publishInfo.pTopicName, TEST_TOPIC_NAME_LENGTH );
TEST_ASSERT_EQUAL_INT( MQTT_SAMPLE_PAYLOAD_LEN, publishInfo.payloadLength );
TEST_ASSERT_EQUAL_MEMORY( MQTT_SAMPLE_PAYLOAD, publishInfo.pPayload, MQTT_SAMPLE_PAYLOAD_LEN );

/* Zero length payload. */
memset( &publishInfo, 0x00, sizeof( publishInfo ) );
Expand Down Expand Up @@ -1752,7 +1769,7 @@ void test_MQTT_DeserializePublish( void )
status = MQTT_DeserializePublish( &mqttPacketInfo, &packetIdentifier, &publishInfo );
TEST_ASSERT_EQUAL_INT( MQTTSuccess, status );
TEST_ASSERT_EQUAL_INT( TEST_TOPIC_NAME_LENGTH, publishInfo.topicNameLength );
TEST_ASSERT_EQUAL_STRING( TEST_TOPIC_NAME, publishInfo.pTopicName );
TEST_ASSERT_EQUAL_MEMORY( TEST_TOPIC_NAME, publishInfo.pTopicName, TEST_TOPIC_NAME_LENGTH );
TEST_ASSERT_EQUAL_INT( 0, publishInfo.payloadLength );
TEST_ASSERT_NULL( publishInfo.pPayload );
}
Expand Down

0 comments on commit 432bf1c

Please sign in to comment.