-
Notifications
You must be signed in to change notification settings - Fork 103
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
Changes from 4 commits
cd4d6a8
a9a3391
03deda8
0281aa8
92c254a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. */ | ||
|
@@ -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. */ | ||
|
@@ -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; | ||
} | ||
|
||
/** | ||
|
@@ -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; | ||
|
@@ -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 ) ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -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, | ||
|
@@ -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 ); | ||
} | ||
|
||
/* ========================================================================== */ | ||
|
@@ -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, | ||
|
There was a problem hiding this comment.
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 ifpPublishInfo->qos != MQTTQoS0
?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.