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

Conversation

muneebahmed10
Copy link
Contributor

Description:
Set payload to NULL for zero length payloads:

  • MQTT_DeserializePublish sets pPayload to point to the byte after the topic name (or packet ID for QoS > 0). If there is no payload, then this could point to garbage data in the buffer. payloadLength is correctly set to 0 for this case, but it also makes sense to set the payload to NULL as well.

Improve unit tests for MQTT_DeserializePublish:

  • The unit tests for MQTT_DeserializePublish don't reset its publishInfo struct between test cases. Before a test, its pointers point to constant strings, but are changed to point to the serialized buffer afterwards. This is an issue for the next test as it will try to serialize the data into the same buffer that is being pointed to, and mangles the data.
  • The deserialized publish information isn't checked, only the return code. The above test issue was caught when adding these checks.
  • Added unit test to check for a NULL pointer when payloads are zero length.

publishInfo.pTopicName = TEST_TOPIC_NAME;
publishInfo.topicNameLength = TEST_TOPIC_NAME_LENGTH;
publishInfo.pPayload = MQTT_SAMPLE_PAYLOAD;
publishInfo.payloadLength = MQTT_SAMPLE_PAYLOAD_LEN;
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.

Could you possibly use setupWillInfo for these instead, changing what is necessary for the specific test case? Maybe using the built in unity fixture, setUp, can make this easier for you.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I'll rename it to setupPublishInfo

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.

yourslab
yourslab previously approved these changes Sep 18, 2020
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

@muneebahmed10 muneebahmed10 merged commit 38f24af into FreeRTOS:master Sep 19, 2020
@muneebahmed10 muneebahmed10 deleted the null-payload branch September 19, 2020 00:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants