diff --git a/include/mosquitto.h b/include/mosquitto.h index 86d2484b..780f0009 100644 --- a/include/mosquitto.h +++ b/include/mosquitto.h @@ -3387,6 +3387,156 @@ libmosq_EXPORT const mosquitto_property *mosquitto_property_read_string_pair( libmosq_EXPORT int mosquitto_property_type(const mosquitto_property *property); +/* + * Function: mosquitto_property_byte_value + * + * Return the property value for a byte type property. + * + * Parameters: + * property - pointer to a valid mosquitto_property pointer. + * + * Returns: + * Byte value on success + * 0 - on error (property is NULL, or not a byte) + */ +libmosq_EXPORT uint8_t mosquitto_property_byte_value(const mosquitto_property *property); + + +/* + * Function: mosquitto_property_int16_value + * + * Return the property value for an int16 type property. + * + * Parameters: + * property - pointer to a valid mosquitto_property pointer. + * + * Returns: + * Int16 value on success + * 0 - on error (property is NULL, or not a int16) + */ +libmosq_EXPORT uint16_t mosquitto_property_int16_value(const mosquitto_property *property); + + +/* + * Function: mosquitto_property_int32_value + * + * Return the property value for an int32 type property. + * + * Parameters: + * property - pointer to a valid mosquitto_property pointer. + * + * Returns: + * Int32 value on success + * 0 - on error (property is NULL, or not a int32) + */ +libmosq_EXPORT uint32_t mosquitto_property_int32_value(const mosquitto_property *property); + + +/* + * Function: mosquitto_property_varint_value + * + * Return the property value for a varint type property. + * + * Parameters: + * property - pointer to a valid mosquitto_property pointer. + * + * Returns: + * Varint value on success + * 0 - on error (property is NULL, or not a varint) + */ +libmosq_EXPORT uint32_t mosquitto_property_varint_value(const mosquitto_property *property); + + +/* + * Function: mosquitto_property_binary_value + * + * Return the property value for a binary type property. + * + * Parameters: + * property - pointer to a valid mosquitto_property pointer. + * + * Returns: + * Binary value on success + * NULL - on error (property is NULL, or not a binary) + */ +libmosq_EXPORT const void *mosquitto_property_binary_value(const mosquitto_property *property); + + +/* + * Function: mosquitto_property_byte_value_length + * + * Return the property value for a byte type property. + * + * Parameters: + * property - pointer to a valid mosquitto_property pointer. + * + * Returns: + * Binary value length on success + * 0 - on error (property is NULL, or not a binary) + */ +libmosq_EXPORT uint16_t mosquitto_property_binary_value_length(const mosquitto_property *property); + + +/* + * Function: mosquitto_property_string_value + * + * Return the property value for a string or string pair type property. + * + * Parameters: + * property - pointer to a valid mosquitto_property pointer. + * + * Returns: + * String value on success + * NULL - on error (property is NULL, or not a string or string pair) + */ +libmosq_EXPORT const char *mosquitto_property_string_value(const mosquitto_property *property); + + +/* + * Function: mosquitto_property_string_value_length + * + * Return the length of the property value for a string or string pair type property. + * + * Parameters: + * property - pointer to a valid mosquitto_property pointer. + * + * Returns: + * Value length on success + * 0 - on error (property is NULL, or not a string or string pair) + */ +libmosq_EXPORT uint16_t mosquitto_property_string_value_length(const mosquitto_property *property); + + +/* + * Function: mosquitto_property_string_value + * + * Return the property name for a string pair type property. + * + * Parameters: + * property - pointer to a valid mosquitto_property pointer. + * + * Returns: + * String name on success + * NULL - on error (property is NULL, or not a string pair) + */ +libmosq_EXPORT const char *mosquitto_property_string_name(const mosquitto_property *property); + + +/* + * Function: mosquitto_property_string_name_length + * + * Return the property name length for a string pair type property. + * + * Parameters: + * property - pointer to a valid mosquitto_property pointer. + * + * Returns: + * Name length on success + * 0 - on error (property is NULL, or not a string pair) + */ +libmosq_EXPORT uint16_t mosquitto_property_string_name_length(const mosquitto_property *property); + + /* * Function: mosquitto_property_free_all * diff --git a/lib/linker.version b/lib/linker.version index 6d01f11f..b1313f6d 100644 --- a/lib/linker.version +++ b/lib/linker.version @@ -151,4 +151,14 @@ MOSQ_2.1 { mosquitto_unsubscribe2_v5_callback_set; mosquitto_property_remove; mosquitto_property_type; + mosquitto_property_byte_value; + mosquitto_property_int16_value; + mosquitto_property_int32_value; + mosquitto_property_varint_value; + mosquitto_property_binary_value; + mosquitto_property_binary_value_length; + mosquitto_property_string_value; + mosquitto_property_string_value_length; + mosquitto_property_string_name; + mosquitto_property_string_name_length; } MOSQ_1.7; diff --git a/lib/property_mosq.c b/lib/property_mosq.c index c4a57a8f..467cf4a3 100644 --- a/lib/property_mosq.c +++ b/lib/property_mosq.c @@ -1274,3 +1274,103 @@ BROKER_EXPORT int mosquitto_property_copy_all(mosquitto_property **dest, const m return MOSQ_ERR_SUCCESS; } + + +uint8_t mosquitto_property_byte_value(const mosquitto_property *property) +{ + if(property && property->property_type == MQTT_PROP_TYPE_BYTE){ + return property->value.i8; + }else{ + return 0; + } +} + + +uint16_t mosquitto_property_int16_value(const mosquitto_property *property) +{ + if(property && property->property_type == MQTT_PROP_TYPE_INT16){ + return property->value.i16; + }else{ + return 0; + } +} + + +uint32_t mosquitto_property_int32_value(const mosquitto_property *property) +{ + if(property && property->property_type == MQTT_PROP_TYPE_INT32){ + return property->value.i32; + }else{ + return 0; + } +} + + +uint32_t mosquitto_property_varint_value(const mosquitto_property *property) +{ + if(property && property->property_type == MQTT_PROP_TYPE_VARINT){ + return property->value.varint; + }else{ + return 0; + } +} + + +const void *mosquitto_property_binary_value(const mosquitto_property *property) +{ + if(property && property->property_type == MQTT_PROP_TYPE_BINARY){ + return property->value.bin.v; + }else{ + return NULL; + } +} + + +uint16_t mosquitto_property_binary_value_length(const mosquitto_property *property) +{ + if(property && property->property_type == MQTT_PROP_TYPE_BINARY){ + return property->value.bin.len; + }else{ + return 0; + } +} + + +const char *mosquitto_property_string_value(const mosquitto_property *property) +{ + if(property && (property->property_type == MQTT_PROP_TYPE_STRING || property->property_type == MQTT_PROP_TYPE_STRING_PAIR)){ + return property->value.s.v; + }else{ + return NULL; + } +} + + +uint16_t mosquitto_property_string_value_length(const mosquitto_property *property) +{ + if(property && (property->property_type == MQTT_PROP_TYPE_STRING || property->property_type == MQTT_PROP_TYPE_STRING_PAIR)){ + return property->value.s.len; + }else{ + return 0; + } +} + + +const char *mosquitto_property_string_name(const mosquitto_property *property) +{ + if(property && property->property_type == MQTT_PROP_TYPE_STRING_PAIR){ + return property->name.v; + }else{ + return NULL; + } +} + + +uint16_t mosquitto_property_string_name_length(const mosquitto_property *property) +{ + if(property && property->property_type == MQTT_PROP_TYPE_STRING_PAIR){ + return property->name.len; + }else{ + return 0; + } +} diff --git a/test/unit/lib/CMakeLists.txt b/test/unit/lib/CMakeLists.txt index deade7d5..179b1d52 100644 --- a/test/unit/lib/CMakeLists.txt +++ b/test/unit/lib/CMakeLists.txt @@ -6,6 +6,7 @@ add_executable(lib-test property_read.c property_user_read.c property_write.c + property_value.c strings_test.c stubs.c utf8.c diff --git a/test/unit/lib/Makefile b/test/unit/lib/Makefile index 893496a3..bf18effd 100644 --- a/test/unit/lib/Makefile +++ b/test/unit/lib/Makefile @@ -21,6 +21,7 @@ TEST_OBJS = \ property_read.o \ property_user_read.o \ property_write.o \ + property_value.o \ strings_test.o \ stubs.o \ utf8.o \ diff --git a/test/unit/lib/property_value.c b/test/unit/lib/property_value.c new file mode 100644 index 00000000..51ba7309 --- /dev/null +++ b/test/unit/lib/property_value.c @@ -0,0 +1,316 @@ +#include +#include + +#include "mqtt_protocol.h" +#include "property_mosq.h" +#include "packet_mosq.h" + +static void TEST_value_byte_success(void) +{ + mosquitto_property *property = NULL; + uint8_t value, value_set = 1; + int rc; + + rc = mosquitto_property_add_byte(&property, MQTT_PROP_PAYLOAD_FORMAT_INDICATOR, value_set); + CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); + CU_ASSERT_PTR_NOT_NULL(property); + if(property){ + value = mosquitto_property_byte_value(property); + CU_ASSERT_EQUAL(value, value_set); + + mosquitto_property_free_all(&property); + } +} + +static void TEST_value_byte_fail(void) +{ + mosquitto_property *property = NULL; + uint8_t value, value_set = 1; + int rc; + + rc = mosquitto_property_add_int16(&property, MQTT_PROP_RECEIVE_MAXIMUM, value_set); + CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); + CU_ASSERT_PTR_NOT_NULL(property); + if(property){ + value = mosquitto_property_byte_value(property); + CU_ASSERT_EQUAL(value, 0); + + mosquitto_property_free_all(&property); + } +} + +static void TEST_value_int16_success(void) +{ + mosquitto_property *property = NULL; + uint16_t value, value_set = 65535; + int rc; + + rc = mosquitto_property_add_int16(&property, MQTT_PROP_RECEIVE_MAXIMUM, value_set); + CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); + CU_ASSERT_PTR_NOT_NULL(property); + if(property){ + value = mosquitto_property_int16_value(property); + CU_ASSERT_EQUAL(value, value_set); + + mosquitto_property_free_all(&property); + } +} + +static void TEST_value_int16_fail(void) +{ + mosquitto_property *property = NULL; + uint16_t value, value_set = 65535; + int rc; + + rc = mosquitto_property_add_int32(&property, MQTT_PROP_MESSAGE_EXPIRY_INTERVAL, value_set); + CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); + CU_ASSERT_PTR_NOT_NULL(property); + if(property){ + value = mosquitto_property_int16_value(property); + CU_ASSERT_EQUAL(value, 0); + + mosquitto_property_free_all(&property); + } +} + +static void TEST_value_int32_success(void) +{ + mosquitto_property *property = NULL; + uint32_t value, value_set = 123456; + int rc; + + rc = mosquitto_property_add_int32(&property, MQTT_PROP_MESSAGE_EXPIRY_INTERVAL, value_set); + CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); + CU_ASSERT_PTR_NOT_NULL(property); + if(property){ + value = mosquitto_property_int32_value(property); + CU_ASSERT_EQUAL(value, value_set); + + mosquitto_property_free_all(&property); + } +} + +static void TEST_value_int32_fail(void) +{ + mosquitto_property *property = NULL; + uint32_t value, value_set = 123456; + int rc; + + rc = mosquitto_property_add_varint(&property, MQTT_PROP_SUBSCRIPTION_IDENTIFIER, value_set); + CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); + CU_ASSERT_PTR_NOT_NULL(property); + if(property){ + value = mosquitto_property_int32_value(property); + CU_ASSERT_EQUAL(value, 0); + + mosquitto_property_free_all(&property); + } +} + +static void TEST_value_varint_success(void) +{ + mosquitto_property *property = NULL; + uint32_t value, value_set = 654321; + int rc; + + rc = mosquitto_property_add_varint(&property, MQTT_PROP_SUBSCRIPTION_IDENTIFIER, value_set); + CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); + CU_ASSERT_PTR_NOT_NULL(property); + if(property){ + value = mosquitto_property_varint_value(property); + CU_ASSERT_EQUAL(value, value_set); + + mosquitto_property_free_all(&property); + } +} + +static void TEST_value_varint_fail(void) +{ + mosquitto_property *property = NULL; + uint32_t value; + int rc; + + rc = mosquitto_property_add_int16(&property, MQTT_PROP_RECEIVE_MAXIMUM, 1); + CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); + CU_ASSERT_PTR_NOT_NULL(property); + if(property){ + value = mosquitto_property_varint_value(property); + CU_ASSERT_EQUAL(value, 0); + + mosquitto_property_free_all(&property); + } +} + +static void TEST_value_binary_success(void) +{ + mosquitto_property *property = NULL; + uint8_t value_set[] = {0, 1, 2, 3, 4, 5, 6, 7, 8}; + const uint8_t *value; + uint16_t len; + int rc; + + rc = mosquitto_property_add_binary(&property, MQTT_PROP_AUTHENTICATION_DATA, value_set, sizeof(value_set)); + CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); + CU_ASSERT_PTR_NOT_NULL(property); + if(property){ + len = mosquitto_property_binary_value_length(property); + value = mosquitto_property_binary_value(property); + CU_ASSERT_EQUAL(len, sizeof(value_set)); + CU_ASSERT_NSTRING_EQUAL(value, value_set, sizeof(value_set)); + + mosquitto_property_free_all(&property); + } +} + +static void TEST_value_binary_fail(void) +{ + mosquitto_property *property = NULL; + const uint8_t *value; + uint16_t len; + int rc; + + rc = mosquitto_property_add_int16(&property, MQTT_PROP_RECEIVE_MAXIMUM, 1); + CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); + CU_ASSERT_PTR_NOT_NULL(property); + if(property){ + len = mosquitto_property_binary_value_length(property); + value = mosquitto_property_binary_value(property); + CU_ASSERT_EQUAL(len, 0); + CU_ASSERT_PTR_NULL(value); + + mosquitto_property_free_all(&property); + } +} + +static void TEST_value_string_success(void) +{ + mosquitto_property *property = NULL; + char value_set[] = "test"; + const char *value; + uint16_t len; + int rc; + + rc = mosquitto_property_add_string(&property, MQTT_PROP_AUTHENTICATION_METHOD, value_set); + CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); + CU_ASSERT_PTR_NOT_NULL(property); + if(property){ + len = mosquitto_property_string_value_length(property); + value = mosquitto_property_string_value(property); + CU_ASSERT_EQUAL(len, strlen(value_set)); + CU_ASSERT_NSTRING_EQUAL(value, value_set, strlen(value_set)); + + mosquitto_property_free_all(&property); + } +} + +static void TEST_value_string_fail(void) +{ + mosquitto_property *property = NULL; + const char *value; + uint16_t len; + int rc; + + rc = mosquitto_property_add_int16(&property, MQTT_PROP_RECEIVE_MAXIMUM, 1); + CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); + CU_ASSERT_PTR_NOT_NULL(property); + if(property){ + len = mosquitto_property_string_value_length(property); + value = mosquitto_property_string_value(property); + CU_ASSERT_EQUAL(len, 0); + CU_ASSERT_PTR_NULL(value); + + mosquitto_property_free_all(&property); + } +} + +static void TEST_value_string_pair_success(void) +{ + mosquitto_property *property = NULL; + char value_set[] = "value"; + const char *value; + uint16_t len; + char name_set[] = "value"; + const char *name; + int rc; + + rc = mosquitto_property_add_string_pair(&property, MQTT_PROP_USER_PROPERTY, value_set, name_set); + CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); + CU_ASSERT_PTR_NOT_NULL(property); + if(property){ + len = mosquitto_property_string_value_length(property); + value = mosquitto_property_string_value(property); + CU_ASSERT_EQUAL(len, strlen(value_set)); + CU_ASSERT_NSTRING_EQUAL(value, value_set, strlen(value_set)); + + len = mosquitto_property_string_name_length(property); + name = mosquitto_property_string_name(property); + CU_ASSERT_EQUAL(len, strlen(name_set)); + CU_ASSERT_NSTRING_EQUAL(name, name_set, strlen(name_set)); + + mosquitto_property_free_all(&property); + } +} + +static void TEST_value_string_pair_fail(void) +{ + mosquitto_property *property = NULL; + const char *value; + uint16_t len; + const char *name; + int rc; + + rc = mosquitto_property_add_int16(&property, MQTT_PROP_RECEIVE_MAXIMUM, 1); + CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); + CU_ASSERT_PTR_NOT_NULL(property); + if(property){ + len = mosquitto_property_string_value_length(property); + value = mosquitto_property_string_value(property); + CU_ASSERT_EQUAL(len, 0); + CU_ASSERT_PTR_NULL(value); + + len = mosquitto_property_string_name_length(property); + name = mosquitto_property_string_name(property); + CU_ASSERT_EQUAL(len, 0); + CU_ASSERT_PTR_NULL(name); + + mosquitto_property_free_all(&property); + } +} + +/* ======================================================================== + * TEST SUITE SETUP + * ======================================================================== */ + +int init_property_value_tests(void) +{ + CU_pSuite test_suite = NULL; + + test_suite = CU_add_suite("Property value", NULL, NULL); + if(!test_suite){ + printf("Error adding CUnit Property value test suite.\n"); + return 1; + } + + if(0 + || !CU_add_test(test_suite, "Byte value success", TEST_value_byte_success) + || !CU_add_test(test_suite, "Int16 value success", TEST_value_int16_success) + || !CU_add_test(test_suite, "Int32 value success", TEST_value_int32_success) + || !CU_add_test(test_suite, "Varint value success", TEST_value_varint_success) + || !CU_add_test(test_suite, "Binary value success", TEST_value_binary_success) + || !CU_add_test(test_suite, "String value success", TEST_value_string_success) + || !CU_add_test(test_suite, "String pair value success", TEST_value_string_pair_success) + || !CU_add_test(test_suite, "Byte value fail", TEST_value_byte_fail) + || !CU_add_test(test_suite, "Int16 value fail", TEST_value_int16_fail) + || !CU_add_test(test_suite, "Int32 value fail", TEST_value_int32_fail) + || !CU_add_test(test_suite, "Varint value fail", TEST_value_varint_fail) + || !CU_add_test(test_suite, "Binary value fail", TEST_value_binary_fail) + || !CU_add_test(test_suite, "String value fail", TEST_value_string_fail) + || !CU_add_test(test_suite, "String pair value fail", TEST_value_string_pair_fail) + ){ + + printf("Error adding Property Value CUnit tests.\n"); + return 1; + } + + return 0; +} diff --git a/test/unit/lib/test.c b/test/unit/lib/test.c index dcc5cc63..44bd4410 100644 --- a/test/unit/lib/test.c +++ b/test/unit/lib/test.c @@ -10,6 +10,7 @@ int init_property_add_tests(void); int init_property_read_tests(void); int init_property_user_read_tests(void); int init_property_write_tests(void); +int init_property_value_tests(void); int init_strings_tests(void); int init_utf8_tests(void); int init_util_topic_tests(void); @@ -35,6 +36,7 @@ int main(int argc, char *argv[]) || init_property_read_tests() || init_property_user_read_tests() || init_property_write_tests() + || init_property_value_tests() || init_util_topic_tests() || init_misc_trim_tests() || init_strings_tests()