Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set NULL payloads and fix unit test #71

Merged
merged 5 commits into from
Sep 19, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions source/core_mqtt_serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,9 @@ static MQTTStatus_t deserializePublish( const MQTTPacketInfo_t * pIncomingPacket

LogDebug( ( "Packet identifier %hu.", *pPacketId ) );

/* Advance pointer two bytes to start of payload as in the QoS 0 case. */
pPacketIdentifierHigh += sizeof( uint16_t );

/* Packet identifier cannot be 0. */
if( *pPacketId == 0U )
{
Expand All @@ -1286,17 +1289,17 @@ static MQTTStatus_t deserializePublish( const MQTTPacketInfo_t * pIncomingPacket
{
/* Calculate the length of the payload. QoS 1 or 2 PUBLISH packets contain
* a packet identifier, but QoS 0 PUBLISH packets do not. */
if( pPublishInfo->qos == MQTTQoS0 )
{
pPublishInfo->payloadLength = ( pIncomingPacket->remainingLength - pPublishInfo->topicNameLength - sizeof( uint16_t ) );
pPublishInfo->pPayload = pPacketIdentifierHigh;
}
else
pPublishInfo->payloadLength = pIncomingPacket->remainingLength - pPublishInfo->topicNameLength - sizeof( uint16_t );

if( pPublishInfo->qos != MQTTQoS0 )
{
pPublishInfo->payloadLength = ( pIncomingPacket->remainingLength - pPublishInfo->topicNameLength - 2U * sizeof( uint16_t ) );
pPublishInfo->pPayload = pPacketIdentifierHigh + sizeof( uint16_t );
/* Two more bytes for the packet identifier. */
pPublishInfo->payloadLength -= sizeof( uint16_t );
Copy link
Contributor

@yourslab yourslab Sep 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding two bytes in advance then subtracting them if pPublishInfo->qos != MQTTQoS0, why not just add once if pPublishInfo->qos != MQTTQoS0?

Copy link
Contributor Author

@muneebahmed10 muneebahmed10 Sep 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what you mean, we're not adding any bytes to the payload length. The payload length is defined as: Remaining length - # of Topic length bytes - Topic length - # of packet ID bytes. There are two packet ID bytes for QoS > 0, and none for QoS 0. So, we need to subtract an additional 2 for QoS > 0.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh got it, I seem to have misread the code.

}

/* Set payload if it exists. */
pPublishInfo->pPayload = ( pPublishInfo->payloadLength != 0U ) ? pPacketIdentifierHigh : NULL;

LogDebug( ( "Payload length %lu.", ( unsigned long ) pPublishInfo->payloadLength ) );
}

Expand Down
86 changes: 62 additions & 24 deletions test/unit-test/core_mqtt_serializer_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
#define CLIENT_IDENTIFIER_LENGTH ( ( uint16_t ) ( sizeof( CLIENT_IDENTIFIER ) - 1 ) ) /**< @brief Length of client identifier. */

/*
* Will topic name and length to use for the MQTT API tests.
* Topic name and length to use for the MQTT API tests.
*/
#define TEST_TOPIC_NAME ( "/test/topic" ) /**< @brief An arbitrary topic name. */
#define TEST_TOPIC_NAME_LENGTH ( ( uint16_t ) ( sizeof( TEST_TOPIC_NAME ) - 1 ) ) /**< @brief Length of topic name. */
Expand All @@ -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 @@ -290,19 +284,19 @@ static void setupConnectInfo( MQTTConnectInfo_t * const pConnectInfo )
}

/**
* @brief Initialize pWillInfo using test-defined macros.
* @brief Initialize pPublishInfo using test-defined macros.
*
* @param[in] pWillInfo Last Will and Testament.
* @param[in] pPublishInfo Publish information.
*/
static void setupWillInfo( MQTTPublishInfo_t * const pWillInfo )
static void setupPublishInfo( MQTTPublishInfo_t * pPublishInfo )
{
pWillInfo->pPayload = MQTT_SAMPLE_PAYLOAD;
pWillInfo->payloadLength = MQTT_SAMPLE_PAYLOAD_LEN;
pWillInfo->pTopicName = MQTT_CLIENT_IDENTIFIER;
pWillInfo->topicNameLength = MQTT_CLIENT_IDENTIFIER_LEN;
pWillInfo->dup = true;
pWillInfo->qos = MQTTQoS0;
pWillInfo->retain = true;
pPublishInfo->pTopicName = TEST_TOPIC_NAME;
pPublishInfo->topicNameLength = TEST_TOPIC_NAME_LENGTH;
pPublishInfo->pPayload = MQTT_SAMPLE_PAYLOAD;
pPublishInfo->payloadLength = MQTT_SAMPLE_PAYLOAD_LEN;
pPublishInfo->qos = MQTTQoS0;
pPublishInfo->dup = false;
pPublishInfo->retain = false;
}

/**
Expand Down Expand Up @@ -1660,10 +1654,7 @@ 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 );
setupPublishInfo( &publishInfo );

/* Test serialization and deserialization of a QoS 0 PUBLISH. */
publishInfo.qos = MQTTQoS0;
Expand All @@ -1688,8 +1679,14 @@ 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 ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the cast to void * should not be required and looks strange to me. The point of a void * is to be a permissive pointer that accepts whatever it gets.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

/* Reset publish info since its pointers now point to our serialized buffer. */
setupPublishInfo( &publishInfo );

/* Test serialization and deserialization of a QoS 1 PUBLISH. */
publishInfo.qos = MQTTQoS1;
Expand All @@ -1711,8 +1708,13 @@ 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. */
setupPublishInfo( &publishInfo );
publishInfo.qos = MQTTQoS2;
/* Remaining length and packet size should be same as before. */
status = MQTT_SerializePublish( &publishInfo,
Expand All @@ -1725,6 +1727,40 @@ 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 ) );
publishInfo.pTopicName = TEST_TOPIC_NAME;
publishInfo.topicNameLength = TEST_TOPIC_NAME_LENGTH;
publishInfo.payloadLength = 0;
publishInfo.qos = MQTTQoS0;

/* Generate packet. */
status = MQTT_GetPublishPacketSize( &publishInfo, &remainingLength, &packetSize );
TEST_ASSERT_EQUAL_INT( MQTTSuccess, status );
TEST_ASSERT_GREATER_OR_EQUAL( packetSize, bufferSize );

status = MQTT_SerializePublish( &publishInfo,
0,
remainingLength,
&fixedBuffer );
TEST_ASSERT_EQUAL_INT( MQTTSuccess, status );

/* Deserialize packet. */
mqttPacketInfo.type = buffer[ 0 ];
mqttPacketInfo.remainingLength = ( size_t ) buffer[ 1 ];
mqttPacketInfo.pRemainingData = &buffer[ 2 ];
memset( &publishInfo, 0x00, sizeof( publishInfo ) );
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( 0, publishInfo.payloadLength );
TEST_ASSERT_NULL( publishInfo.pPayload );
}

/* ========================================================================== */
Expand Down Expand Up @@ -2285,7 +2321,9 @@ void test_MQTT_SerializeConnect_Happy_Paths()
/* Fill structs to pass into methods to be tested. */
setupNetworkBuffer( &networkBuffer );
setupConnectInfo( &connectInfo );
setupWillInfo( &willInfo );
setupPublishInfo( &willInfo );
willInfo.dup = true;
willInfo.retain = true;

/* Get MQTT connect packet size and remaining length. */
mqttStatus = MQTT_GetConnectPacketSize( &connectInfo,
Expand Down