Add `mosquitto_property_remove`

This isfor removing properties from property lists.
pull/2756/head
Roger A. Light 3 years ago
parent 63b84c493b
commit d72e1c4949

@ -163,6 +163,8 @@ Client library:
version.
- Add `mosquitto_unsubscribe2_v5_callback_set`, which provides a callback that
gives access to reason codes for each of the unsubscription requests.
- Add `mosquitto_property_remove`, for removing properties from property
lists.
Clients:
- Add `-W` timeout support to Windows.

@ -3105,6 +3105,23 @@ libmosq_EXPORT int mosquitto_property_add_string(mosquitto_property **proplist,
libmosq_EXPORT int mosquitto_property_add_string_pair(mosquitto_property **proplist, int identifier, const char *name, const char *value);
/*
* Function: mosquitto_property_remove
*
* Remove a property from a property list. The property will not be freed.
*
* Parameters:
* proplist - pointer to mosquitto_property pointer, the list of properties
* property - pointer to the property to remove
*
* Returns:
* MOSQ_ERR_SUCCESS - on success
* MOSQ_ERR_INVAL - if proplist is NULL or property is NULL
* MOSQ_ERR_NOT_FOUND - if the property was not found
*/
libmosq_EXPORT int mosquitto_property_remove(mosquitto_property **proplist, const mosquitto_property *property);
/*
* Function: mosquitto_property_identifier
*

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

@ -1197,6 +1197,33 @@ BROKER_EXPORT const mosquitto_property *mosquitto_property_read_string_pair(cons
}
BROKER_EXPORT int mosquitto_property_remove(mosquitto_property **proplist, const mosquitto_property *property)
{
mosquitto_property *item, *item_prev = NULL;
if(proplist == NULL || property == NULL){
return MOSQ_ERR_INVAL;
}
item = *proplist;
while(item){
if(item == property){
if(item_prev == NULL){
*proplist = (*proplist)->next;
}else{
item_prev->next = item->next;
}
item->next = NULL;
return MOSQ_ERR_SUCCESS;
}
item_prev = item;
item = item->next;
}
return MOSQ_ERR_NOT_FOUND;
}
BROKER_EXPORT int mosquitto_property_copy_all(mosquitto_property **dest, const mosquitto_property *src)
{
mosquitto_property *pnew, *plast = NULL;

@ -5,6 +5,25 @@
#include "property_mosq.h"
#include "packet_mosq.h"
static void check_count(mosquitto_property *proplist, int expected)
{
mosquitto_property *p;
int count;
if(proplist == NULL){
CU_ASSERT_EQUAL(expected, 0);
return;
}
p = proplist;
count = 0;
while(p){
count++;
p = p->next;
}
CU_ASSERT_EQUAL(count, expected);
}
/* ========================================================================
* BAD IDENTIFIER
* ======================================================================== */
@ -459,8 +478,6 @@ static void TEST_add_single_string_pair(void)
static void TEST_add_all_connect(void)
{
mosquitto_property *proplist = NULL;
mosquitto_property *p;
int count;
int rc;
rc = mosquitto_property_add_int32(&proplist, MQTT_PROP_SESSION_EXPIRY_INTERVAL, 86400);
@ -491,13 +508,7 @@ static void TEST_add_all_connect(void)
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
CU_ASSERT_PTR_NOT_NULL(proplist);
p = proplist;
count = 0;
while(p){
count++;
p = p->next;
}
CU_ASSERT_EQUAL(count, 9);
check_count(proplist, 9);
mosquitto_property_free_all(&proplist);
}
@ -506,8 +517,6 @@ static void TEST_add_all_connect(void)
static void TEST_add_all_connack(void)
{
mosquitto_property *proplist = NULL;
mosquitto_property *p;
int count;
int rc;
rc = mosquitto_property_add_int32(&proplist, MQTT_PROP_SESSION_EXPIRY_INTERVAL, 86400);
@ -562,13 +571,7 @@ static void TEST_add_all_connack(void)
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
CU_ASSERT_PTR_NOT_NULL(proplist);
p = proplist;
count = 0;
while(p){
count++;
p = p->next;
}
CU_ASSERT_EQUAL(count, 17);
check_count(proplist, 17);
mosquitto_property_free_all(&proplist);
}
@ -606,6 +609,128 @@ static void TEST_check_length(void)
mosquitto_property_free_all(&proplist);
}
static void TEST_remove_single(void)
{
mosquitto_property *proplist = NULL, *property;
int rc;
unsigned int len;
len = property__get_remaining_length(proplist);
CU_ASSERT_EQUAL(len, 1);
for(int i=1; i<10; i++){
rc = mosquitto_property_add_byte(&proplist, MQTT_PROP_SHARED_SUB_AVAILABLE, 0);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
CU_ASSERT_PTR_NOT_NULL(proplist);
}
check_count(proplist, 9);
/* Remove end item */
property = proplist;
while(property && property->next){
property = property->next;
}
rc = mosquitto_property_remove(&proplist, property);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
mosquitto_property_free_all(&property);
check_count(proplist, 8);
/* Remove middle item */
property = proplist;
for(int i=0; i<4; i++){
property = property->next;
}
rc = mosquitto_property_remove(&proplist, property);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
mosquitto_property_free_all(&property);
check_count(proplist, 7);
/* Remove front item */
property = proplist;
rc = mosquitto_property_remove(&proplist, property);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
CU_ASSERT_PTR_NOT_EQUAL(proplist, property);
mosquitto_property_free_all(&property);
check_count(proplist, 6);
mosquitto_property_free_all(&proplist);
}
static void TEST_remove_all(void)
{
mosquitto_property *proplist = NULL, *property;
int rc;
for(int i=0; i<100; i++){
rc = mosquitto_property_add_byte(&proplist, MQTT_PROP_SHARED_SUB_AVAILABLE, 0);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
CU_ASSERT_PTR_NOT_NULL(proplist);
}
for(int i=0; i<100; i++){
property = proplist;
rc = mosquitto_property_remove(&proplist, property);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
mosquitto_property_free_all(&property);
check_count(proplist, 100-i-1);
}
CU_ASSERT_PTR_NULL(proplist);
mosquitto_property_free_all(&proplist);
}
static void TEST_remove_non_existent(void)
{
mosquitto_property *proplist = NULL, *property = NULL;
int rc;
unsigned int len;
len = property__get_remaining_length(proplist);
CU_ASSERT_EQUAL(len, 1);
rc = mosquitto_property_add_byte(&proplist, MQTT_PROP_SHARED_SUB_AVAILABLE, 0);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
CU_ASSERT_PTR_NOT_NULL(proplist);
if(proplist){
rc = mosquitto_property_add_byte(&property, MQTT_PROP_SHARED_SUB_AVAILABLE, 0);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
CU_ASSERT_PTR_NOT_NULL(property);
if(property){
rc = mosquitto_property_remove(&proplist, property);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_NOT_FOUND);
}
}
mosquitto_property_free_all(&proplist);
mosquitto_property_free_all(&property);
}
static void TEST_remove_invalid(void)
{
mosquitto_property *proplist = NULL;
int rc;
unsigned int len;
rc = mosquitto_property_remove(NULL, NULL);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_INVAL);
len = property__get_remaining_length(proplist);
CU_ASSERT_EQUAL(len, 1);
rc = mosquitto_property_add_byte(&proplist, MQTT_PROP_SHARED_SUB_AVAILABLE, 0);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
CU_ASSERT_PTR_NOT_NULL(proplist);
if(proplist){
rc = mosquitto_property_remove(&proplist, NULL);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_INVAL);
rc = mosquitto_property_remove(NULL, proplist);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_INVAL);
}
mosquitto_property_free_all(&proplist);
}
/* ========================================================================
* TEST SUITE SETUP
* ======================================================================== */
@ -638,6 +763,10 @@ int init_property_add_tests(void)
|| !CU_add_test(test_suite, "Add single string pair", TEST_add_single_string_pair)
|| !CU_add_test(test_suite, "Add all CONNECT", TEST_add_all_connect)
|| !CU_add_test(test_suite, "Add all CONNACK", TEST_add_all_connack)
|| !CU_add_test(test_suite, "Remove single", TEST_remove_single)
|| !CU_add_test(test_suite, "Remove all", TEST_remove_all)
|| !CU_add_test(test_suite, "Remove non-existent", TEST_remove_non_existent)
|| !CU_add_test(test_suite, "Remove invalid", TEST_remove_invalid)
){
printf("Error adding Property Add CUnit tests.\n");

@ -44,7 +44,7 @@ int main(int argc, char *argv[])
return 1;
}
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_set_mode(CU_BRM_NORMAL);
CU_basic_run_tests();
fails = CU_get_number_of_failures();
CU_cleanup_registry();

Loading…
Cancel
Save