From f36af1eb3c47b96ecf1f5c4010efb3d9e6ea261f Mon Sep 17 00:00:00 2001 From: danewalton Date: Tue, 25 Aug 2020 15:25:00 -0700 Subject: [PATCH] Rename az_iot_hub_client_properties to az_iot_message_properties (#1124) --- CHANGELOG.md | 4 +- sdk/docs/iot/README.md | 12 +- sdk/docs/iot/resources/iot_hub_flow.puml | 4 +- sdk/inc/azure/iot/az_iot_common.h | 105 +++- sdk/inc/azure/iot/az_iot_hub_client.h | 100 +--- sdk/samples/iot/sample_pnp.c | 8 +- sdk/samples/iot/sample_pnp.h | 4 +- sdk/src/azure/iot/az_iot_common.c | 126 +++- sdk/src/azure/iot/az_iot_hub_client.c | 116 ---- sdk/src/azure/iot/az_iot_hub_client_c2d.c | 2 +- .../azure/iot/az_iot_hub_client_telemetry.c | 2 +- sdk/src/azure/iot/az_iot_hub_client_twin.c | 14 +- sdk/tests/iot/common/test_az_iot_common.c | 557 +++++++++++++++++- sdk/tests/iot/hub/test_az_iot_hub_client.c | 532 ----------------- .../iot/hub/test_az_iot_hub_client_c2d.c | 14 +- .../hub/test_az_iot_hub_client_telemetry.c | 32 +- 16 files changed, 824 insertions(+), 808 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5187a863c8..37998478d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,10 +5,12 @@ ### New Features - Add `az_json_writer_append_json_text()` to support appending existing JSON with the JSON writer. -- Add supported, system properties for IoT Hub messages to az_iot_common.h +- Add support for system properties for IoT Hub messages to `az_iot_common.h`. ### Breaking Changes +- Rename `az_iot_hub_client_properties` to `az_iot_message_properties` and move it from `az_iot_hub_client.h` to `az_iot_common.h`. + ### Bug Fixes ### Other Changes and Improvements diff --git a/sdk/docs/iot/README.md b/sdk/docs/iot/README.md index bdb32d0660..8e3801040f 100644 --- a/sdk/docs/iot/README.md +++ b/sdk/docs/iot/README.md @@ -137,7 +137,7 @@ int main() ### Properties -Included in the Azure SDK for Embedded C are helper functions to form and manage properties for IoT Hub services. Implementation starts by using the `az_iot_hub_client_properties_init()` API. The user is free to initialize using an empty, but appropriately sized, span to later append properties or an already populated span containing a properly formatted property buffer. "Properly formatted" properties follow the form `{key}={value}&{key}={value}`. +Included in the Azure SDK for Embedded C are helper functions to form and manage properties for IoT Hub services. Implementation starts by using the `az_iot_message_properties_init()` API. The user is free to initialize using an empty, but appropriately sized, span to later append properties or an already populated span containing a properly formatted property buffer. "Properly formatted" properties follow the form `{key}={value}&{key}={value}`. Below is an example use case of appending properties. @@ -150,10 +150,10 @@ void my_property_func() az_span property_span = az_span_create(property_buffer, sizeof(property_buffer)); //Initialize the property struct with the span - az_iot_hub_client_properties props; - az_iot_hub_client_properties_init(&props, property_span, 0); + az_iot_message_properties props; + az_iot_message_properties_init(&props, property_span, 0); //Append properties - az_iot_hub_client_properties_append(&props, AZ_SPAN_FROM_STR("key"), AZ_SPAN_FROM_STR("value")); + az_iot_message_properties_append(&props, AZ_SPAN_FROM_STR("key"), AZ_SPAN_FROM_STR("value")); //At this point, you are able to pass the `props` to other APIs with property parameters. } ``` @@ -166,8 +166,8 @@ static az_span my_prop_span = AZ_SPAN_LITERAL_FROM_STR("my_device=contoso&my_key void my_property_func() { //Initialize the property struct with the span - az_iot_hub_client_properties props; - az_iot_hub_client_properties_init(&props, my_prop_span, az_span_size(my_prop_span)); + az_iot_message_properties props; + az_iot_message_properties_init(&props, my_prop_span, az_span_size(my_prop_span)); //At this point, you are able to pass the `props` to other APIs with property parameters. } ``` diff --git a/sdk/docs/iot/resources/iot_hub_flow.puml b/sdk/docs/iot/resources/iot_hub_flow.puml index fd34e14fc6..f62f2d95ff 100644 --- a/sdk/docs/iot/resources/iot_hub_flow.puml +++ b/sdk/docs/iot/resources/iot_hub_flow.puml @@ -74,7 +74,7 @@ az_iot_hub_client_sas_get_signature : - token_expiration_epoch_time az_iot_hub_client_sas_get_password: - Base64(HMAC-SHA256(signature, SharedAccessKey)) az_iot_hub_client_sas_get_password: - key_name -az_iot_hub_client_telemetry_get_publish_topic : - az_iot_hub_client_properties +az_iot_hub_client_telemetry_get_publish_topic : - az_iot_message_properties state az_iot_hub_client_method_request <> az_iot_hub_client_method_request: - request_id @@ -84,7 +84,7 @@ az_iot_hub_client_methods_response_get_publish_topic: - request_id az_iot_hub_client_methods_response_get_publish_topic: - status state az_iot_hub_client_c2d_request <> -az_iot_hub_client_c2d_request : - az_iot_hub_client_properties +az_iot_hub_client_c2d_request : - az_iot_message_properties az_iot_hub_client_twin_document_get_publish_topic : - request_id diff --git a/sdk/inc/azure/iot/az_iot_common.h b/sdk/inc/azure/iot/az_iot_common.h index 0c616423d6..b9cf087e86 100644 --- a/sdk/inc/azure/iot/az_iot_common.h +++ b/sdk/inc/azure/iot/az_iot_common.h @@ -61,17 +61,112 @@ typedef enum AZ_IOT_STATUS_TIMEOUT = 504, } az_iot_status; + /** - * @brief Supported IoT Hub message properties + * + * Properties APIs + * + * IoT message properties are used for Device-to-Cloud (D2C) as well as Cloud-to-Device (C2D). + * Properties are always appended to the MQTT topic of the published or received message and + * must contain Uri-encoded keys and values. */ -#define AZ_IOT_HUB_MESSAGE_PROPERTIES_MESSAGE_ID \ +/** + * @brief Supported IoT message properties + */ +#define AZ_IOT_MESSAGE_PROPERTIES_MESSAGE_ID \ "%24.mid" /**< Add unique identification to a message */ -#define AZ_IOT_HUB_MESSAGE_PROPERTIES_CORRELATION_ID \ +#define AZ_IOT_MESSAGE_PROPERTIES_CORRELATION_ID \ "%24.cid" /**< Used in distributed tracing. More information here: \ https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-distributed-tracing */ -#define AZ_IOT_HUB_MESSAGE_PROPERTIES_CONTENT_TYPE \ +#define AZ_IOT_MESSAGE_PROPERTIES_CONTENT_TYPE \ "%24.ct" /**< URL encoded and of the form text%2Fplain or application%2Fjson, etc */ -#define AZ_IOT_HUB_MESSAGE_PROPERTIES_CONTENT_ENCODING "%24.ce" /**< UTF-8, UTF-16, etc */ +#define AZ_IOT_MESSAGE_PROPERTIES_CONTENT_ENCODING "%24.ce" /**< UTF-8, UTF-16, etc */ + +/** + * @brief Telemetry or C2D properties. + * + */ +typedef struct +{ + struct + { + az_span properties_buffer; + int32_t properties_written; + uint32_t current_property_index; + } _internal; +} az_iot_message_properties; + +/** + * @brief Initializes the Telemetry or C2D properties. + * + * @note The properties init API will not encode properties. In order to support + * the following characters, they must be percent-encoded (RFC3986) as follows: + * `/` : `%2F` + * `%` : `%25` + * `#` : `%23` + * `&` : `%26` + * Only these characters would have to be encoded. If you would like to avoid the need to + * encode the names/values, avoid using these characters in names and values. + * + * @param[in] properties The #az_iot_message_properties to initialize + * @param[in] buffer Can either be an empty #az_span or an #az_span containing properly formatted + * (with above mentioned characters encoded if applicable) properties with the + * following format: {key}={value}&{key}={value}. + * @param[in] written_length The length of the properly formatted properties already initialized + * within the buffer. If the \p buffer is empty (uninitialized), this should be 0. + * @return #az_result + */ +AZ_NODISCARD az_result az_iot_message_properties_init( + az_iot_message_properties* properties, + az_span buffer, + int32_t written_length); + +/** + * @brief Appends a key-value property to the list of properties. + * + * @note The properties append API will not encode properties. In order to support + * the following characters, they must be percent-encoded (RFC3986) as follows: + * `/` : `%2F` + * `%` : `%25` + * `#` : `%23` + * `&` : `%26` + * Only these characters would have to be encoded. If you would like to avoid the need to + * encode the names/values, avoid using these characters in names and values. + * + * @param[in] properties The #az_iot_message_properties to use for this call + * @param[in] name The name of the property. + * @param[in] value The value of the property. + * @return #az_result + */ +AZ_NODISCARD az_result az_iot_message_properties_append( + az_iot_message_properties* properties, + az_span name, + az_span value); + +/** + * @brief Finds the value of a property. + * @remark This will return the first value of the property with the given name if multiple + * properties with the same key exist. + * + * @param[in] properties The #az_iot_message_properties to use for this call + * @param[in] name The name of the property. + * @param[out] out_value An #az_span containing the value of the property. + * @return #az_result. + */ +AZ_NODISCARD az_result az_iot_message_properties_find( + az_iot_message_properties* properties, + az_span name, + az_span* out_value); + +/** + * @brief Iterates over the list of properties. + * + * @param[in] properties The #az_iot_message_properties to use for this call + * @param[out] out An #az_pair containing the key and the value of the next property. + * @return #az_result + */ +AZ_NODISCARD az_result +az_iot_message_properties_next(az_iot_message_properties* properties, az_pair* out); /** * @brief Checks if the status indicates a successful operation. diff --git a/sdk/inc/azure/iot/az_iot_hub_client.h b/sdk/inc/azure/iot/az_iot_hub_client.h index 8f5cee2a8e..9b44e528c1 100644 --- a/sdk/inc/azure/iot/az_iot_hub_client.h +++ b/sdk/inc/azure/iot/az_iot_hub_client.h @@ -210,100 +210,6 @@ AZ_NODISCARD az_result az_iot_hub_client_sas_get_password( size_t mqtt_password_size, size_t* out_mqtt_password_length); -/** - * - * Properties APIs - * - * IoT Hub message properties are used for Device-to-Cloud (D2C) as well as Cloud-to-Device (C2D). - * Properties are always appended to the MQTT topic of the published or received message and - * must contain Uri-encoded keys and values. - */ - -/** - * @brief Telemetry or C2D properties. - * - */ -typedef struct -{ - struct - { - az_span properties_buffer; - int32_t properties_written; - uint32_t current_property_index; - } _internal; -} az_iot_hub_client_properties; - -/** - * @brief Initializes the Telemetry or C2D properties. - * - * @note The properties init API will not encode properties. In order to support - * the following characters, they must be percent-encoded (RFC3986) as follows: - * `/` : `%2F` - * `%` : `%25` - * `#` : `%23` - * `&` : `%26` - * Only these characters would have to be encoded. If you would like to avoid the need to - * encode the names/values, avoid using these characters in names and values. - * - * @param[in] properties The #az_iot_hub_client_properties to initialize - * @param[in] buffer Can either be an empty #az_span or an #az_span containing properly formatted - * (with above mentioned characters encoded if applicable) properties with the - * following format: {key}={value}&{key}={value}. - * @param[in] written_length The length of the properly formatted properties already initialized - * within the buffer. If the \p buffer is empty (uninitialized), this should be 0. - * @return #az_result - */ -AZ_NODISCARD az_result az_iot_hub_client_properties_init( - az_iot_hub_client_properties* properties, - az_span buffer, - int32_t written_length); - -/** - * @brief Appends a key-value property to the list of properties. - * - * @note The properties append API will not encode properties. In order to support - * the following characters, they must be percent-encoded (RFC3986) as follows: - * `/` : `%2F` - * `%` : `%25` - * `#` : `%23` - * `&` : `%26` - * Only these characters would have to be encoded. If you would like to avoid the need to - * encode the names/values, avoid using these characters in names and values. - * - * @param[in] properties The #az_iot_hub_client_properties to use for this call - * @param[in] name The name of the property. - * @param[in] value The value of the property. - * @return #az_result - */ -AZ_NODISCARD az_result az_iot_hub_client_properties_append( - az_iot_hub_client_properties* properties, - az_span name, - az_span value); - -/** - * @brief Finds the value of a property. - * @remark This will return the first value of the property with the given name if multiple - * properties with the same key exist. - * - * @param[in] properties The #az_iot_hub_client_properties to use for this call - * @param[in] name The name of the property. - * @param[out] out_value An #az_span containing the value of the property. - * @return #az_result. - */ -AZ_NODISCARD az_result az_iot_hub_client_properties_find( - az_iot_hub_client_properties* properties, - az_span name, - az_span* out_value); - -/** - * @brief Iterates over the list of properties. - * - * @param[in] properties The #az_iot_hub_client_properties to use for this call - * @param[out] out An #az_pair containing the key and the value of the next property. - * @return #az_result - */ -AZ_NODISCARD az_result -az_iot_hub_client_properties_next(az_iot_hub_client_properties* properties, az_pair* out); /** * @@ -317,7 +223,7 @@ az_iot_hub_client_properties_next(az_iot_hub_client_properties* properties, az_p * @remark This topic can also be used to set the MQTT Will message in the Connect message. * * @param[in] client The #az_iot_hub_client to use for this call. - * @param[in] properties An optional #az_iot_hub_client_properties object (can be NULL). + * @param[in] properties An optional #az_iot_message_properties object (can be NULL). * @param[out] mqtt_topic A buffer with sufficient capacity to hold the MQTT topic. If * successful, contains a null-terminated string with the topic that * needs to be passed to the MQTT client. @@ -328,7 +234,7 @@ az_iot_hub_client_properties_next(az_iot_hub_client_properties* properties, az_p */ AZ_NODISCARD az_result az_iot_hub_client_telemetry_get_publish_topic( az_iot_hub_client const* client, - az_iot_hub_client_properties const* properties, + az_iot_message_properties const* properties, char* mqtt_topic, size_t mqtt_topic_size, size_t* out_mqtt_topic_length); @@ -351,7 +257,7 @@ AZ_NODISCARD az_result az_iot_hub_client_telemetry_get_publish_topic( */ typedef struct { - az_iot_hub_client_properties properties; /**< The properties associated with this C2D request. */ + az_iot_message_properties properties; /**< The properties associated with this C2D request. */ } az_iot_hub_client_c2d_request; /** diff --git a/sdk/samples/iot/sample_pnp.c b/sdk/samples/iot/sample_pnp.c index bf92d0403f..c05d8087c5 100644 --- a/sdk/samples/iot/sample_pnp.c +++ b/sdk/samples/iot/sample_pnp.c @@ -144,13 +144,13 @@ static az_result is_component_in_model( // Get the telemetry topic for PnP az_result pnp_get_telemetry_topic( const az_iot_hub_client* client, - az_iot_hub_client_properties* properties, + az_iot_message_properties* properties, az_span component_name, char* mqtt_topic, size_t mqtt_topic_size, size_t* out_mqtt_topic_length) { - az_iot_hub_client_properties pnp_properties; + az_iot_message_properties pnp_properties; if (az_span_ptr(component_name) != NULL) { @@ -158,11 +158,11 @@ az_result pnp_get_telemetry_topic( { properties = &pnp_properties; - AZ_RETURN_IF_FAILED(az_iot_hub_client_properties_init( + AZ_RETURN_IF_FAILED(az_iot_message_properties_init( properties, AZ_SPAN_FROM_BUFFER(pnp_properties_buffer), 0)); } - AZ_RETURN_IF_FAILED(az_iot_hub_client_properties_append( + AZ_RETURN_IF_FAILED(az_iot_message_properties_append( properties, component_telemetry_prop_span, component_name)); } diff --git a/sdk/samples/iot/sample_pnp.h b/sdk/samples/iot/sample_pnp.h index b9db8a32ee..3ae7e37535 100644 --- a/sdk/samples/iot/sample_pnp.h +++ b/sdk/samples/iot/sample_pnp.h @@ -35,7 +35,7 @@ typedef az_result (*pnp_append_property_callback)(az_json_writer* jw, void* cont * @remark This topic can also be used to set the MQTT Will message in the Connect message. * * @param[in] client The #az_iot_hub_client to use for this call. - * @param[in] properties An optional #az_iot_hub_client_properties object (can be NULL). + * @param[in] properties An optional #az_iot_message_properties object (can be NULL). * @param[in] component_name An optional component name if the telemetry is being sent from a * sub-component. * @param[out] mqtt_topic A buffer with sufficient capacity to hold the MQTT topic. If @@ -48,7 +48,7 @@ typedef az_result (*pnp_append_property_callback)(az_json_writer* jw, void* cont */ az_result pnp_get_telemetry_topic( const az_iot_hub_client* client, - az_iot_hub_client_properties* properties, + az_iot_message_properties* properties, az_span component_name, char* mqtt_topic, size_t mqtt_topic_size, diff --git a/sdk/src/azure/iot/az_iot_common.c b/sdk/src/azure/iot/az_iot_common.c index 403d9cf855..21785d769f 100644 --- a/sdk/src/azure/iot/az_iot_common.c +++ b/sdk/src/azure/iot/az_iot_common.c @@ -3,18 +3,135 @@ #include -#include -#include -#include #include #include +#include #include +#include +#include #include #include #include +static const az_span hub_client_param_separator_span = AZ_SPAN_LITERAL_FROM_STR("&"); +static const az_span hub_client_param_equals_span = AZ_SPAN_LITERAL_FROM_STR("="); + +AZ_NODISCARD az_result az_iot_message_properties_init( + az_iot_message_properties* properties, + az_span buffer, + int32_t written_length) +{ + _az_PRECONDITION_NOT_NULL(properties); + _az_PRECONDITION_VALID_SPAN(buffer, 0, true); + _az_PRECONDITION(written_length >= 0); + + properties->_internal.properties_buffer = buffer; + properties->_internal.properties_written = written_length; + properties->_internal.current_property_index = 0; + + return AZ_OK; +} + +AZ_NODISCARD az_result +az_iot_message_properties_append(az_iot_message_properties* properties, az_span name, az_span value) +{ + _az_PRECONDITION_NOT_NULL(properties); + _az_PRECONDITION_VALID_SPAN(name, 1, false); + _az_PRECONDITION_VALID_SPAN(value, 1, false); + + int32_t prop_length = properties->_internal.properties_written; + + az_span remainder = az_span_slice_to_end(properties->_internal.properties_buffer, prop_length); + + int32_t required_length = az_span_size(name) + az_span_size(value) + 1; + + if (prop_length > 0) + { + required_length += 1; + } + + AZ_RETURN_IF_NOT_ENOUGH_SIZE(remainder, required_length); + + if (prop_length > 0) + { + remainder = az_span_copy_u8(remainder, *az_span_ptr(hub_client_param_separator_span)); + } + + remainder = az_span_copy(remainder, name); + remainder = az_span_copy_u8(remainder, *az_span_ptr(hub_client_param_equals_span)); + az_span_copy(remainder, value); + + properties->_internal.properties_written += required_length; + + return AZ_OK; +} + +AZ_NODISCARD az_result az_iot_message_properties_find( + az_iot_message_properties* properties, + az_span name, + az_span* out_value) +{ + _az_PRECONDITION_NOT_NULL(properties); + _az_PRECONDITION_VALID_SPAN(name, 1, false); + _az_PRECONDITION_NOT_NULL(out_value); + + az_span remaining = az_span_slice( + properties->_internal.properties_buffer, 0, properties->_internal.properties_written); + + while (az_span_size(remaining) != 0) + { + az_span delim_span = _az_span_token(remaining, hub_client_param_equals_span, &remaining); + if (az_span_is_content_equal(delim_span, name)) + { + *out_value = _az_span_token(remaining, hub_client_param_separator_span, &remaining); + return AZ_OK; + } + else + { + az_span value; + value = _az_span_token(remaining, hub_client_param_separator_span, &remaining); + (void)value; + } + } + + return AZ_ERROR_ITEM_NOT_FOUND; +} + +AZ_NODISCARD az_result +az_iot_message_properties_next(az_iot_message_properties* properties, az_pair* out) +{ + _az_PRECONDITION_NOT_NULL(properties); + _az_PRECONDITION_NOT_NULL(out); + + int32_t index = (int32_t)properties->_internal.current_property_index; + int32_t prop_length = properties->_internal.properties_written; + + if (index == prop_length) + { + *out = AZ_PAIR_NULL; + return AZ_ERROR_UNEXPECTED_END; + } + + az_span remainder; + az_span prop_span = az_span_slice(properties->_internal.properties_buffer, index, prop_length); + + out->key = _az_span_token(prop_span, hub_client_param_equals_span, &remainder); + out->value = _az_span_token(remainder, hub_client_param_separator_span, &remainder); + if (az_span_size(remainder) == 0) + { + properties->_internal.current_property_index = (uint32_t)prop_length; + } + else + { + properties->_internal.current_property_index + = (uint32_t)(az_span_ptr(remainder) - az_span_ptr(properties->_internal.properties_buffer)); + } + + return AZ_OK; +} + AZ_NODISCARD int32_t az_iot_retry_calc_delay( int32_t operation_msec, int16_t attempt, @@ -87,7 +204,8 @@ AZ_NODISCARD int32_t _az_iot_u64toa_size(uint64_t number) } } -AZ_NODISCARD az_result _az_span_copy_url_encode(az_span destination, az_span source, az_span* out_remainder) +AZ_NODISCARD az_result +_az_span_copy_url_encode(az_span destination, az_span source, az_span* out_remainder) { int32_t length; AZ_RETURN_IF_FAILED(_az_span_url_encode(destination, source, &length)); diff --git a/sdk/src/azure/iot/az_iot_hub_client.c b/sdk/src/azure/iot/az_iot_hub_client.c index b97f5b2fc1..4fb98ba32a 100644 --- a/sdk/src/azure/iot/az_iot_hub_client.c +++ b/sdk/src/azure/iot/az_iot_hub_client.c @@ -180,119 +180,3 @@ AZ_NODISCARD az_result az_iot_hub_client_get_client_id( return AZ_OK; } - -AZ_NODISCARD az_result az_iot_hub_client_properties_init( - az_iot_hub_client_properties* properties, - az_span buffer, - int32_t written_length) -{ - _az_PRECONDITION_NOT_NULL(properties); - _az_PRECONDITION_VALID_SPAN(buffer, 0, true); - _az_PRECONDITION(written_length >= 0); - - properties->_internal.properties_buffer = buffer; - properties->_internal.properties_written = written_length; - properties->_internal.current_property_index = 0; - - return AZ_OK; -} - -AZ_NODISCARD az_result az_iot_hub_client_properties_append( - az_iot_hub_client_properties* properties, - az_span name, - az_span value) -{ - _az_PRECONDITION_NOT_NULL(properties); - _az_PRECONDITION_VALID_SPAN(name, 1, false); - _az_PRECONDITION_VALID_SPAN(value, 1, false); - - int32_t prop_length = properties->_internal.properties_written; - - az_span remainder = az_span_slice_to_end(properties->_internal.properties_buffer, prop_length); - - int32_t required_length = az_span_size(name) + az_span_size(value) + 1; - - if (prop_length > 0) - { - required_length += 1; - } - - AZ_RETURN_IF_NOT_ENOUGH_SIZE(remainder, required_length); - - if (prop_length > 0) - { - remainder = az_span_copy_u8(remainder, *az_span_ptr(hub_client_param_separator_span)); - } - - remainder = az_span_copy(remainder, name); - remainder = az_span_copy_u8(remainder, *az_span_ptr(hub_client_param_equals_span)); - az_span_copy(remainder, value); - - properties->_internal.properties_written += required_length; - - return AZ_OK; -} - -AZ_NODISCARD az_result az_iot_hub_client_properties_find( - az_iot_hub_client_properties* properties, - az_span name, - az_span* out_value) -{ - _az_PRECONDITION_NOT_NULL(properties); - _az_PRECONDITION_VALID_SPAN(name, 1, false); - _az_PRECONDITION_NOT_NULL(out_value); - - az_span remaining = az_span_slice( - properties->_internal.properties_buffer, 0, properties->_internal.properties_written); - - while (az_span_size(remaining) != 0) - { - az_span delim_span = _az_span_token(remaining, hub_client_param_equals_span, &remaining); - if (az_span_is_content_equal(delim_span, name)) - { - *out_value = _az_span_token(remaining, hub_client_param_separator_span, &remaining); - return AZ_OK; - } - else - { - az_span value; - value = _az_span_token(remaining, hub_client_param_separator_span, &remaining); - (void)value; - } - } - - return AZ_ERROR_ITEM_NOT_FOUND; -} - -AZ_NODISCARD az_result -az_iot_hub_client_properties_next(az_iot_hub_client_properties* properties, az_pair* out) -{ - _az_PRECONDITION_NOT_NULL(properties); - _az_PRECONDITION_NOT_NULL(out); - - int32_t index = (int32_t)properties->_internal.current_property_index; - int32_t prop_length = properties->_internal.properties_written; - - if (index == prop_length) - { - *out = AZ_PAIR_NULL; - return AZ_ERROR_UNEXPECTED_END; - } - - az_span remainder; - az_span prop_span = az_span_slice(properties->_internal.properties_buffer, index, prop_length); - - out->key = _az_span_token(prop_span, hub_client_param_equals_span, &remainder); - out->value = _az_span_token(remainder, hub_client_param_separator_span, &remainder); - if (az_span_size(remainder) == 0) - { - properties->_internal.current_property_index = (uint32_t)prop_length; - } - else - { - properties->_internal.current_property_index - = (uint32_t)(az_span_ptr(remainder) - az_span_ptr(properties->_internal.properties_buffer)); - } - - return AZ_OK; -} diff --git a/sdk/src/azure/iot/az_iot_hub_client_c2d.c b/sdk/src/azure/iot/az_iot_hub_client_c2d.c index 3f03b1df04..6918b414fd 100644 --- a/sdk/src/azure/iot/az_iot_hub_client_c2d.c +++ b/sdk/src/azure/iot/az_iot_hub_client_c2d.c @@ -36,7 +36,7 @@ AZ_NODISCARD az_result az_iot_hub_client_c2d_parse_received_topic( token = _az_span_token(reminder, c2d_topic_suffix, &reminder); AZ_RETURN_IF_FAILED( - az_iot_hub_client_properties_init(&out_request->properties, token, az_span_size(token))); + az_iot_message_properties_init(&out_request->properties, token, az_span_size(token))); return AZ_OK; } diff --git a/sdk/src/azure/iot/az_iot_hub_client_telemetry.c b/sdk/src/azure/iot/az_iot_hub_client_telemetry.c index e839bb3e50..fda07510cf 100644 --- a/sdk/src/azure/iot/az_iot_hub_client_telemetry.c +++ b/sdk/src/azure/iot/az_iot_hub_client_telemetry.c @@ -18,7 +18,7 @@ static const az_span telemetry_topic_suffix = AZ_SPAN_LITERAL_FROM_STR("/message AZ_NODISCARD az_result az_iot_hub_client_telemetry_get_publish_topic( az_iot_hub_client const* client, - az_iot_hub_client_properties const* properties, + az_iot_message_properties const* properties, char* mqtt_topic, size_t mqtt_topic_size, size_t* out_mqtt_topic_length) diff --git a/sdk/src/azure/iot/az_iot_hub_client_twin.c b/sdk/src/azure/iot/az_iot_hub_client_twin.c index 340f2f2803..a7d99beb8d 100644 --- a/sdk/src/azure/iot/az_iot_hub_client_twin.c +++ b/sdk/src/azure/iot/az_iot_hub_client_twin.c @@ -144,18 +144,18 @@ AZ_NODISCARD az_result az_iot_hub_client_twin_parse_received_topic( out_twin_response->status = (az_iot_status)status_int; // Get request id prop value - az_iot_hub_client_properties props; + az_iot_message_properties props; az_span prop_span = az_span_slice(remainder, 1, az_span_size(remainder)); AZ_RETURN_IF_FAILED( - az_iot_hub_client_properties_init(&props, prop_span, az_span_size(prop_span))); - AZ_RETURN_IF_FAILED(az_iot_hub_client_properties_find( + az_iot_message_properties_init(&props, prop_span, az_span_size(prop_span))); + AZ_RETURN_IF_FAILED(az_iot_message_properties_find( &props, az_iot_hub_client_request_id_span, &out_twin_response->request_id)); if (out_twin_response->status == AZ_IOT_STATUS_NO_CONTENT) { // Is a reported prop response out_twin_response->response_type = AZ_IOT_CLIENT_TWIN_RESPONSE_TYPE_REPORTED_PROPERTIES; - AZ_RETURN_IF_FAILED(az_iot_hub_client_properties_find( + AZ_RETURN_IF_FAILED(az_iot_message_properties_find( &props, az_iot_hub_twin_version_prop, &out_twin_response->version)); } else @@ -172,15 +172,15 @@ AZ_NODISCARD az_result az_iot_hub_client_twin_parse_received_topic( >= 0) { // Is a /PATCH case (desired props) - az_iot_hub_client_properties props; + az_iot_message_properties props; az_span prop_span = az_span_slice( received_topic, twin_feature_index + az_span_size(az_iot_hub_twin_patch_sub_topic) + (int32_t)sizeof(az_iot_hub_client_twin_question), az_span_size(received_topic)); AZ_RETURN_IF_FAILED( - az_iot_hub_client_properties_init(&props, prop_span, az_span_size(prop_span))); - AZ_RETURN_IF_FAILED(az_iot_hub_client_properties_find( + az_iot_message_properties_init(&props, prop_span, az_span_size(prop_span))); + AZ_RETURN_IF_FAILED(az_iot_message_properties_find( &props, az_iot_hub_twin_version_prop, &out_twin_response->version)); out_twin_response->response_type = AZ_IOT_CLIENT_TWIN_RESPONSE_TYPE_DESIRED_PROPERTIES; diff --git a/sdk/tests/iot/common/test_az_iot_common.c b/sdk/tests/iot/common/test_az_iot_common.c index bcec78de29..a9b88265a8 100644 --- a/sdk/tests/iot/common/test_az_iot_common.c +++ b/sdk/tests/iot/common/test_az_iot_common.c @@ -2,14 +2,15 @@ // SPDX-License-Identifier: MIT #include "test_az_iot_common.h" -#include -#include +#include +#include +#include #include #include -#include #include -#include -#include +#include +#include +#include #include #include @@ -18,6 +19,121 @@ #include +#define TEST_SPAN_BUFFER_SIZE 256 + +// Properties +#define TEST_KEY "key" +#define TEST_KEY_ONE "key_one" +#define TEST_KEY_TWO "key_two" +#define TEST_KEY_THREE "key_three" +#define TEST_VALUE_ONE "value_one" +#define TEST_VALUE_TWO "value_two" +#define TEST_VALUE_THREE "value_three" +#define TEST_KEY_VALUE_ONE "key_one=value_one" +#define TEST_KEY_VALUE_TWO "key_one=value_one&key_two=value_two" +#define TEST_KEY_VALUE_SUBSTRING "key_one=value_one&key=value_two" +#define TEST_KEY_VALUE_SAME "key_one=key&key=value_two" +#define TEST_KEY_VALUE_THREE "key_one=value_one&key_two=value_two&key_three=value_three" + +static const az_span test_key = AZ_SPAN_LITERAL_FROM_STR(TEST_KEY); +static const az_span test_key_one = AZ_SPAN_LITERAL_FROM_STR(TEST_KEY_ONE); +static const az_span test_key_two = AZ_SPAN_LITERAL_FROM_STR(TEST_KEY_TWO); +static const az_span test_key_three = AZ_SPAN_LITERAL_FROM_STR(TEST_KEY_THREE); +static const az_span test_value_one = AZ_SPAN_LITERAL_FROM_STR(TEST_VALUE_ONE); +static const az_span test_value_two = AZ_SPAN_LITERAL_FROM_STR(TEST_VALUE_TWO); +static const az_span test_value_three = AZ_SPAN_LITERAL_FROM_STR(TEST_VALUE_THREE); + +static const char test_correct_one_key_value[] = "key_one=value_one"; +static const char test_correct_two_key_value[] = "key_one=value_one&key_two=value_two"; + +#ifndef AZ_NO_PRECONDITION_CHECKING +ENABLE_PRECONDITION_CHECK_TESTS() + +static void test_az_iot_message_properties_init_NULL_props_fails(void** state) +{ + (void)state; + + uint8_t test_span_buf[TEST_SPAN_BUFFER_SIZE]; + az_span test_span = az_span_for_test_init(test_span_buf, _az_COUNTOF(test_span_buf)); + + ASSERT_PRECONDITION_CHECKED(az_iot_message_properties_init(NULL, test_span, 0)); +} + +static void test_az_iot_message_properties_append_get_NULL_props_fails(void** state) +{ + (void)state; + + ASSERT_PRECONDITION_CHECKED(az_iot_message_properties_append(NULL, test_key_one, test_value_one)); +} + +static void test_az_iot_message_properties_append_NULL_name_span_fails(void** state) +{ + (void)state; + + az_iot_message_properties props; + + ASSERT_PRECONDITION_CHECKED( + az_iot_message_properties_append(&props, AZ_SPAN_NULL, test_value_one)); +} + +static void test_az_iot_message_properties_append_NULL_value_span_fails(void** state) +{ + (void)state; + + az_iot_message_properties props; + + ASSERT_PRECONDITION_CHECKED(az_iot_message_properties_append(&props, test_key_one, AZ_SPAN_NULL)); +} + +static void test_az_iot_message_properties_find_NULL_props_fail(void** state) +{ + (void)state; + + az_span out_value; + + ASSERT_PRECONDITION_CHECKED(az_iot_message_properties_find(NULL, test_key_one, &out_value)); +} + +static void test_az_iot_message_properties_find_NULL_name_fail(void** state) +{ + (void)state; + + az_iot_message_properties props; + + az_span out_value; + + ASSERT_PRECONDITION_CHECKED(az_iot_message_properties_find(&props, AZ_SPAN_NULL, &out_value)); +} + +static void test_az_iot_message_properties_find_NULL_value_fail(void** state) +{ + (void)state; + + az_iot_message_properties props; + + ASSERT_PRECONDITION_CHECKED(az_iot_message_properties_find(&props, test_key_one, NULL)); +} + +static void test_az_iot_message_properties_next_NULL_props_fail(void** state) +{ + (void)state; + + az_pair pair_out; + + ASSERT_PRECONDITION_CHECKED(az_iot_message_properties_next(NULL, &pair_out)); +} + +static void test_az_iot_message_properties_next_NULL_out_fail(void** state) +{ + (void)state; + + az_iot_message_properties props; + + ASSERT_PRECONDITION_CHECKED(az_iot_message_properties_next(&props, NULL)); +} + +#endif // AZ_NO_PRECONDITION_CHECKING + static void test_az_iot_u32toa_size_success() { assert_int_equal(_az_iot_u32toa_size(0), 1); @@ -144,7 +260,8 @@ static void test_az_span_copy_url_encode_succeed() assert_int_equal(_az_span_copy_url_encode(url_encoded_span, url_decoded_span, &remaining), AZ_OK); assert_int_equal(az_span_size(remaining), 0); - assert_int_equal(az_span_size(url_encoded_span) - az_span_size(remaining), _az_COUNTOF(expected_result) - 1); + assert_int_equal( + az_span_size(url_encoded_span) - az_span_size(remaining), _az_COUNTOF(expected_result) - 1); } static void test_az_span_copy_url_encode_insufficient_size_fail() @@ -156,7 +273,397 @@ static void test_az_span_copy_url_encode_insufficient_size_fail() az_span remaining; - assert_int_equal(_az_span_copy_url_encode(url_encoded_span, url_decoded_span, &remaining), AZ_ERROR_INSUFFICIENT_SPAN_SIZE); + assert_int_equal( + _az_span_copy_url_encode(url_encoded_span, url_decoded_span, &remaining), + AZ_ERROR_INSUFFICIENT_SPAN_SIZE); +} + +static void test_az_iot_message_properties_init_succeed(void** state) +{ + (void)state; + + uint8_t test_span_buf[TEST_SPAN_BUFFER_SIZE] = { 0 }; + az_span test_span = az_span_create(test_span_buf, sizeof(test_span_buf)); + az_iot_message_properties props; + + assert_int_equal(az_iot_message_properties_init(&props, test_span, 0), AZ_OK); + assert_int_equal(props._internal.current_property_index, 0); +} + +static void test_az_iot_message_properties_init_user_set_params_succeed(void** state) +{ + (void)state; + + az_span test_span = az_span_create_from_str(TEST_KEY_VALUE_ONE); + az_iot_message_properties props; + + assert_int_equal( + az_iot_message_properties_init(&props, test_span, az_span_size(test_span)), AZ_OK); + + assert_memory_equal( + az_span_ptr(props._internal.properties_buffer), + test_correct_one_key_value, + sizeof(test_correct_one_key_value) - 1); +} + +static void test_az_iot_message_properties_append_succeed(void** state) +{ + (void)state; + + uint8_t test_span_buf[TEST_SPAN_BUFFER_SIZE]; + az_span test_span = az_span_for_test_init(test_span_buf, sizeof(test_span_buf)); + + az_iot_message_properties props; + assert_int_equal(az_iot_message_properties_init(&props, test_span, 0), AZ_OK); + + assert_int_equal(az_iot_message_properties_append(&props, test_key_one, test_value_one), AZ_OK); + az_span_for_test_verify( + az_span_slice(props._internal.properties_buffer, 0, props._internal.properties_written), + test_correct_one_key_value, + _az_COUNTOF(test_correct_one_key_value) - 1, + az_span_create(test_span_buf, _az_COUNTOF(test_span_buf)), + TEST_SPAN_BUFFER_SIZE); +} + +static void test_az_iot_message_properties_append_empty_buffer_fail(void** state) +{ + (void)state; + + az_iot_message_properties props; + assert_int_equal(az_iot_message_properties_init(&props, AZ_SPAN_NULL, 0), AZ_OK); + + assert_int_equal( + az_iot_message_properties_append(&props, test_key_one, test_value_one), + AZ_ERROR_INSUFFICIENT_SPAN_SIZE); +} + +static void test_az_iot_message_properties_append_small_buffer_fail(void** state) +{ + (void)state; + + uint8_t test_span_buf[sizeof(test_correct_one_key_value) - 2]; + az_span test_span = az_span_create(test_span_buf, sizeof(test_span_buf)); + az_iot_message_properties props; + + assert_int_equal(az_iot_message_properties_init(&props, test_span, 0), AZ_OK); + assert_int_equal( + az_iot_message_properties_append(&props, test_key_one, test_value_one), + AZ_ERROR_INSUFFICIENT_SPAN_SIZE); + assert_int_equal( + az_span_size(props._internal.properties_buffer), sizeof(test_correct_one_key_value) - 2); +} + +static void test_az_iot_message_properties_append_twice_succeed(void** state) +{ + (void)state; + + uint8_t test_span_buf[TEST_SPAN_BUFFER_SIZE]; + az_span test_span = az_span_for_test_init(test_span_buf, sizeof(test_span_buf)); + + az_iot_message_properties props; + assert_int_equal(az_iot_message_properties_init(&props, test_span, 0), AZ_OK); + + assert_int_equal(az_iot_message_properties_append(&props, test_key_one, test_value_one), AZ_OK); + assert_int_equal(az_iot_message_properties_append(&props, test_key_two, test_value_two), AZ_OK); + az_span_for_test_verify( + az_span_slice(props._internal.properties_buffer, 0, props._internal.properties_written), + test_correct_two_key_value, + _az_COUNTOF(test_correct_two_key_value) - 1, + az_span_create(test_span_buf, _az_COUNTOF(test_span_buf)), + TEST_SPAN_BUFFER_SIZE); +} + +static void test_az_iot_message_properties_append_twice_small_buffer_fail(void** state) +{ + (void)state; + + uint8_t test_span_buf[sizeof(test_correct_two_key_value) - 2]; + az_span test_span = az_span_create(test_span_buf, sizeof(test_span_buf)); + az_iot_message_properties props; + + assert_int_equal(az_iot_message_properties_init(&props, test_span, 0), AZ_OK); + assert_int_equal(az_iot_message_properties_append(&props, test_key_one, test_value_one), AZ_OK); + assert_int_equal( + az_iot_message_properties_append(&props, test_key_two, test_value_two), + AZ_ERROR_INSUFFICIENT_SPAN_SIZE); + assert_int_equal(props._internal.properties_written, sizeof(test_correct_one_key_value) - 1); + assert_int_equal( + az_span_size(props._internal.properties_buffer), sizeof(test_correct_two_key_value) - 2); +} + +static void test_az_iot_message_properties_find_succeed(void** state) +{ + (void)state; + + az_span test_span = az_span_create_from_str(TEST_KEY_VALUE_ONE); + az_iot_message_properties props; + + assert_int_equal( + az_iot_message_properties_init(&props, test_span, az_span_size(test_span)), AZ_OK); + + az_span out_value; + assert_int_equal(az_iot_message_properties_find(&props, test_key_one, &out_value), AZ_OK); + assert_memory_equal( + az_span_ptr(out_value), az_span_ptr(test_value_one), (size_t)az_span_size(test_value_one)); +} + +static void test_az_iot_message_properties_find_middle_succeed(void** state) +{ + (void)state; + + az_span test_span = az_span_create_from_str(TEST_KEY_VALUE_THREE); + az_iot_message_properties props; + + assert_int_equal( + az_iot_message_properties_init(&props, test_span, az_span_size(test_span)), AZ_OK); + + az_span out_value; + assert_int_equal(az_iot_message_properties_find(&props, test_key_two, &out_value), AZ_OK); + assert_memory_equal( + az_span_ptr(out_value), az_span_ptr(test_value_two), (size_t)az_span_size(test_value_two)); +} + +static void test_az_iot_message_properties_find_end_succeed(void** state) +{ + (void)state; + + az_span test_span = az_span_create_from_str(TEST_KEY_VALUE_TWO); + az_iot_message_properties props; + + assert_int_equal( + az_iot_message_properties_init(&props, test_span, az_span_size(test_span)), AZ_OK); + + az_span out_value; + assert_int_equal(az_iot_message_properties_find(&props, test_key_two, &out_value), AZ_OK); + assert_memory_equal( + az_span_ptr(out_value), az_span_ptr(test_value_two), (size_t)az_span_size(test_value_two)); +} + +static void test_az_iot_message_properties_find_substring_succeed(void** state) +{ + (void)state; + + az_span test_span = az_span_create_from_str(TEST_KEY_VALUE_SUBSTRING); + az_iot_message_properties props; + + assert_int_equal( + az_iot_message_properties_init(&props, test_span, az_span_size(test_span)), AZ_OK); + + az_span out_value; + assert_int_equal(az_iot_message_properties_find(&props, test_key, &out_value), AZ_OK); + assert_memory_equal( + az_span_ptr(out_value), az_span_ptr(test_value_two), (size_t)az_span_size(test_value_two)); +} + +static void test_az_iot_message_properties_find_name_value_same_succeed(void** state) +{ + (void)state; + + az_span test_span = az_span_create_from_str(TEST_KEY_VALUE_SAME); + az_iot_message_properties props; + + assert_int_equal( + az_iot_message_properties_init(&props, test_span, az_span_size(test_span)), AZ_OK); + + az_span out_value; + assert_int_equal(az_iot_message_properties_find(&props, test_key, &out_value), AZ_OK); + assert_memory_equal( + az_span_ptr(out_value), az_span_ptr(test_value_two), (size_t)az_span_size(test_value_two)); +} + +static void test_az_iot_message_properties_find_empty_buffer_fail(void** state) +{ + (void)state; + + az_iot_message_properties props; + + assert_int_equal(az_iot_message_properties_init(&props, AZ_SPAN_NULL, 0), AZ_OK); + + az_span out_value; + assert_int_equal( + az_iot_message_properties_find(&props, test_key_one, &out_value), AZ_ERROR_ITEM_NOT_FOUND); +} + +static void test_az_iot_message_properties_find_fail(void** state) +{ + (void)state; + + az_span test_span = az_span_create_from_str(TEST_KEY_VALUE_ONE); + az_iot_message_properties props; + + assert_int_equal( + az_iot_message_properties_init(&props, test_span, az_span_size(test_span)), AZ_OK); + + az_span out_value; + assert_int_equal( + az_iot_message_properties_find(&props, AZ_SPAN_FROM_STR("key_foo"), &out_value), + AZ_ERROR_ITEM_NOT_FOUND); +} + +static void test_az_iot_message_properties_find_substring_fail(void** state) +{ + (void)state; + + az_span test_span = az_span_create_from_str(TEST_KEY_VALUE_TWO); + az_iot_message_properties props; + + assert_int_equal( + az_iot_message_properties_init(&props, test_span, az_span_size(test_span)), AZ_OK); + + az_span out_value; + assert_int_equal( + az_iot_message_properties_find(&props, AZ_SPAN_FROM_STR("key"), &out_value), + AZ_ERROR_ITEM_NOT_FOUND); +} + +static void test_az_iot_message_properties_find_substring_suffix_fail(void** state) +{ + (void)state; + + az_span test_span = az_span_create_from_str(TEST_KEY_VALUE_TWO); + az_iot_message_properties props; + + assert_int_equal( + az_iot_message_properties_init(&props, test_span, az_span_size(test_span)), AZ_OK); + + az_span out_value; + assert_int_equal( + az_iot_message_properties_find(&props, AZ_SPAN_FROM_STR("one"), &out_value), + AZ_ERROR_ITEM_NOT_FOUND); +} + +static void test_az_iot_message_properties_find_value_match_fail(void** state) +{ + (void)state; + + az_span test_span = az_span_create_from_str(TEST_KEY_VALUE_THREE); + az_iot_message_properties props; + + assert_int_equal( + az_iot_message_properties_init(&props, test_span, az_span_size(test_span)), AZ_OK); + + az_span out_value; + assert_int_equal( + az_iot_message_properties_find(&props, AZ_SPAN_FROM_STR("value_two"), &out_value), + AZ_ERROR_ITEM_NOT_FOUND); +} + +static void test_az_iot_message_properties_find_value_match_end_fail(void** state) +{ + (void)state; + + az_span test_span = az_span_create_from_str(TEST_KEY_VALUE_THREE); + az_iot_message_properties props; + + assert_int_equal( + az_iot_message_properties_init(&props, test_span, az_span_size(test_span)), AZ_OK); + + az_span out_value; + assert_int_equal( + az_iot_message_properties_find(&props, AZ_SPAN_FROM_STR("value_three"), &out_value), + AZ_ERROR_ITEM_NOT_FOUND); +} + +static void test_az_iot_message_properties_next_succeed(void** state) +{ + (void)state; + + az_span test_span = az_span_create_from_str(TEST_KEY_VALUE_THREE); + az_iot_message_properties props; + assert_int_equal( + az_iot_message_properties_init(&props, test_span, az_span_size(test_span)), AZ_OK); + + az_pair pair_out; + + assert_int_equal(az_iot_message_properties_next(&props, &pair_out), AZ_OK); + assert_memory_equal( + az_span_ptr(pair_out.key), az_span_ptr(test_key_one), (size_t)az_span_size(test_key_one)); + assert_memory_equal( + az_span_ptr(pair_out.value), + az_span_ptr(test_value_one), + (size_t)az_span_size(test_value_one)); + + assert_int_equal(az_iot_message_properties_next(&props, &pair_out), AZ_OK); + assert_memory_equal( + az_span_ptr(pair_out.key), az_span_ptr(test_key_two), (size_t)az_span_size(test_key_two)); + assert_memory_equal( + az_span_ptr(pair_out.value), + az_span_ptr(test_value_two), + (size_t)az_span_size(test_value_two)); + + assert_int_equal(az_iot_message_properties_next(&props, &pair_out), AZ_OK); + assert_memory_equal( + az_span_ptr(pair_out.key), az_span_ptr(test_key_three), (size_t)az_span_size(test_key_three)); + assert_memory_equal( + az_span_ptr(pair_out.value), + az_span_ptr(test_value_three), + (size_t)az_span_size(test_value_three)); + + assert_int_equal(az_iot_message_properties_next(&props, &pair_out), AZ_ERROR_UNEXPECTED_END); +} + +static void test_az_iot_message_properties_next_twice_succeed(void** state) +{ + (void)state; + + az_span test_span = az_span_create_from_str(TEST_KEY_VALUE_TWO); + az_iot_message_properties props; + assert_int_equal( + az_iot_message_properties_init(&props, test_span, az_span_size(test_span)), AZ_OK); + + az_pair pair_out; + + assert_int_equal(az_iot_message_properties_next(&props, &pair_out), AZ_OK); + assert_memory_equal( + az_span_ptr(pair_out.key), az_span_ptr(test_key_one), (size_t)az_span_size(test_key_one)); + assert_memory_equal( + az_span_ptr(pair_out.value), + az_span_ptr(test_value_one), + (size_t)az_span_size(test_value_one)); + + assert_int_equal(az_iot_message_properties_next(&props, &pair_out), AZ_OK); + assert_memory_equal( + az_span_ptr(pair_out.key), az_span_ptr(test_key_two), (size_t)az_span_size(test_key_two)); + assert_memory_equal( + az_span_ptr(pair_out.value), + az_span_ptr(test_value_two), + (size_t)az_span_size(test_value_two)); + + assert_int_equal(az_iot_message_properties_next(&props, &pair_out), AZ_ERROR_UNEXPECTED_END); + + // Reset to beginning of span + assert_int_equal( + az_iot_message_properties_init(&props, test_span, az_span_size(test_span)), AZ_OK); + + assert_int_equal(az_iot_message_properties_next(&props, &pair_out), AZ_OK); + assert_memory_equal( + az_span_ptr(pair_out.key), az_span_ptr(test_key_one), (size_t)az_span_size(test_key_one)); + assert_memory_equal( + az_span_ptr(pair_out.value), + az_span_ptr(test_value_one), + (size_t)az_span_size(test_value_one)); + + assert_int_equal(az_iot_message_properties_next(&props, &pair_out), AZ_OK); + assert_memory_equal( + az_span_ptr(pair_out.key), az_span_ptr(test_key_two), (size_t)az_span_size(test_key_two)); + assert_memory_equal( + az_span_ptr(pair_out.value), + az_span_ptr(test_value_two), + (size_t)az_span_size(test_value_two)); + + assert_int_equal(az_iot_message_properties_next(&props, &pair_out), AZ_ERROR_UNEXPECTED_END); +} + +static void test_az_iot_message_properties_next_empty_succeed(void** state) +{ + (void)state; + + az_iot_message_properties props; + assert_int_equal(az_iot_message_properties_init(&props, AZ_SPAN_NULL, 0), AZ_OK); + + az_pair pair_out; + + assert_int_equal(az_iot_message_properties_next(&props, &pair_out), AZ_ERROR_UNEXPECTED_END); } #ifdef _MSC_VER @@ -166,7 +673,22 @@ static void test_az_span_copy_url_encode_insufficient_size_fail() int test_az_iot_common() { +#ifndef AZ_NO_PRECONDITION_CHECKING + SETUP_PRECONDITION_CHECK_TESTS(); +#endif // AZ_NO_PRECONDITION_CHECKING + const struct CMUnitTest tests[] = { +#ifndef AZ_NO_PRECONDITION_CHECKING + cmocka_unit_test(test_az_iot_message_properties_init_NULL_props_fails), + cmocka_unit_test(test_az_iot_message_properties_append_get_NULL_props_fails), + cmocka_unit_test(test_az_iot_message_properties_append_NULL_name_span_fails), + cmocka_unit_test(test_az_iot_message_properties_append_NULL_value_span_fails), + cmocka_unit_test(test_az_iot_message_properties_find_NULL_props_fail), + cmocka_unit_test(test_az_iot_message_properties_find_NULL_name_fail), + cmocka_unit_test(test_az_iot_message_properties_find_NULL_value_fail), + cmocka_unit_test(test_az_iot_message_properties_next_NULL_props_fail), + cmocka_unit_test(test_az_iot_message_properties_next_NULL_out_fail), +#endif // AZ_NO_PRECONDITION_CHECKING cmocka_unit_test(test_az_iot_u32toa_size_success), cmocka_unit_test(test_az_iot_u64toa_size_success), cmocka_unit_test(test_az_iot_is_success_status_translate_success), @@ -176,6 +698,27 @@ int test_az_iot_common() cmocka_unit_test(test_az_iot_provisioning_client_logging_succeed), cmocka_unit_test(test_az_span_copy_url_encode_succeed), cmocka_unit_test(test_az_span_copy_url_encode_insufficient_size_fail), + cmocka_unit_test(test_az_iot_message_properties_init_succeed), + cmocka_unit_test(test_az_iot_message_properties_init_user_set_params_succeed), + cmocka_unit_test(test_az_iot_message_properties_append_succeed), + cmocka_unit_test(test_az_iot_message_properties_append_empty_buffer_fail), + cmocka_unit_test(test_az_iot_message_properties_append_small_buffer_fail), + cmocka_unit_test(test_az_iot_message_properties_append_twice_succeed), + cmocka_unit_test(test_az_iot_message_properties_append_twice_small_buffer_fail), + cmocka_unit_test(test_az_iot_message_properties_find_succeed), + cmocka_unit_test(test_az_iot_message_properties_find_middle_succeed), + cmocka_unit_test(test_az_iot_message_properties_find_end_succeed), + cmocka_unit_test(test_az_iot_message_properties_find_substring_succeed), + cmocka_unit_test(test_az_iot_message_properties_find_name_value_same_succeed), + cmocka_unit_test(test_az_iot_message_properties_find_empty_buffer_fail), + cmocka_unit_test(test_az_iot_message_properties_find_fail), + cmocka_unit_test(test_az_iot_message_properties_find_substring_fail), + cmocka_unit_test(test_az_iot_message_properties_find_substring_suffix_fail), + cmocka_unit_test(test_az_iot_message_properties_find_value_match_fail), + cmocka_unit_test(test_az_iot_message_properties_find_value_match_end_fail), + cmocka_unit_test(test_az_iot_message_properties_next_succeed), + cmocka_unit_test(test_az_iot_message_properties_next_twice_succeed), + cmocka_unit_test(test_az_iot_message_properties_next_empty_succeed), }; return cmocka_run_group_tests_name("az_iot_common", tests, NULL, NULL); } diff --git a/sdk/tests/iot/hub/test_az_iot_hub_client.c b/sdk/tests/iot/hub/test_az_iot_hub_client.c index 402c9a3f6a..c93e0e91f3 100644 --- a/sdk/tests/iot/hub/test_az_iot_hub_client.c +++ b/sdk/tests/iot/hub/test_az_iot_hub_client.c @@ -45,31 +45,6 @@ static const char test_correct_user_name_with_module_id[] static const char test_correct_client_id[] = "my_device"; static const char test_correct_client_id_with_module_id[] = "my_device/my_module_id"; -// Properties -#define TEST_KEY "key" -#define TEST_KEY_ONE "key_one" -#define TEST_KEY_TWO "key_two" -#define TEST_KEY_THREE "key_three" -#define TEST_VALUE_ONE "value_one" -#define TEST_VALUE_TWO "value_two" -#define TEST_VALUE_THREE "value_three" -#define TEST_KEY_VALUE_ONE "key_one=value_one" -#define TEST_KEY_VALUE_TWO "key_one=value_one&key_two=value_two" -#define TEST_KEY_VALUE_SUBSTRING "key_one=value_one&key=value_two" -#define TEST_KEY_VALUE_SAME "key_one=key&key=value_two" -#define TEST_KEY_VALUE_THREE "key_one=value_one&key_two=value_two&key_three=value_three" - -static const az_span test_key = AZ_SPAN_LITERAL_FROM_STR(TEST_KEY); -static const az_span test_key_one = AZ_SPAN_LITERAL_FROM_STR(TEST_KEY_ONE); -static const az_span test_key_two = AZ_SPAN_LITERAL_FROM_STR(TEST_KEY_TWO); -static const az_span test_key_three = AZ_SPAN_LITERAL_FROM_STR(TEST_KEY_THREE); -static const az_span test_value_one = AZ_SPAN_LITERAL_FROM_STR(TEST_VALUE_ONE); -static const az_span test_value_two = AZ_SPAN_LITERAL_FROM_STR(TEST_VALUE_TWO); -static const az_span test_value_three = AZ_SPAN_LITERAL_FROM_STR(TEST_VALUE_THREE); - -static const char test_correct_one_key_value[] = "key_one=value_one"; -static const char test_correct_two_key_value[] = "key_one=value_one&key_two=value_two"; - #ifndef AZ_NO_PRECONDITION_CHECKING ENABLE_PRECONDITION_CHECK_TESTS() @@ -170,91 +145,6 @@ static void test_az_iot_hub_client_get_client_id_NULL_output_span_fails(void** s ASSERT_PRECONDITION_CHECKED(az_iot_hub_client_get_client_id(&client, test_buf, 0, &test_length)); } -static void test_az_iot_hub_client_properties_init_NULL_props_fails(void** state) -{ - (void)state; - - uint8_t test_span_buf[TEST_SPAN_BUFFER_SIZE]; - az_span test_span = az_span_for_test_init(test_span_buf, _az_COUNTOF(test_span_buf)); - - ASSERT_PRECONDITION_CHECKED(az_iot_hub_client_properties_init(NULL, test_span, 0)); -} - -static void test_az_iot_hub_client_properties_append_get_NULL_props_fails(void** state) -{ - (void)state; - - ASSERT_PRECONDITION_CHECKED( - az_iot_hub_client_properties_append(NULL, test_key_one, test_value_one)); -} - -static void test_az_iot_hub_client_properties_append_NULL_name_span_fails(void** state) -{ - (void)state; - - az_iot_hub_client_properties props; - - ASSERT_PRECONDITION_CHECKED( - az_iot_hub_client_properties_append(&props, AZ_SPAN_NULL, test_value_one)); -} - -static void test_az_iot_hub_client_properties_append_NULL_value_span_fails(void** state) -{ - (void)state; - - az_iot_hub_client_properties props; - - ASSERT_PRECONDITION_CHECKED( - az_iot_hub_client_properties_append(&props, test_key_one, AZ_SPAN_NULL)); -} - -static void test_az_iot_hub_client_properties_find_NULL_props_fail(void** state) -{ - (void)state; - - az_span out_value; - - ASSERT_PRECONDITION_CHECKED(az_iot_hub_client_properties_find(NULL, test_key_one, &out_value)); -} - -static void test_az_iot_hub_client_properties_find_NULL_name_fail(void** state) -{ - (void)state; - - az_iot_hub_client_properties props; - - az_span out_value; - - ASSERT_PRECONDITION_CHECKED(az_iot_hub_client_properties_find(&props, AZ_SPAN_NULL, &out_value)); -} - -static void test_az_iot_hub_client_properties_find_NULL_value_fail(void** state) -{ - (void)state; - - az_iot_hub_client_properties props; - - ASSERT_PRECONDITION_CHECKED(az_iot_hub_client_properties_find(&props, test_key_one, NULL)); -} - -static void test_az_iot_hub_client_properties_next_NULL_props_fail(void** state) -{ - (void)state; - - az_pair pair_out; - - ASSERT_PRECONDITION_CHECKED(az_iot_hub_client_properties_next(NULL, &pair_out)); -} - -static void test_az_iot_hub_client_properties_next_NULL_out_fail(void** state) -{ - (void)state; - - az_iot_hub_client_properties props; - - ASSERT_PRECONDITION_CHECKED(az_iot_hub_client_properties_next(&props, NULL)); -} - #endif // AZ_NO_PRECONDITION_CHECKING static void test_az_iot_hub_client_get_default_options_succeed(void** state) @@ -573,398 +463,6 @@ static void test_az_iot_hub_client_get_client_id_module_small_buffer_fail(void** AZ_ERROR_INSUFFICIENT_SPAN_SIZE); } -static void test_az_iot_hub_client_properties_init_succeed(void** state) -{ - (void)state; - - uint8_t test_span_buf[TEST_SPAN_BUFFER_SIZE] = { 0 }; - az_span test_span = az_span_create(test_span_buf, sizeof(test_span_buf)); - az_iot_hub_client_properties props; - - assert_int_equal(az_iot_hub_client_properties_init(&props, test_span, 0), AZ_OK); - assert_int_equal(props._internal.current_property_index, 0); -} - -static void test_az_iot_hub_client_properties_init_user_set_params_succeed(void** state) -{ - (void)state; - - az_span test_span = az_span_create_from_str(TEST_KEY_VALUE_ONE); - az_iot_hub_client_properties props; - - assert_int_equal( - az_iot_hub_client_properties_init(&props, test_span, az_span_size(test_span)), AZ_OK); - - assert_memory_equal( - az_span_ptr(props._internal.properties_buffer), - test_correct_one_key_value, - sizeof(test_correct_one_key_value) - 1); -} - -static void test_az_iot_hub_client_properties_append_succeed(void** state) -{ - (void)state; - - uint8_t test_span_buf[TEST_SPAN_BUFFER_SIZE]; - az_span test_span = az_span_for_test_init(test_span_buf, sizeof(test_span_buf)); - - az_iot_hub_client_properties props; - assert_int_equal(az_iot_hub_client_properties_init(&props, test_span, 0), AZ_OK); - - assert_int_equal( - az_iot_hub_client_properties_append(&props, test_key_one, test_value_one), AZ_OK); - az_span_for_test_verify( - az_span_slice(props._internal.properties_buffer, 0, props._internal.properties_written), - test_correct_one_key_value, - _az_COUNTOF(test_correct_one_key_value) - 1, - az_span_create(test_span_buf, _az_COUNTOF(test_span_buf)), - TEST_SPAN_BUFFER_SIZE); -} - -static void test_az_iot_hub_client_properties_append_empty_buffer_fail(void** state) -{ - (void)state; - - az_iot_hub_client_properties props; - assert_int_equal(az_iot_hub_client_properties_init(&props, AZ_SPAN_NULL, 0), AZ_OK); - - assert_int_equal( - az_iot_hub_client_properties_append(&props, test_key_one, test_value_one), - AZ_ERROR_INSUFFICIENT_SPAN_SIZE); -} - -static void test_az_iot_hub_client_properties_append_small_buffer_fail(void** state) -{ - (void)state; - - uint8_t test_span_buf[sizeof(test_correct_one_key_value) - 2]; - az_span test_span = az_span_create(test_span_buf, sizeof(test_span_buf)); - az_iot_hub_client_properties props; - - assert_int_equal(az_iot_hub_client_properties_init(&props, test_span, 0), AZ_OK); - assert_int_equal( - az_iot_hub_client_properties_append(&props, test_key_one, test_value_one), - AZ_ERROR_INSUFFICIENT_SPAN_SIZE); - assert_int_equal( - az_span_size(props._internal.properties_buffer), sizeof(test_correct_one_key_value) - 2); -} - -static void test_az_iot_hub_client_properties_append_twice_succeed(void** state) -{ - (void)state; - - uint8_t test_span_buf[TEST_SPAN_BUFFER_SIZE]; - az_span test_span = az_span_for_test_init(test_span_buf, sizeof(test_span_buf)); - - az_iot_hub_client_properties props; - assert_int_equal(az_iot_hub_client_properties_init(&props, test_span, 0), AZ_OK); - - assert_int_equal( - az_iot_hub_client_properties_append(&props, test_key_one, test_value_one), AZ_OK); - assert_int_equal( - az_iot_hub_client_properties_append(&props, test_key_two, test_value_two), AZ_OK); - az_span_for_test_verify( - az_span_slice(props._internal.properties_buffer, 0, props._internal.properties_written), - test_correct_two_key_value, - _az_COUNTOF(test_correct_two_key_value) - 1, - az_span_create(test_span_buf, _az_COUNTOF(test_span_buf)), - TEST_SPAN_BUFFER_SIZE); -} - -static void test_az_iot_hub_client_properties_append_twice_small_buffer_fail(void** state) -{ - (void)state; - - uint8_t test_span_buf[sizeof(test_correct_two_key_value) - 2]; - az_span test_span = az_span_create(test_span_buf, sizeof(test_span_buf)); - az_iot_hub_client_properties props; - - assert_int_equal(az_iot_hub_client_properties_init(&props, test_span, 0), AZ_OK); - assert_int_equal( - az_iot_hub_client_properties_append(&props, test_key_one, test_value_one), AZ_OK); - assert_int_equal( - az_iot_hub_client_properties_append(&props, test_key_two, test_value_two), - AZ_ERROR_INSUFFICIENT_SPAN_SIZE); - assert_int_equal(props._internal.properties_written, sizeof(test_correct_one_key_value) - 1); - assert_int_equal( - az_span_size(props._internal.properties_buffer), sizeof(test_correct_two_key_value) - 2); -} - -static void test_az_iot_hub_client_properties_find_succeed(void** state) -{ - (void)state; - - az_span test_span = az_span_create_from_str(TEST_KEY_VALUE_ONE); - az_iot_hub_client_properties props; - - assert_int_equal( - az_iot_hub_client_properties_init(&props, test_span, az_span_size(test_span)), AZ_OK); - - az_span out_value; - assert_int_equal(az_iot_hub_client_properties_find(&props, test_key_one, &out_value), AZ_OK); - assert_memory_equal( - az_span_ptr(out_value), az_span_ptr(test_value_one), (size_t)az_span_size(test_value_one)); -} - -static void test_az_iot_hub_client_properties_find_middle_succeed(void** state) -{ - (void)state; - - az_span test_span = az_span_create_from_str(TEST_KEY_VALUE_THREE); - az_iot_hub_client_properties props; - - assert_int_equal( - az_iot_hub_client_properties_init(&props, test_span, az_span_size(test_span)), AZ_OK); - - az_span out_value; - assert_int_equal(az_iot_hub_client_properties_find(&props, test_key_two, &out_value), AZ_OK); - assert_memory_equal( - az_span_ptr(out_value), az_span_ptr(test_value_two), (size_t)az_span_size(test_value_two)); -} - -static void test_az_iot_hub_client_properties_find_end_succeed(void** state) -{ - (void)state; - - az_span test_span = az_span_create_from_str(TEST_KEY_VALUE_TWO); - az_iot_hub_client_properties props; - - assert_int_equal( - az_iot_hub_client_properties_init(&props, test_span, az_span_size(test_span)), AZ_OK); - - az_span out_value; - assert_int_equal(az_iot_hub_client_properties_find(&props, test_key_two, &out_value), AZ_OK); - assert_memory_equal( - az_span_ptr(out_value), az_span_ptr(test_value_two), (size_t)az_span_size(test_value_two)); -} - -static void test_az_iot_hub_client_properties_find_substring_succeed(void** state) -{ - (void)state; - - az_span test_span = az_span_create_from_str(TEST_KEY_VALUE_SUBSTRING); - az_iot_hub_client_properties props; - - assert_int_equal( - az_iot_hub_client_properties_init(&props, test_span, az_span_size(test_span)), AZ_OK); - - az_span out_value; - assert_int_equal(az_iot_hub_client_properties_find(&props, test_key, &out_value), AZ_OK); - assert_memory_equal( - az_span_ptr(out_value), az_span_ptr(test_value_two), (size_t)az_span_size(test_value_two)); -} - -static void test_az_iot_hub_client_properties_find_name_value_same_succeed(void** state) -{ - (void)state; - - az_span test_span = az_span_create_from_str(TEST_KEY_VALUE_SAME); - az_iot_hub_client_properties props; - - assert_int_equal( - az_iot_hub_client_properties_init(&props, test_span, az_span_size(test_span)), AZ_OK); - - az_span out_value; - assert_int_equal(az_iot_hub_client_properties_find(&props, test_key, &out_value), AZ_OK); - assert_memory_equal( - az_span_ptr(out_value), az_span_ptr(test_value_two), (size_t)az_span_size(test_value_two)); -} - -static void test_az_iot_hub_client_properties_find_empty_buffer_fail(void** state) -{ - (void)state; - - az_iot_hub_client_properties props; - - assert_int_equal(az_iot_hub_client_properties_init(&props, AZ_SPAN_NULL, 0), AZ_OK); - - az_span out_value; - assert_int_equal( - az_iot_hub_client_properties_find(&props, test_key_one, &out_value), AZ_ERROR_ITEM_NOT_FOUND); -} - -static void test_az_iot_hub_client_properties_find_fail(void** state) -{ - (void)state; - - az_span test_span = az_span_create_from_str(TEST_KEY_VALUE_ONE); - az_iot_hub_client_properties props; - - assert_int_equal( - az_iot_hub_client_properties_init(&props, test_span, az_span_size(test_span)), AZ_OK); - - az_span out_value; - assert_int_equal( - az_iot_hub_client_properties_find(&props, AZ_SPAN_FROM_STR("key_foo"), &out_value), - AZ_ERROR_ITEM_NOT_FOUND); -} - -static void test_az_iot_hub_client_properties_find_substring_fail(void** state) -{ - (void)state; - - az_span test_span = az_span_create_from_str(TEST_KEY_VALUE_TWO); - az_iot_hub_client_properties props; - - assert_int_equal( - az_iot_hub_client_properties_init(&props, test_span, az_span_size(test_span)), AZ_OK); - - az_span out_value; - assert_int_equal( - az_iot_hub_client_properties_find(&props, AZ_SPAN_FROM_STR("key"), &out_value), - AZ_ERROR_ITEM_NOT_FOUND); -} - -static void test_az_iot_hub_client_properties_find_substring_suffix_fail(void** state) -{ - (void)state; - - az_span test_span = az_span_create_from_str(TEST_KEY_VALUE_TWO); - az_iot_hub_client_properties props; - - assert_int_equal( - az_iot_hub_client_properties_init(&props, test_span, az_span_size(test_span)), AZ_OK); - - az_span out_value; - assert_int_equal( - az_iot_hub_client_properties_find(&props, AZ_SPAN_FROM_STR("one"), &out_value), - AZ_ERROR_ITEM_NOT_FOUND); -} - -static void test_az_iot_hub_client_properties_find_value_match_fail(void** state) -{ - (void)state; - - az_span test_span = az_span_create_from_str(TEST_KEY_VALUE_THREE); - az_iot_hub_client_properties props; - - assert_int_equal( - az_iot_hub_client_properties_init(&props, test_span, az_span_size(test_span)), AZ_OK); - - az_span out_value; - assert_int_equal( - az_iot_hub_client_properties_find(&props, AZ_SPAN_FROM_STR("value_two"), &out_value), - AZ_ERROR_ITEM_NOT_FOUND); -} - -static void test_az_iot_hub_client_properties_find_value_match_end_fail(void** state) -{ - (void)state; - - az_span test_span = az_span_create_from_str(TEST_KEY_VALUE_THREE); - az_iot_hub_client_properties props; - - assert_int_equal( - az_iot_hub_client_properties_init(&props, test_span, az_span_size(test_span)), AZ_OK); - - az_span out_value; - assert_int_equal( - az_iot_hub_client_properties_find(&props, AZ_SPAN_FROM_STR("value_three"), &out_value), - AZ_ERROR_ITEM_NOT_FOUND); -} - -static void test_az_iot_hub_client_properties_next_succeed(void** state) -{ - (void)state; - - az_span test_span = az_span_create_from_str(TEST_KEY_VALUE_THREE); - az_iot_hub_client_properties props; - assert_int_equal( - az_iot_hub_client_properties_init(&props, test_span, az_span_size(test_span)), AZ_OK); - - az_pair pair_out; - - assert_int_equal(az_iot_hub_client_properties_next(&props, &pair_out), AZ_OK); - assert_memory_equal( - az_span_ptr(pair_out.key), az_span_ptr(test_key_one), (size_t)az_span_size(test_key_one)); - assert_memory_equal( - az_span_ptr(pair_out.value), - az_span_ptr(test_value_one), - (size_t)az_span_size(test_value_one)); - - assert_int_equal(az_iot_hub_client_properties_next(&props, &pair_out), AZ_OK); - assert_memory_equal( - az_span_ptr(pair_out.key), az_span_ptr(test_key_two), (size_t)az_span_size(test_key_two)); - assert_memory_equal( - az_span_ptr(pair_out.value), - az_span_ptr(test_value_two), - (size_t)az_span_size(test_value_two)); - - assert_int_equal(az_iot_hub_client_properties_next(&props, &pair_out), AZ_OK); - assert_memory_equal( - az_span_ptr(pair_out.key), az_span_ptr(test_key_three), (size_t)az_span_size(test_key_three)); - assert_memory_equal( - az_span_ptr(pair_out.value), - az_span_ptr(test_value_three), - (size_t)az_span_size(test_value_three)); - - assert_int_equal(az_iot_hub_client_properties_next(&props, &pair_out), AZ_ERROR_UNEXPECTED_END); -} - -static void test_az_iot_hub_client_properties_next_twice_succeed(void** state) -{ - (void)state; - - az_span test_span = az_span_create_from_str(TEST_KEY_VALUE_TWO); - az_iot_hub_client_properties props; - assert_int_equal( - az_iot_hub_client_properties_init(&props, test_span, az_span_size(test_span)), AZ_OK); - - az_pair pair_out; - - assert_int_equal(az_iot_hub_client_properties_next(&props, &pair_out), AZ_OK); - assert_memory_equal( - az_span_ptr(pair_out.key), az_span_ptr(test_key_one), (size_t)az_span_size(test_key_one)); - assert_memory_equal( - az_span_ptr(pair_out.value), - az_span_ptr(test_value_one), - (size_t)az_span_size(test_value_one)); - - assert_int_equal(az_iot_hub_client_properties_next(&props, &pair_out), AZ_OK); - assert_memory_equal( - az_span_ptr(pair_out.key), az_span_ptr(test_key_two), (size_t)az_span_size(test_key_two)); - assert_memory_equal( - az_span_ptr(pair_out.value), - az_span_ptr(test_value_two), - (size_t)az_span_size(test_value_two)); - - assert_int_equal(az_iot_hub_client_properties_next(&props, &pair_out), AZ_ERROR_UNEXPECTED_END); - - // Reset to beginning of span - assert_int_equal( - az_iot_hub_client_properties_init(&props, test_span, az_span_size(test_span)), AZ_OK); - - assert_int_equal(az_iot_hub_client_properties_next(&props, &pair_out), AZ_OK); - assert_memory_equal( - az_span_ptr(pair_out.key), az_span_ptr(test_key_one), (size_t)az_span_size(test_key_one)); - assert_memory_equal( - az_span_ptr(pair_out.value), - az_span_ptr(test_value_one), - (size_t)az_span_size(test_value_one)); - - assert_int_equal(az_iot_hub_client_properties_next(&props, &pair_out), AZ_OK); - assert_memory_equal( - az_span_ptr(pair_out.key), az_span_ptr(test_key_two), (size_t)az_span_size(test_key_two)); - assert_memory_equal( - az_span_ptr(pair_out.value), - az_span_ptr(test_value_two), - (size_t)az_span_size(test_value_two)); - - assert_int_equal(az_iot_hub_client_properties_next(&props, &pair_out), AZ_ERROR_UNEXPECTED_END); -} - -static void test_az_iot_hub_client_properties_next_empty_succeed(void** state) -{ - (void)state; - - az_iot_hub_client_properties props; - assert_int_equal(az_iot_hub_client_properties_init(&props, AZ_SPAN_NULL, 0), AZ_OK); - - az_pair pair_out; - - assert_int_equal(az_iot_hub_client_properties_next(&props, &pair_out), AZ_ERROR_UNEXPECTED_END); -} - int test_az_iot_hub_client() { #ifndef AZ_NO_PRECONDITION_CHECKING @@ -982,15 +480,6 @@ int test_az_iot_hub_client() cmocka_unit_test(test_az_iot_hub_client_get_client_id_NULL_client_fails), cmocka_unit_test(test_az_iot_hub_client_get_client_id_NULL_input_span_fails), cmocka_unit_test(test_az_iot_hub_client_get_client_id_NULL_output_span_fails), - cmocka_unit_test(test_az_iot_hub_client_properties_init_NULL_props_fails), - cmocka_unit_test(test_az_iot_hub_client_properties_append_get_NULL_props_fails), - cmocka_unit_test(test_az_iot_hub_client_properties_append_NULL_name_span_fails), - cmocka_unit_test(test_az_iot_hub_client_properties_append_NULL_value_span_fails), - cmocka_unit_test(test_az_iot_hub_client_properties_find_NULL_props_fail), - cmocka_unit_test(test_az_iot_hub_client_properties_find_NULL_name_fail), - cmocka_unit_test(test_az_iot_hub_client_properties_find_NULL_value_fail), - cmocka_unit_test(test_az_iot_hub_client_properties_next_NULL_props_fail), - cmocka_unit_test(test_az_iot_hub_client_properties_next_NULL_out_fail), #endif // AZ_NO_PRECONDITION_CHECKING cmocka_unit_test(test_az_iot_hub_client_get_default_options_succeed), cmocka_unit_test(test_az_iot_hub_client_init_succeed), @@ -1010,27 +499,6 @@ int test_az_iot_hub_client() cmocka_unit_test(test_az_iot_hub_client_get_client_id_small_buffer_fail), cmocka_unit_test(test_az_iot_hub_client_get_client_id_module_succeed), cmocka_unit_test(test_az_iot_hub_client_get_client_id_module_small_buffer_fail), - cmocka_unit_test(test_az_iot_hub_client_properties_init_succeed), - cmocka_unit_test(test_az_iot_hub_client_properties_init_user_set_params_succeed), - cmocka_unit_test(test_az_iot_hub_client_properties_append_succeed), - cmocka_unit_test(test_az_iot_hub_client_properties_append_empty_buffer_fail), - cmocka_unit_test(test_az_iot_hub_client_properties_append_small_buffer_fail), - cmocka_unit_test(test_az_iot_hub_client_properties_append_twice_succeed), - cmocka_unit_test(test_az_iot_hub_client_properties_append_twice_small_buffer_fail), - cmocka_unit_test(test_az_iot_hub_client_properties_find_succeed), - cmocka_unit_test(test_az_iot_hub_client_properties_find_middle_succeed), - cmocka_unit_test(test_az_iot_hub_client_properties_find_end_succeed), - cmocka_unit_test(test_az_iot_hub_client_properties_find_substring_succeed), - cmocka_unit_test(test_az_iot_hub_client_properties_find_name_value_same_succeed), - cmocka_unit_test(test_az_iot_hub_client_properties_find_empty_buffer_fail), - cmocka_unit_test(test_az_iot_hub_client_properties_find_fail), - cmocka_unit_test(test_az_iot_hub_client_properties_find_substring_fail), - cmocka_unit_test(test_az_iot_hub_client_properties_find_substring_suffix_fail), - cmocka_unit_test(test_az_iot_hub_client_properties_find_value_match_fail), - cmocka_unit_test(test_az_iot_hub_client_properties_find_value_match_end_fail), - cmocka_unit_test(test_az_iot_hub_client_properties_next_succeed), - cmocka_unit_test(test_az_iot_hub_client_properties_next_twice_succeed), - cmocka_unit_test(test_az_iot_hub_client_properties_next_empty_succeed), }; return cmocka_run_group_tests_name("az_iot_hub_client", tests, NULL, NULL); } diff --git a/sdk/tests/iot/hub/test_az_iot_hub_client_c2d.c b/sdk/tests/iot/hub/test_az_iot_hub_client_c2d.c index d7c8178134..28065e89b2 100644 --- a/sdk/tests/iot/hub/test_az_iot_hub_client_c2d.c +++ b/sdk/tests/iot/hub/test_az_iot_hub_client_c2d.c @@ -101,17 +101,17 @@ static void test_az_iot_hub_client_c2d_parse_received_topic_url_decoded_succeed( az_iot_hub_client_c2d_parse_received_topic(&client, received_topic, &out_request), AZ_OK); az_pair pair; - assert_int_equal(az_iot_hub_client_properties_next(&out_request.properties, &pair), AZ_OK); + assert_int_equal(az_iot_message_properties_next(&out_request.properties, &pair), AZ_OK); assert_true(az_span_is_content_equal(pair.key, AZ_SPAN_FROM_STR("$.mid"))); assert_true(az_span_is_content_equal( pair.value, AZ_SPAN_FROM_STR("79eadb01-bd0d-472d-bd35-ccb76e70eab8"))); - assert_int_equal(az_iot_hub_client_properties_next(&out_request.properties, &pair), AZ_OK); + assert_int_equal(az_iot_message_properties_next(&out_request.properties, &pair), AZ_OK); assert_true(az_span_is_content_equal(pair.key, AZ_SPAN_FROM_STR("$.to"))); assert_true(az_span_is_content_equal( pair.value, AZ_SPAN_FROM_STR("/devices/useragent_c/messages/deviceBound"))); - assert_int_equal(az_iot_hub_client_properties_next(&out_request.properties, &pair), AZ_OK); + assert_int_equal(az_iot_message_properties_next(&out_request.properties, &pair), AZ_OK); assert_true(az_span_is_content_equal(pair.key, AZ_SPAN_FROM_STR("abc"))); assert_true(az_span_is_content_equal(pair.value, AZ_SPAN_FROM_STR("123"))); } @@ -131,20 +131,20 @@ static void test_az_iot_hub_client_c2d_parse_received_topic_url_encoded_succeed( az_iot_hub_client_c2d_parse_received_topic(&client, received_topic, &out_request), AZ_OK); az_pair pair; - assert_int_equal(az_iot_hub_client_properties_next(&out_request.properties, &pair), AZ_OK); + assert_int_equal(az_iot_message_properties_next(&out_request.properties, &pair), AZ_OK); assert_true(az_span_is_content_equal(pair.key, AZ_SPAN_FROM_STR("%24.to"))); assert_true(az_span_is_content_equal( pair.value, AZ_SPAN_FROM_STR("%2Fdevices%2Fuseragent_c%2Fmessages%2FdeviceBound"))); - assert_int_equal(az_iot_hub_client_properties_next(&out_request.properties, &pair), AZ_OK); + assert_int_equal(az_iot_message_properties_next(&out_request.properties, &pair), AZ_OK); assert_true(az_span_is_content_equal(pair.key, AZ_SPAN_FROM_STR("abc"))); assert_true(az_span_is_content_equal(pair.value, AZ_SPAN_FROM_STR("123"))); - assert_int_equal(az_iot_hub_client_properties_next(&out_request.properties, &pair), AZ_OK); + assert_int_equal(az_iot_message_properties_next(&out_request.properties, &pair), AZ_OK); assert_true(az_span_is_content_equal(pair.key, AZ_SPAN_FROM_STR("ghi"))); assert_true(az_span_is_content_equal(pair.value, AZ_SPAN_FROM_STR("%2Fsome%2Fthing"))); - assert_int_equal(az_iot_hub_client_properties_next(&out_request.properties, &pair), AZ_OK); + assert_int_equal(az_iot_message_properties_next(&out_request.properties, &pair), AZ_OK); assert_true(az_span_is_content_equal(pair.key, AZ_SPAN_FROM_STR("jkl"))); assert_true( az_span_is_content_equal(pair.value, AZ_SPAN_FROM_STR("%2Fsome%2Fthing%2F%3Fbla%3Dbla"))); diff --git a/sdk/tests/iot/hub/test_az_iot_hub_client_telemetry.c b/sdk/tests/iot/hub/test_az_iot_hub_client_telemetry.c index 8c2c811700..1593ed0b65 100644 --- a/sdk/tests/iot/hub/test_az_iot_hub_client_telemetry.c +++ b/sdk/tests/iot/hub/test_az_iot_hub_client_telemetry.c @@ -137,9 +137,9 @@ static void test_az_iot_hub_client_telemetry_get_publish_topic_with_options_with assert_int_equal( az_iot_hub_client_init(&client, test_device_hostname, test_device_id, &options), AZ_OK); - az_iot_hub_client_properties props; + az_iot_message_properties props; assert_int_equal( - az_iot_hub_client_properties_init(&props, test_props, az_span_size(test_props)), AZ_OK); + az_iot_message_properties_init(&props, test_props, az_span_size(test_props)), AZ_OK); char test_buf[TEST_SPAN_BUFFER_SIZE]; size_t test_length; @@ -163,18 +163,18 @@ static void test_az_iot_hub_client_telemetry_get_publish_topic_with_props_unfill az_iot_hub_client_init(&client, test_device_hostname, test_device_id, NULL), AZ_OK); // Create unfilled property span - az_iot_hub_client_properties props; + az_iot_message_properties props; uint8_t test_prop_unfilled_buf[TEST_SPAN_BUFFER_SIZE]; memset(test_prop_unfilled_buf, 0xFF, sizeof(test_prop_unfilled_buf)); az_span test_prop_unfilled_span = az_span_create(test_prop_unfilled_buf, sizeof(test_prop_unfilled_buf)); - assert_int_equal(az_iot_hub_client_properties_init(&props, test_prop_unfilled_span, 0), AZ_OK); + assert_int_equal(az_iot_message_properties_init(&props, test_prop_unfilled_span, 0), AZ_OK); assert_int_equal( - az_iot_hub_client_properties_append( + az_iot_message_properties_append( &props, AZ_SPAN_FROM_STR("key"), AZ_SPAN_FROM_STR("value")), AZ_OK); assert_int_equal( - az_iot_hub_client_properties_append( + az_iot_message_properties_append( &props, AZ_SPAN_FROM_STR("key_two"), AZ_SPAN_FROM_STR("value2")), AZ_OK); @@ -203,9 +203,9 @@ test_az_iot_hub_client_telemetry_get_publish_topic_with_options_with_props_small assert_int_equal( az_iot_hub_client_init(&client, test_device_hostname, test_device_id, &options), AZ_OK); - az_iot_hub_client_properties props; + az_iot_message_properties props; assert_int_equal( - az_iot_hub_client_properties_init(&props, test_props, az_span_size(test_props)), AZ_OK); + az_iot_message_properties_init(&props, test_props, az_span_size(test_props)), AZ_OK); char test_buf[sizeof(g_test_correct_topic_with_options_with_props) - 2]; size_t test_length; @@ -225,9 +225,9 @@ static void test_az_iot_hub_client_telemetry_get_publish_topic_no_options_with_p assert_int_equal( az_iot_hub_client_init(&client, test_device_hostname, test_device_id, NULL), AZ_OK); - az_iot_hub_client_properties props; + az_iot_message_properties props; assert_int_equal( - az_iot_hub_client_properties_init(&props, test_props, az_span_size(test_props)), AZ_OK); + az_iot_message_properties_init(&props, test_props, az_span_size(test_props)), AZ_OK); char test_buf[TEST_SPAN_BUFFER_SIZE]; size_t test_length; @@ -251,9 +251,9 @@ test_az_iot_hub_client_telemetry_get_publish_topic_no_options_with_props_small_b assert_int_equal( az_iot_hub_client_init(&client, test_device_hostname, test_device_id, NULL), AZ_OK); - az_iot_hub_client_properties props; + az_iot_message_properties props; assert_int_equal( - az_iot_hub_client_properties_init(&props, test_props, az_span_size(test_props)), AZ_OK); + az_iot_message_properties_init(&props, test_props, az_span_size(test_props)), AZ_OK); char test_buf[sizeof(g_test_correct_topic_no_options_with_props) - 2]; size_t test_length; @@ -277,9 +277,9 @@ test_az_iot_hub_client_telemetry_get_publish_topic_with_options_module_id_with_p assert_int_equal( az_iot_hub_client_init(&client, test_device_hostname, test_device_id, &options), AZ_OK); - az_iot_hub_client_properties props; + az_iot_message_properties props; assert_int_equal( - az_iot_hub_client_properties_init(&props, test_props, az_span_size(test_props)), AZ_OK); + az_iot_message_properties_init(&props, test_props, az_span_size(test_props)), AZ_OK); char test_buf[TEST_SPAN_BUFFER_SIZE]; size_t test_length; @@ -306,9 +306,9 @@ test_az_iot_hub_client_telemetry_get_publish_topic_with_options_module_id_with_p assert_int_equal( az_iot_hub_client_init(&client, test_device_hostname, test_device_id, &options), AZ_OK); - az_iot_hub_client_properties props; + az_iot_message_properties props; assert_int_equal( - az_iot_hub_client_properties_init(&props, test_props, az_span_size(test_props)), AZ_OK); + az_iot_message_properties_init(&props, test_props, az_span_size(test_props)), AZ_OK); char test_buf[sizeof(g_test_correct_topic_with_options_module_id_with_props) - 2]; size_t test_length;