Add mosquitto_property_type().

pull/2826/head
Roger A. Light 2 years ago
parent e306691b7b
commit e44042c32b

@ -3372,6 +3372,21 @@ libmosq_EXPORT const mosquitto_property *mosquitto_property_read_string_pair(
char **value,
bool skip_first);
/*
* Function: mosquitto_property_type
*
* Return the property type for a single property.
*
* Parameters:
* property - pointer to a valid mosquitto_property pointer.
*
* Returns:
* A valid property type on success
* 0 - on error
*/
libmosq_EXPORT int mosquitto_property_type(const mosquitto_property *property);
/*
* Function: mosquitto_property_free_all
*

@ -150,4 +150,5 @@ MOSQ_2.1 {
mosquitto_sub_matches_acl_with_pattern;
mosquitto_unsubscribe2_v5_callback_set;
mosquitto_property_remove;
mosquitto_property_type;
} MOSQ_1.7;

@ -69,6 +69,7 @@ static int property__read(struct mosquitto__packet_in *packet, uint32_t *len, mo
if(rc) return rc;
*len -= 1; /* byte */
property->value.i8 = byte;
property->property_type = MQTT_PROP_TYPE_BYTE;
break;
case MQTT_PROP_SERVER_KEEP_ALIVE:
@ -79,6 +80,7 @@ static int property__read(struct mosquitto__packet_in *packet, uint32_t *len, mo
if(rc) return rc;
*len -= 2; /* uint16 */
property->value.i16 = uint16;
property->property_type = MQTT_PROP_TYPE_INT16;
break;
case MQTT_PROP_MESSAGE_EXPIRY_INTERVAL:
@ -89,6 +91,7 @@ static int property__read(struct mosquitto__packet_in *packet, uint32_t *len, mo
if(rc) return rc;
*len -= 4; /* uint32 */
property->value.i32 = uint32;
property->property_type = MQTT_PROP_TYPE_INT32;
break;
case MQTT_PROP_SUBSCRIPTION_IDENTIFIER:
@ -96,6 +99,7 @@ static int property__read(struct mosquitto__packet_in *packet, uint32_t *len, mo
if(rc) return rc;
*len -= byte_count;
property->value.varint = varint;
property->property_type = MQTT_PROP_TYPE_VARINT;
break;
case MQTT_PROP_CONTENT_TYPE:
@ -110,6 +114,7 @@ static int property__read(struct mosquitto__packet_in *packet, uint32_t *len, mo
*len = (*len) - 2 - slen1; /* uint16, string len */
property->value.s.v = str1;
property->value.s.len = slen1;
property->property_type = MQTT_PROP_TYPE_STRING;
break;
case MQTT_PROP_AUTHENTICATION_DATA:
@ -119,6 +124,7 @@ static int property__read(struct mosquitto__packet_in *packet, uint32_t *len, mo
*len = (*len) - 2 - slen1; /* uint16, binary len */
property->value.bin.v = str1;
property->value.bin.len = slen1;
property->property_type = MQTT_PROP_TYPE_BINARY;
break;
case MQTT_PROP_USER_PROPERTY:
@ -137,6 +143,7 @@ static int property__read(struct mosquitto__packet_in *packet, uint32_t *len, mo
property->name.len = slen1;
property->value.s.v = str2;
property->value.s.len = slen2;
property->property_type = MQTT_PROP_TYPE_STRING_PAIR;
break;
default:
@ -196,44 +203,24 @@ void property__free(mosquitto_property **property)
{
if(!property || !(*property)) return;
switch((*property)->identifier){
case MQTT_PROP_CONTENT_TYPE:
case MQTT_PROP_RESPONSE_TOPIC:
case MQTT_PROP_ASSIGNED_CLIENT_IDENTIFIER:
case MQTT_PROP_AUTHENTICATION_METHOD:
case MQTT_PROP_RESPONSE_INFORMATION:
case MQTT_PROP_SERVER_REFERENCE:
case MQTT_PROP_REASON_STRING:
switch((*property)->property_type){
case MQTT_PROP_TYPE_STRING:
mosquitto__FREE((*property)->value.s.v);
break;
case MQTT_PROP_AUTHENTICATION_DATA:
case MQTT_PROP_CORRELATION_DATA:
case MQTT_PROP_TYPE_BINARY:
mosquitto__FREE((*property)->value.bin.v);
break;
case MQTT_PROP_USER_PROPERTY:
case MQTT_PROP_TYPE_STRING_PAIR:
mosquitto__FREE((*property)->name.v);
mosquitto__FREE((*property)->value.s.v);
break;
case MQTT_PROP_PAYLOAD_FORMAT_INDICATOR:
case MQTT_PROP_MESSAGE_EXPIRY_INTERVAL:
case MQTT_PROP_SUBSCRIPTION_IDENTIFIER:
case MQTT_PROP_SESSION_EXPIRY_INTERVAL:
case MQTT_PROP_SERVER_KEEP_ALIVE:
case MQTT_PROP_REQUEST_PROBLEM_INFORMATION:
case MQTT_PROP_WILL_DELAY_INTERVAL:
case MQTT_PROP_REQUEST_RESPONSE_INFORMATION:
case MQTT_PROP_RECEIVE_MAXIMUM:
case MQTT_PROP_TOPIC_ALIAS_MAXIMUM:
case MQTT_PROP_TOPIC_ALIAS:
case MQTT_PROP_MAXIMUM_QOS:
case MQTT_PROP_RETAIN_AVAILABLE:
case MQTT_PROP_MAXIMUM_PACKET_SIZE:
case MQTT_PROP_WILDCARD_SUB_AVAILABLE:
case MQTT_PROP_SUBSCRIPTION_ID_AVAILABLE:
case MQTT_PROP_SHARED_SUB_AVAILABLE:
case MQTT_PROP_TYPE_BYTE:
case MQTT_PROP_TYPE_INT16:
case MQTT_PROP_TYPE_INT32:
case MQTT_PROP_TYPE_VARINT:
/* Nothing to free */
break;
}
@ -262,34 +249,17 @@ unsigned int property__get_length(const mosquitto_property *property)
{
if(!property) return 0;
switch(property->identifier){
/* Byte */
case MQTT_PROP_PAYLOAD_FORMAT_INDICATOR:
case MQTT_PROP_REQUEST_PROBLEM_INFORMATION:
case MQTT_PROP_REQUEST_RESPONSE_INFORMATION:
case MQTT_PROP_MAXIMUM_QOS:
case MQTT_PROP_RETAIN_AVAILABLE:
case MQTT_PROP_WILDCARD_SUB_AVAILABLE:
case MQTT_PROP_SUBSCRIPTION_ID_AVAILABLE:
case MQTT_PROP_SHARED_SUB_AVAILABLE:
switch(property->property_type){
case MQTT_PROP_TYPE_BYTE:
return 2; /* 1 (identifier) + 1 byte */
/* uint16 */
case MQTT_PROP_SERVER_KEEP_ALIVE:
case MQTT_PROP_RECEIVE_MAXIMUM:
case MQTT_PROP_TOPIC_ALIAS_MAXIMUM:
case MQTT_PROP_TOPIC_ALIAS:
case MQTT_PROP_TYPE_INT16:
return 3; /* 1 (identifier) + 2 bytes */
/* uint32 */
case MQTT_PROP_MESSAGE_EXPIRY_INTERVAL:
case MQTT_PROP_WILL_DELAY_INTERVAL:
case MQTT_PROP_MAXIMUM_PACKET_SIZE:
case MQTT_PROP_SESSION_EXPIRY_INTERVAL:
case MQTT_PROP_TYPE_INT32:
return 5; /* 1 (identifier) + 4 bytes */
/* varint */
case MQTT_PROP_SUBSCRIPTION_IDENTIFIER:
case MQTT_PROP_TYPE_VARINT:
if(property->value.varint < 128){
return 2;
}else if(property->value.varint < 16384){
@ -302,23 +272,13 @@ unsigned int property__get_length(const mosquitto_property *property)
return 0;
}
/* binary */
case MQTT_PROP_CORRELATION_DATA:
case MQTT_PROP_AUTHENTICATION_DATA:
case MQTT_PROP_TYPE_BINARY:
return 3U + property->value.bin.len; /* 1 + 2 bytes (len) + X bytes (payload) */
/* string */
case MQTT_PROP_CONTENT_TYPE:
case MQTT_PROP_RESPONSE_TOPIC:
case MQTT_PROP_ASSIGNED_CLIENT_IDENTIFIER:
case MQTT_PROP_AUTHENTICATION_METHOD:
case MQTT_PROP_RESPONSE_INFORMATION:
case MQTT_PROP_SERVER_REFERENCE:
case MQTT_PROP_REASON_STRING:
case MQTT_PROP_TYPE_STRING:
return 3U + property->value.s.len; /* 1 + 2 bytes (len) + X bytes (string) */
/* string pair */
case MQTT_PROP_USER_PROPERTY:
case MQTT_PROP_TYPE_STRING_PAIR:
return 5U + property->value.s.len + property->name.len; /* 1 + 2*(2 bytes (len) + X bytes (string))*/
default:
@ -361,52 +321,32 @@ static int property__write(struct mosquitto__packet *packet, const mosquitto_pro
rc = packet__write_varint(packet, (uint32_t)property->identifier);
if(rc) return rc;
switch(property->identifier){
case MQTT_PROP_PAYLOAD_FORMAT_INDICATOR:
case MQTT_PROP_REQUEST_PROBLEM_INFORMATION:
case MQTT_PROP_REQUEST_RESPONSE_INFORMATION:
case MQTT_PROP_MAXIMUM_QOS:
case MQTT_PROP_RETAIN_AVAILABLE:
case MQTT_PROP_WILDCARD_SUB_AVAILABLE:
case MQTT_PROP_SUBSCRIPTION_ID_AVAILABLE:
case MQTT_PROP_SHARED_SUB_AVAILABLE:
switch(property->property_type){
case MQTT_PROP_TYPE_BYTE:
packet__write_byte(packet, property->value.i8);
break;
case MQTT_PROP_SERVER_KEEP_ALIVE:
case MQTT_PROP_RECEIVE_MAXIMUM:
case MQTT_PROP_TOPIC_ALIAS_MAXIMUM:
case MQTT_PROP_TOPIC_ALIAS:
case MQTT_PROP_TYPE_INT16:
packet__write_uint16(packet, property->value.i16);
break;
case MQTT_PROP_MESSAGE_EXPIRY_INTERVAL:
case MQTT_PROP_SESSION_EXPIRY_INTERVAL:
case MQTT_PROP_WILL_DELAY_INTERVAL:
case MQTT_PROP_MAXIMUM_PACKET_SIZE:
case MQTT_PROP_TYPE_INT32:
packet__write_uint32(packet, property->value.i32);
break;
case MQTT_PROP_SUBSCRIPTION_IDENTIFIER:
case MQTT_PROP_TYPE_VARINT:
return packet__write_varint(packet, property->value.varint);
case MQTT_PROP_CONTENT_TYPE:
case MQTT_PROP_RESPONSE_TOPIC:
case MQTT_PROP_ASSIGNED_CLIENT_IDENTIFIER:
case MQTT_PROP_AUTHENTICATION_METHOD:
case MQTT_PROP_RESPONSE_INFORMATION:
case MQTT_PROP_SERVER_REFERENCE:
case MQTT_PROP_REASON_STRING:
case MQTT_PROP_TYPE_STRING:
packet__write_string(packet, property->value.s.v, property->value.s.len);
break;
case MQTT_PROP_AUTHENTICATION_DATA:
case MQTT_PROP_CORRELATION_DATA:
case MQTT_PROP_TYPE_BINARY:
packet__write_uint16(packet, property->value.bin.len);
packet__write_bytes(packet, property->value.bin.v, property->value.bin.len);
break;
case MQTT_PROP_USER_PROPERTY:
case MQTT_PROP_TYPE_STRING_PAIR:
packet__write_string(packet, property->name.v, property->name.len);
packet__write_string(packet, property->value.s.v, property->value.s.len);
break;
@ -728,6 +668,7 @@ BROKER_EXPORT int mosquitto_property_add_byte(mosquitto_property **proplist, int
prop->client_generated = true;
prop->identifier = identifier;
prop->value.i8 = value;
prop->property_type = MQTT_PROP_TYPE_BYTE;
property__add(proplist, prop);
return MOSQ_ERR_SUCCESS;
@ -752,6 +693,7 @@ BROKER_EXPORT int mosquitto_property_add_int16(mosquitto_property **proplist, in
prop->client_generated = true;
prop->identifier = identifier;
prop->value.i16 = value;
prop->property_type = MQTT_PROP_TYPE_INT16;
property__add(proplist, prop);
return MOSQ_ERR_SUCCESS;
@ -777,6 +719,7 @@ BROKER_EXPORT int mosquitto_property_add_int32(mosquitto_property **proplist, in
prop->client_generated = true;
prop->identifier = identifier;
prop->value.i32 = value;
prop->property_type = MQTT_PROP_TYPE_INT32;
property__add(proplist, prop);
return MOSQ_ERR_SUCCESS;
@ -796,6 +739,7 @@ BROKER_EXPORT int mosquitto_property_add_varint(mosquitto_property **proplist, i
prop->client_generated = true;
prop->identifier = identifier;
prop->value.varint = value;
prop->property_type = MQTT_PROP_TYPE_VARINT;
property__add(proplist, prop);
return MOSQ_ERR_SUCCESS;
@ -818,6 +762,7 @@ BROKER_EXPORT int mosquitto_property_add_binary(mosquitto_property **proplist, i
prop->client_generated = true;
prop->identifier = identifier;
prop->property_type = MQTT_PROP_TYPE_BINARY;
if(len){
prop->value.bin.v = mosquitto__malloc(len);
@ -862,6 +807,7 @@ BROKER_EXPORT int mosquitto_property_add_string(mosquitto_property **proplist, i
prop->client_generated = true;
prop->identifier = identifier;
prop->property_type = MQTT_PROP_TYPE_STRING;
if(value && slen > 0){
prop->value.s.v = mosquitto__strdup(value);
if(!prop->value.s.v){
@ -896,6 +842,7 @@ BROKER_EXPORT int mosquitto_property_add_string_pair(mosquitto_property **propli
prop->client_generated = true;
prop->identifier = identifier;
prop->property_type = MQTT_PROP_TYPE_STRING_PAIR;
if(name){
prop->name.v = mosquitto__strdup(name);
@ -1002,6 +949,14 @@ BROKER_EXPORT int mosquitto_property_identifier(const mosquitto_property *proper
}
BROKER_EXPORT int mosquitto_property_type(const mosquitto_property *property)
{
if(property == NULL) return 0;
return property->property_type;
}
BROKER_EXPORT const mosquitto_property *mosquitto_property_next(const mosquitto_property *proplist)
{
if(proplist == NULL) return NULL;
@ -1248,43 +1203,25 @@ BROKER_EXPORT int mosquitto_property_copy_all(mosquitto_property **dest, const m
pnew->client_generated = src->client_generated;
pnew->identifier = src->identifier;
switch(pnew->identifier){
case MQTT_PROP_PAYLOAD_FORMAT_INDICATOR:
case MQTT_PROP_REQUEST_PROBLEM_INFORMATION:
case MQTT_PROP_REQUEST_RESPONSE_INFORMATION:
case MQTT_PROP_MAXIMUM_QOS:
case MQTT_PROP_RETAIN_AVAILABLE:
case MQTT_PROP_WILDCARD_SUB_AVAILABLE:
case MQTT_PROP_SUBSCRIPTION_ID_AVAILABLE:
case MQTT_PROP_SHARED_SUB_AVAILABLE:
pnew->property_type = src->property_type;
switch(pnew->property_type){
case MQTT_PROP_TYPE_BYTE:
pnew->value.i8 = src->value.i8;
break;
case MQTT_PROP_SERVER_KEEP_ALIVE:
case MQTT_PROP_RECEIVE_MAXIMUM:
case MQTT_PROP_TOPIC_ALIAS_MAXIMUM:
case MQTT_PROP_TOPIC_ALIAS:
case MQTT_PROP_TYPE_INT16:
pnew->value.i16 = src->value.i16;
break;
case MQTT_PROP_MESSAGE_EXPIRY_INTERVAL:
case MQTT_PROP_SESSION_EXPIRY_INTERVAL:
case MQTT_PROP_WILL_DELAY_INTERVAL:
case MQTT_PROP_MAXIMUM_PACKET_SIZE:
case MQTT_PROP_TYPE_INT32:
pnew->value.i32 = src->value.i32;
break;
case MQTT_PROP_SUBSCRIPTION_IDENTIFIER:
case MQTT_PROP_TYPE_VARINT:
pnew->value.varint = src->value.varint;
break;
case MQTT_PROP_CONTENT_TYPE:
case MQTT_PROP_RESPONSE_TOPIC:
case MQTT_PROP_ASSIGNED_CLIENT_IDENTIFIER:
case MQTT_PROP_AUTHENTICATION_METHOD:
case MQTT_PROP_RESPONSE_INFORMATION:
case MQTT_PROP_SERVER_REFERENCE:
case MQTT_PROP_REASON_STRING:
case MQTT_PROP_TYPE_STRING:
pnew->value.s.len = src->value.s.len;
if(src->value.s.v){
pnew->value.s.v = strdup(src->value.s.v);
@ -1295,8 +1232,7 @@ BROKER_EXPORT int mosquitto_property_copy_all(mosquitto_property **dest, const m
}
break;
case MQTT_PROP_AUTHENTICATION_DATA:
case MQTT_PROP_CORRELATION_DATA:
case MQTT_PROP_TYPE_BINARY:
pnew->value.bin.len = src->value.bin.len;
if(src->value.bin.len){
pnew->value.bin.v = malloc(pnew->value.bin.len);
@ -1308,7 +1244,7 @@ BROKER_EXPORT int mosquitto_property_copy_all(mosquitto_property **dest, const m
}
break;
case MQTT_PROP_USER_PROPERTY:
case MQTT_PROP_TYPE_STRING_PAIR:
pnew->value.s.len = src->value.s.len;
if(src->value.s.v){
pnew->value.s.v = strdup(src->value.s.v);

@ -38,6 +38,7 @@ struct mqtt5__property {
} value;
struct mqtt__string name;
int32_t identifier;
uint8_t property_type;
bool client_generated;
};

@ -229,6 +229,7 @@ int send__real_publish(struct mosquitto *mosq, uint16_t mid, const char *topic,
expiry_prop.next = NULL;
expiry_prop.value.i32 = expiry_interval;
expiry_prop.identifier = MQTT_PROP_MESSAGE_EXPIRY_INTERVAL;
expiry_prop.property_type = MQTT_PROP_TYPE_INT32;
expiry_prop.client_generated = false;
proplen += property__get_length_all(&expiry_prop);
@ -238,6 +239,7 @@ int send__real_publish(struct mosquitto *mosq, uint16_t mid, const char *topic,
topic_alias_prop.next = NULL;
topic_alias_prop.value.i16 = topic_alias;
topic_alias_prop.identifier = MQTT_PROP_TOPIC_ALIAS;
topic_alias_prop.property_type = MQTT_PROP_TYPE_INT16;
topic_alias_prop.client_generated = false;
proplen += property__get_length_all(&topic_alias_prop);
@ -246,6 +248,7 @@ int send__real_publish(struct mosquitto *mosq, uint16_t mid, const char *topic,
subscription_id_prop.next = NULL;
subscription_id_prop.value.varint = subscription_identifier;
subscription_id_prop.identifier = MQTT_PROP_SUBSCRIPTION_IDENTIFIER;
subscription_id_prop.property_type = MQTT_PROP_TYPE_VARINT;
subscription_id_prop.client_generated = false;
proplen += property__get_length_all(&subscription_id_prop);

@ -405,6 +405,7 @@ int bridge__connect_step3(struct mosquitto *context)
if(context->bridge->receive_maximum != 0){
receive_maximum.value.i16 = context->bridge->receive_maximum;
receive_maximum.identifier = MQTT_PROP_RECEIVE_MAXIMUM;
receive_maximum.property_type = MQTT_PROP_TYPE_INT16;
receive_maximum.client_generated = false;
receive_maximum.next = properties;
properties = &receive_maximum;
@ -412,6 +413,7 @@ int bridge__connect_step3(struct mosquitto *context)
if(context->bridge->session_expiry_interval != 0){
session_expiry_interval.value.i32 = context->bridge->session_expiry_interval;
session_expiry_interval.identifier = MQTT_PROP_SESSION_EXPIRY_INTERVAL;
session_expiry_interval.property_type = MQTT_PROP_TYPE_INT32;
session_expiry_interval.client_generated = false;
session_expiry_interval.next = properties;
properties = &session_expiry_interval;
@ -419,6 +421,7 @@ int bridge__connect_step3(struct mosquitto *context)
if(context->bridge->max_topic_alias != 0){
topic_alias_max.value.i16 = context->bridge->max_topic_alias;
topic_alias_max.identifier = MQTT_PROP_TOPIC_ALIAS_MAXIMUM;
topic_alias_max.property_type = MQTT_PROP_TYPE_INT16;
topic_alias_max.client_generated = false;
topic_alias_max.next = properties;
properties = &topic_alias_max;
@ -575,6 +578,7 @@ int bridge__connect(struct mosquitto *context)
if(context->bridge->receive_maximum != 0){
receive_maximum.value.i16 = context->bridge->receive_maximum;
receive_maximum.identifier = MQTT_PROP_RECEIVE_MAXIMUM;
receive_maximum.property_type = MQTT_PROP_TYPE_INT16;
receive_maximum.client_generated = false;
receive_maximum.next = properties;
properties = &receive_maximum;
@ -582,6 +586,7 @@ int bridge__connect(struct mosquitto *context)
if(context->bridge->session_expiry_interval != 0){
session_expiry_interval.value.i32 = context->bridge->session_expiry_interval;
session_expiry_interval.identifier = MQTT_PROP_SESSION_EXPIRY_INTERVAL;
session_expiry_interval.property_type = MQTT_PROP_TYPE_INT32;
session_expiry_interval.client_generated = false;
session_expiry_interval.next = properties;
properties = &session_expiry_interval;
@ -589,6 +594,7 @@ int bridge__connect(struct mosquitto *context)
if(context->bridge->max_topic_alias != 0){
topic_alias_max.value.i16 = context->bridge->max_topic_alias;
topic_alias_max.identifier = MQTT_PROP_TOPIC_ALIAS_MAXIMUM;
topic_alias_max.property_type = MQTT_PROP_TYPE_INT16;
topic_alias_max.client_generated = false;
topic_alias_max.next = properties;
properties = &topic_alias_max;

@ -94,13 +94,9 @@ int persist__chunk_client_msg_write_v6(FILE *db_fptr, struct P_client_msg *chunk
uint32_t proplen = 0;
int rc;
mosquitto_property subscription_id_prop =
{.next = NULL, .identifier = MQTT_PROP_SUBSCRIPTION_IDENTIFIER, .client_generated = true};
{.next = NULL, .identifier = MQTT_PROP_SUBSCRIPTION_IDENTIFIER, .client_generated = true, .property_type = MQTT_PROP_TYPE_VARINT};
if(chunk->subscription_identifier){
subscription_id_prop.next = NULL;
subscription_id_prop.identifier = MQTT_PROP_SUBSCRIPTION_IDENTIFIER;
subscription_id_prop.client_generated = true;
subscription_id_prop.value.varint = chunk->subscription_identifier;
proplen += property__get_remaining_length(&subscription_id_prop);
}

@ -22,6 +22,7 @@ static void byte_prop_write_helper(
property.identifier = identifier;
property.value.i8 = value_expected;
property.property_type = MQTT_PROP_TYPE_BYTE;
rc = packet__alloc(&packet, 0, property__get_length_all(&property)+11);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
@ -41,8 +42,12 @@ static void byte_prop_write_helper(
CU_ASSERT_EQUAL(in_packet.pos, remaining_length);
if(properties){
CU_ASSERT_EQUAL(properties->identifier, identifier);
CU_ASSERT_EQUAL(mosquitto_property_identifier(properties), identifier);
CU_ASSERT_EQUAL(properties->value.i8, value_expected);
CU_ASSERT_EQUAL(properties->property_type, MQTT_PROP_TYPE_BYTE);
CU_ASSERT_EQUAL(mosquitto_property_type(properties), MQTT_PROP_TYPE_BYTE);
CU_ASSERT_PTR_EQUAL(properties->next, NULL);
CU_ASSERT_PTR_EQUAL(mosquitto_property_next(properties), NULL);
CU_ASSERT_EQUAL(property__get_length_all(properties), 2);
mosquitto_property_free_all(&properties);
}
@ -68,6 +73,7 @@ static void int32_prop_write_helper(
property.identifier = identifier;
property.value.i32 = value_expected;
property.property_type = MQTT_PROP_TYPE_INT32;
rc = packet__alloc(&packet, 0, property__get_length_all(&property)+11);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
@ -87,8 +93,12 @@ static void int32_prop_write_helper(
CU_ASSERT_EQUAL(in_packet.pos, remaining_length);
if(properties){
CU_ASSERT_EQUAL(properties->identifier, identifier);
CU_ASSERT_EQUAL(mosquitto_property_identifier(properties), identifier);
CU_ASSERT_EQUAL(properties->value.i32, value_expected);
CU_ASSERT_EQUAL(properties->property_type, MQTT_PROP_TYPE_INT32);
CU_ASSERT_EQUAL(mosquitto_property_type(properties), MQTT_PROP_TYPE_INT32);
CU_ASSERT_PTR_EQUAL(properties->next, NULL);
CU_ASSERT_PTR_EQUAL(mosquitto_property_next(properties), NULL);
CU_ASSERT_EQUAL(property__get_length_all(properties), 5);
mosquitto_property_free_all(&properties);
}
@ -114,6 +124,7 @@ static void int16_prop_write_helper(
property.identifier = identifier;
property.value.i16 = value_expected;
property.property_type = MQTT_PROP_TYPE_INT16;
rc = packet__alloc(&packet, 0, property__get_length_all(&property)+11);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
@ -133,8 +144,12 @@ static void int16_prop_write_helper(
CU_ASSERT_EQUAL(in_packet.pos, remaining_length);
if(properties){
CU_ASSERT_EQUAL(properties->identifier, identifier);
CU_ASSERT_EQUAL(mosquitto_property_identifier(properties), identifier);
CU_ASSERT_EQUAL(properties->value.i16, value_expected);
CU_ASSERT_EQUAL(properties->property_type, MQTT_PROP_TYPE_INT16);
CU_ASSERT_EQUAL(mosquitto_property_type(properties), MQTT_PROP_TYPE_INT16);
CU_ASSERT_PTR_EQUAL(properties->next, NULL);
CU_ASSERT_PTR_EQUAL(mosquitto_property_next(properties), NULL);
CU_ASSERT_EQUAL(property__get_length_all(properties), 3);
mosquitto_property_free_all(&properties);
}
@ -158,6 +173,7 @@ static void string_prop_write_helper(
memset(&property, 0, sizeof(mosquitto_property));
property.identifier = identifier;
property.property_type = MQTT_PROP_TYPE_STRING;
property.value.s.v = strdup(value_expected);
CU_ASSERT_PTR_NOT_NULL(property.value.s.v);
if(!property.value.s.v) return;
@ -182,9 +198,13 @@ static void string_prop_write_helper(
CU_ASSERT_EQUAL(in_packet.pos, remaining_length);
if(properties){
CU_ASSERT_EQUAL(properties->identifier, identifier);
CU_ASSERT_EQUAL(mosquitto_property_identifier(properties), identifier);
CU_ASSERT_EQUAL(properties->value.s.len, strlen(value_expected));
CU_ASSERT_STRING_EQUAL(properties->value.s.v, value_expected);
CU_ASSERT_EQUAL(properties->property_type, MQTT_PROP_TYPE_STRING);
CU_ASSERT_EQUAL(mosquitto_property_type(properties), MQTT_PROP_TYPE_STRING);
CU_ASSERT_PTR_EQUAL(properties->next, NULL);
CU_ASSERT_PTR_EQUAL(mosquitto_property_next(properties), NULL);
CU_ASSERT_EQUAL(property__get_length_all(properties), 1+2+strlen(value_expected));
mosquitto_property_free_all(&properties);
}
@ -211,6 +231,7 @@ static void binary_prop_write_helper(
memset(&property, 0, sizeof(mosquitto_property));
property.identifier = identifier;
property.property_type = MQTT_PROP_TYPE_BINARY;
property.value.bin.v = malloc(len_expected);
CU_ASSERT_PTR_NOT_NULL(property.value.bin.v);
if(!property.value.bin.v) return;
@ -236,9 +257,13 @@ static void binary_prop_write_helper(
CU_ASSERT_EQUAL(in_packet.pos, remaining_length);
if(properties){
CU_ASSERT_EQUAL(properties->identifier, identifier);
CU_ASSERT_EQUAL(mosquitto_property_identifier(properties), identifier);
CU_ASSERT_EQUAL(properties->value.bin.len, len_expected);
CU_ASSERT_EQUAL(memcmp(properties->value.bin.v, value_expected, len_expected), 0);
CU_ASSERT_EQUAL(properties->property_type, MQTT_PROP_TYPE_BINARY);
CU_ASSERT_EQUAL(mosquitto_property_type(properties), MQTT_PROP_TYPE_BINARY);
CU_ASSERT_PTR_EQUAL(properties->next, NULL);
CU_ASSERT_PTR_EQUAL(mosquitto_property_next(properties), NULL);
CU_ASSERT_EQUAL(property__get_length_all(properties), 1+2+len_expected);
mosquitto_property_free_all(&properties);
}
@ -264,6 +289,7 @@ static void string_pair_prop_write_helper(
memset(&property, 0, sizeof(mosquitto_property));
property.identifier = identifier;
property.property_type = MQTT_PROP_TYPE_STRING_PAIR;
property.value.s.v = strdup(value_expected);
CU_ASSERT_PTR_NOT_NULL(property.value.s.v);
if(!property.value.s.v) return;
@ -293,14 +319,19 @@ static void string_pair_prop_write_helper(
CU_ASSERT_EQUAL(in_packet.pos, remaining_length);
if(properties){
CU_ASSERT_EQUAL(properties->identifier, identifier);
CU_ASSERT_EQUAL(mosquitto_property_identifier(properties), identifier);
CU_ASSERT_EQUAL(properties->name.len, strlen(name_expected));
CU_ASSERT_EQUAL(properties->value.s.len, strlen(value_expected));
CU_ASSERT_STRING_EQUAL(properties->name.v, name_expected);
CU_ASSERT_STRING_EQUAL(properties->value.s.v, value_expected);
CU_ASSERT_EQUAL(properties->property_type, MQTT_PROP_TYPE_STRING_PAIR);
CU_ASSERT_EQUAL(mosquitto_property_type(properties), MQTT_PROP_TYPE_STRING_PAIR);
if(expect_multiple){
CU_ASSERT_PTR_NOT_NULL(properties->next);
CU_ASSERT_PTR_NOT_NULL(mosquitto_property_next(properties));
}else{
CU_ASSERT_PTR_NULL(properties->next);
CU_ASSERT_PTR_NULL(mosquitto_property_next(properties));
CU_ASSERT_EQUAL(property__get_length_all(properties), 1+2+strlen(name_expected)+2+strlen(value_expected));
}
mosquitto_property_free_all(&properties);
@ -326,6 +357,7 @@ static void varint_prop_write_helper(
memset(&property, 0, sizeof(mosquitto_property));
property.identifier = identifier;
property.property_type = MQTT_PROP_TYPE_VARINT;
property.value.varint = value_expected;
CU_ASSERT_EQUAL(remaining_length, property__get_length_all(&property)+1);
@ -347,8 +379,12 @@ static void varint_prop_write_helper(
CU_ASSERT_EQUAL(rc, rc_expected);
if(properties){
CU_ASSERT_EQUAL(properties->identifier, identifier);
CU_ASSERT_EQUAL(mosquitto_property_identifier(properties), identifier);
CU_ASSERT_EQUAL(properties->property_type, MQTT_PROP_TYPE_VARINT);
CU_ASSERT_EQUAL(mosquitto_property_type(properties), MQTT_PROP_TYPE_VARINT);
CU_ASSERT_EQUAL(properties->value.varint, value_expected);
CU_ASSERT_PTR_NULL(properties->next);
CU_ASSERT_PTR_NULL(mosquitto_property_next(properties));
if(value_expected < 128){
CU_ASSERT_EQUAL(property__get_length_all(properties), 2);
}else if(value_expected < 16384){
@ -382,10 +418,11 @@ static void TEST_bad_identifier(void)
if(packet == NULL) return;
property.identifier = 0xFFFF;
property.property_type = MQTT_PROP_TYPE_BYTE;
packet->packet_length = 10;
packet->remaining_length = 8;
rc = property__write_all(packet, &property, true);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_INVAL);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); // We don't check invalid identifier types here
free(packet);
}

Loading…
Cancel
Save