From 9a5d6a85484d06024dc18c4778ba733266a634ee Mon Sep 17 00:00:00 2001 From: TwistedTwigleg Date: Tue, 3 May 2022 15:16:13 -0400 Subject: [PATCH] Device Defender changes (#67) Adjusts the code to allow custom metrics for Device Defender in the C++ V2 SDK. Commit log: * Modified device_defender to free the string lists from memory * minor clang-format fix * A really minor format issue I missed when manually fixing clang-format issue * Added support for double numbers and double lists to Device Defender * Fixed clang format issues and removed no longer needed header import * Adjusted for numbers to always be doubles, removed unnecessary code, removed int returns on registering custom metrics * Fixed crash with empty custom metric arrays * Added some const back to functions to fix tests * Fixed clang-format issues and adjusted tests to use doubles instead of int64_t * Removed useless asserts in Device Defender tests * Fixed a few missed DeviceDefender tests * With all the fixes, I forgot a newline. Fixed * Code review adjustments: added removed consts back and changed increment back to prefix style * Slight modification after merge to use doubles with DeviceDefender again * Fixed metrics not sending correctly * Fixed clang-format issues, hopefully fixed memory leak in tests * Further clang-format fixes and memory leak fixes in test * Test to confirm the failure is related to the JSON module change * Memory leak narrowing in: Testing to make sure cleanup is not leaking memory * Memory leak narrowing: Added header back to test * Fixed memory leak issue in test * clang-format fix * Test heap overflow issue - remove validation to see if that is causing problem * Test heap overflow issue - comment out unused function * Test heap overflow issue - make byte cursor from payload * Test heap overflow issue - add back header validation and JSON parsing * Test heap overflow issue - just create JSON report and free it * Test heap overflow issue - send byte cursor directly * Test heap overflow issue - test adding JSON validation back to normal metric test * Test heap overflow issue - fix custom metric test and remove debug comments * Test heap overflow issue - fix compile issue --- include/aws/iotdevice/device_defender.h | 32 ++--- source/device_defender.c | 150 +++++++-------------- tests/aws_iot_devicedefender_client_test.c | 26 ++-- tests/metrics_tests.c | 96 ++++++------- 4 files changed, 123 insertions(+), 181 deletions(-) diff --git a/include/aws/iotdevice/device_defender.h b/include/aws/iotdevice/device_defender.h index 059b460a..3843832c 100644 --- a/include/aws/iotdevice/device_defender.h +++ b/include/aws/iotdevice/device_defender.h @@ -74,17 +74,17 @@ typedef void( * * returns: AWS_OP_SUCCESS if the custom metric was successfully added to the task config */ -typedef int(aws_iotdevice_defender_get_number_fn)(int64_t *const value, void *userdata); +typedef int(aws_iotdevice_defender_get_number_fn)(double *value, void *userdata); /** * User callback type invoked to retrieve a number list custom metric * * List provided will already be initialized and caller must push items into the list - * of type int64_t. + * of type double. * * returns: AWS_OP_SUCCESS if the custom metric was successfully added to the task config */ -typedef int(aws_iotdevice_defender_get_number_list_fn)(struct aws_array_list *const number_list, void *userdata); +typedef int(aws_iotdevice_defender_get_number_list_fn)(struct aws_array_list *number_list, void *userdata); /** * User callback type invoked to retrieve a string list custom metric @@ -95,7 +95,7 @@ typedef int(aws_iotdevice_defender_get_number_list_fn)(struct aws_array_list *co * * returns: AWS_OP_SUCCESS if the custom metric was successfully added to the task config */ -typedef int(aws_iotdevice_defender_get_string_list_fn)(struct aws_array_list *const string_list, void *userdata); +typedef int(aws_iotdevice_defender_get_string_list_fn)(struct aws_array_list *string_list, void *userdata); /** * User callback type invoked to retrieve an ip list custom metric @@ -106,7 +106,7 @@ typedef int(aws_iotdevice_defender_get_string_list_fn)(struct aws_array_list *co * * returns: AWS_OP_SUCCESS if the custom metric was successfully added to the task config */ -typedef int(aws_iotdevice_defender_get_ip_list_fn)(struct aws_array_list *const ip_list, void *userdata); +typedef int(aws_iotdevice_defender_get_ip_list_fn)(struct aws_array_list *ip_list, void *userdata); enum aws_iotdevice_defender_report_format { AWS_IDDRF_JSON, AWS_IDDRF_SHORT_JSON, AWS_IDDRF_CBOR }; @@ -116,10 +116,10 @@ enum aws_iotdevice_defender_report_format { AWS_IDDRF_JSON, AWS_IDDRF_SHORT_JSON */ enum defender_custom_metric_type { DD_METRIC_UNKNOWN, - DD_METRIC_NUMBER, /* int64_t */ - DD_METRIC_NUMBER_LIST, /* aws_array_list: int64_t */ + DD_METRIC_NUMBER, /* double */ + DD_METRIC_NUMBER_LIST, /* aws_array_list: double */ DD_METRIC_STRING_LIST, /* aws_array_list: struct aws_string */ - DD_METRIC_IP_LIST /* aws_array_list: struct aws_string */ + DD_METRIC_IP_LIST, /* aws_array_list: struct aws_string */ }; struct aws_iotdevice_defender_task; @@ -259,11 +259,9 @@ int aws_iotdevice_defender_config_set_callback_userdata( * \param[in] supplier callback function to produce the metric value when * requested at report generation time * \param[in] userdata specific callback data for the supplier callback function - * \returns AWS_OP_SUCCESS if the custom metric has been associated with the - * task configuration successfully */ AWS_IOTDEVICE_API -int aws_iotdevice_defender_config_register_number_metric( +void aws_iotdevice_defender_config_register_number_metric( struct aws_iotdevice_defender_task_config *task_config, const struct aws_byte_cursor *metric_name, aws_iotdevice_defender_get_number_fn *supplier, @@ -278,11 +276,9 @@ int aws_iotdevice_defender_config_register_number_metric( * \param[in] supplier callback function to produce the metric value when * requested at report generation time * \param[in] userdata specific callback data for the supplier callback function - * \returns AWS_OP_SUCCESS if the custom metric has been associated with the - * task configuration successfully */ AWS_IOTDEVICE_API -int aws_iotdevice_defender_config_register_number_list_metric( +void aws_iotdevice_defender_config_register_number_list_metric( struct aws_iotdevice_defender_task_config *task_config, const struct aws_byte_cursor *metric_name, aws_iotdevice_defender_get_number_list_fn *supplier, @@ -297,11 +293,9 @@ int aws_iotdevice_defender_config_register_number_list_metric( * \param[in] supplier callback function to produce the metric value when * requested at report generation time * \param[in] userdata specific callback data for the supplier callback function - * \returns AWS_OP_SUCCESS if the custom metric has been associated with the - * task configuration successfully */ AWS_IOTDEVICE_API -int aws_iotdevice_defender_config_register_string_list_metric( +void aws_iotdevice_defender_config_register_string_list_metric( struct aws_iotdevice_defender_task_config *task_config, const struct aws_byte_cursor *metric_name, aws_iotdevice_defender_get_string_list_fn *supplier, @@ -316,11 +310,9 @@ int aws_iotdevice_defender_config_register_string_list_metric( * \param[in] supplier callback function to produce the metric value when * requested at report generation time * \param[in] userdata specific callback data for the supplier callback function - * \returns AWS_OP_SUCCESS if the custom metric has been associated with the - * task configuration successfully */ AWS_IOTDEVICE_API -int aws_iotdevice_defender_config_register_ip_list_metric( +void aws_iotdevice_defender_config_register_ip_list_metric( struct aws_iotdevice_defender_task_config *task_config, const struct aws_byte_cursor *metric_name, aws_iotdevice_defender_get_ip_list_fn *supplier, diff --git a/source/device_defender.c b/source/device_defender.c index 74161e09..05aa2e17 100644 --- a/source/device_defender.c +++ b/source/device_defender.c @@ -58,12 +58,12 @@ struct defender_custom_metric { * Result of a custom metric's data collection callback function that needs to be * populated into a report * - * Data union only needs to physically point to a single number, and single list. + * Data union only needs to physically point to a single number and a single list. */ struct defender_custom_metric_data { struct defender_custom_metric *metric; union { - int64_t number; + double number; struct aws_array_list list; } data; int callback_result; @@ -300,7 +300,7 @@ static int s_get_metric_report_json( } struct aws_json_value *header = aws_json_value_new_object(allocator); - if (aws_json_value_add_to_object(root, aws_byte_cursor_from_c_str("header"), header) == false) { + if (aws_json_value_add_to_object(root, aws_byte_cursor_from_c_str("header"), header) == AWS_OP_ERR) { goto cleanup; } if (aws_json_value_add_to_object( @@ -347,18 +347,19 @@ static int s_get_metric_report_json( struct aws_json_value *est_connections = aws_json_value_new_array(allocator); if (aws_json_value_add_to_object( - established_tcp_conns, aws_byte_cursor_from_c_str("connections"), est_connections) == false) { + established_tcp_conns, aws_byte_cursor_from_c_str("connections"), est_connections) == AWS_OP_ERR) { goto cleanup; } struct aws_json_value *listening_udp_ports = aws_json_value_new_object(allocator); if (aws_json_value_add_to_object(metrics, aws_byte_cursor_from_c_str("listening_udp_ports"), listening_udp_ports) == - false) { + AWS_OP_ERR) { goto cleanup; } struct aws_json_value *udp_ports = aws_json_value_new_array(allocator); - if (aws_json_value_add_to_object(listening_udp_ports, aws_byte_cursor_from_c_str("ports"), udp_ports) == false) { + if (aws_json_value_add_to_object(listening_udp_ports, aws_byte_cursor_from_c_str("ports"), udp_ports) == + AWS_OP_ERR) { goto cleanup; } @@ -382,9 +383,11 @@ static int s_get_metric_report_json( conn, aws_byte_cursor_from_c_str("local_interface"), aws_json_value_new_string( - allocator, aws_byte_cursor_from_c_str((char *)net_conn->local_interface))) == AWS_OP_ERR) { + allocator, aws_byte_cursor_from_c_str(aws_string_c_str(net_conn->local_interface)))) == + AWS_OP_ERR) { goto cleanup; } + if (aws_json_value_add_to_object( conn, aws_byte_cursor_from_c_str("local_port"), @@ -483,7 +486,8 @@ static int s_get_metric_report_json( if (custom_metrics_len != 0) { struct aws_json_value *custom_metrics = aws_json_value_new_object(allocator); - if (aws_json_value_add_to_object(root, aws_byte_cursor_from_c_str("custom_metrics"), custom_metrics) == false) { + if (aws_json_value_add_to_object(root, aws_byte_cursor_from_c_str("custom_metrics"), custom_metrics) == + AWS_OP_ERR) { goto cleanup; } @@ -516,7 +520,7 @@ static int s_get_metric_report_json( if (aws_json_value_add_to_object( item, aws_byte_cursor_from_c_str("number"), - aws_json_value_new_number(allocator, (double)custom_metrics_data[metric_index].data.number)) == + aws_json_value_new_number(allocator, custom_metrics_data[metric_index].data.number)) == AWS_OP_ERR) { goto cleanup; } @@ -528,9 +532,9 @@ static int s_get_metric_report_json( goto cleanup; } for (size_t num_index = 0; num_index < list_size; ++num_index) { - int64_t number = 0; + double number = 0; aws_array_list_get_at(&custom_metrics_data[metric_index].data.list, &number, num_index); - json_value = aws_json_value_new_number(allocator, (double)number); + json_value = aws_json_value_new_number(allocator, number); aws_json_value_add_array_element(json_list, json_value); } } else if ( @@ -545,7 +549,8 @@ static int s_get_metric_report_json( } } else { json_list = aws_json_value_new_array(allocator); - if (aws_json_value_add_to_object(item, aws_byte_cursor_from_c_str("ip_list"), json_list) == false) { + if (aws_json_value_add_to_object(item, aws_byte_cursor_from_c_str("ip_list"), json_list) == + AWS_OP_ERR) { goto cleanup; } } @@ -576,9 +581,6 @@ static int s_get_metric_report_json( return_value = AWS_OP_ERR; } else { return_value = AWS_OP_SUCCESS; - - struct aws_string *tmp = aws_string_new_from_cursor(allocator, &json_report_cursor); - aws_string_destroy_secure(tmp); } aws_byte_buf_clean_up_secure(&json_report_buf); @@ -629,7 +631,7 @@ static int s_init_custom_metric_data( break; case DD_METRIC_NUMBER_LIST: aws_array_list_init_dynamic( - &custom_metric_data[metric_index].data.list, allocator, 0, sizeof(int64_t)); + &custom_metric_data[metric_index].data.list, allocator, 0, sizeof(double)); break; case DD_METRIC_STRING_LIST: /* fall through */ @@ -677,7 +679,9 @@ static void s_clean_up_custom_metric_data( &custom_metrics_data[metric_index].data.list, (void *)&string_or_ip_entry, item_index); aws_string_destroy(string_or_ip_entry); } - /* fall through */ + aws_array_list_clean_up( + &custom_metrics_data[metric_index].data.list); // Destory the list when finished + break; case DD_METRIC_NUMBER_LIST: aws_array_list_clean_up(&custom_metrics_data[metric_index].data.list); break; @@ -1263,7 +1267,7 @@ void aws_iotdevice_defender_task_clean_up(struct aws_iotdevice_defender_task *de s_defender_task_clean_up(defender_task); } -int aws_iotdevice_defender_config_register_number_metric( +void aws_iotdevice_defender_config_register_number_metric( struct aws_iotdevice_defender_task_config *task_config, const struct aws_byte_cursor *metric_name, aws_iotdevice_defender_get_number_fn *supplier, @@ -1274,30 +1278,16 @@ int aws_iotdevice_defender_config_register_number_metric( struct aws_allocator *allocator = task_config->allocator; struct defender_custom_metric *custom_metric = aws_mem_calloc(allocator, 1, sizeof(struct defender_custom_metric)); - if (custom_metric) { - custom_metric->type = DD_METRIC_NUMBER; - custom_metric->metric_name = aws_string_new_from_cursor(allocator, metric_name); - custom_metric->supplier_fn.get_number_fn = supplier; - custom_metric->metric_cb_userdata = userdata; + custom_metric->type = DD_METRIC_NUMBER; + custom_metric->metric_name = aws_string_new_from_cursor(allocator, metric_name); + custom_metric->supplier_fn.get_number_fn = supplier; + custom_metric->metric_cb_userdata = userdata; - if (AWS_OP_SUCCESS != aws_array_list_push_back(&task_config->custom_metrics, &custom_metric)) { - AWS_LOGF_ERROR( - AWS_LS_IOTDEVICE_DEFENDER_TASK, - "id=%p: Failed to add number custom metric " PRInSTR, - (void *)task_config, - AWS_BYTE_CURSOR_PRI(*metric_name)); /* wrong subject */ - aws_string_destroy(custom_metric->metric_name); - aws_mem_release(allocator, custom_metric); - return AWS_OP_ERR; - } - - task_config->custom_metrics_len++; - return AWS_OP_SUCCESS; - } - return AWS_OP_ERR; + aws_array_list_push_back(&task_config->custom_metrics, &custom_metric); + task_config->custom_metrics_len++; } -int aws_iotdevice_defender_config_register_number_list_metric( +void aws_iotdevice_defender_config_register_number_list_metric( struct aws_iotdevice_defender_task_config *task_config, const struct aws_byte_cursor *metric_name, aws_iotdevice_defender_get_number_list_fn *supplier, @@ -1308,30 +1298,16 @@ int aws_iotdevice_defender_config_register_number_list_metric( struct aws_allocator *allocator = task_config->allocator; struct defender_custom_metric *custom_metric = aws_mem_calloc(allocator, 1, sizeof(struct defender_custom_metric)); - if (custom_metric) { - custom_metric->type = DD_METRIC_NUMBER_LIST; - custom_metric->metric_name = aws_string_new_from_cursor(allocator, metric_name); - custom_metric->supplier_fn.get_number_list_fn = supplier; - custom_metric->metric_cb_userdata = userdata; - - if (AWS_OP_SUCCESS != aws_array_list_push_back(&task_config->custom_metrics, &custom_metric)) { - AWS_LOGF_ERROR( - AWS_LS_IOTDEVICE_DEFENDER_TASK, - "id=%p: Failed to add number list custom metric " PRInSTR, - (void *)task_config, - AWS_BYTE_CURSOR_PRI(*metric_name)); /* wrong subject */ - aws_string_destroy(custom_metric->metric_name); - aws_mem_release(allocator, custom_metric); - return AWS_OP_ERR; - } + custom_metric->type = DD_METRIC_NUMBER_LIST; + custom_metric->metric_name = aws_string_new_from_cursor(allocator, metric_name); + custom_metric->supplier_fn.get_number_list_fn = supplier; + custom_metric->metric_cb_userdata = userdata; - task_config->custom_metrics_len++; - return AWS_OP_SUCCESS; - } - return AWS_OP_ERR; + aws_array_list_push_back(&task_config->custom_metrics, &custom_metric); + task_config->custom_metrics_len++; } -int aws_iotdevice_defender_config_register_string_list_metric( +void aws_iotdevice_defender_config_register_string_list_metric( struct aws_iotdevice_defender_task_config *task_config, const struct aws_byte_cursor *metric_name, aws_iotdevice_defender_get_string_list_fn *supplier, @@ -1342,30 +1318,16 @@ int aws_iotdevice_defender_config_register_string_list_metric( struct aws_allocator *allocator = task_config->allocator; struct defender_custom_metric *custom_metric = aws_mem_calloc(allocator, 1, sizeof(struct defender_custom_metric)); - if (custom_metric) { - custom_metric->type = DD_METRIC_STRING_LIST; - custom_metric->metric_name = aws_string_new_from_cursor(allocator, metric_name); - custom_metric->supplier_fn.get_string_list_fn = supplier; - custom_metric->metric_cb_userdata = userdata; - - if (AWS_OP_SUCCESS != aws_array_list_push_back(&task_config->custom_metrics, &custom_metric)) { - AWS_LOGF_ERROR( - AWS_LS_IOTDEVICE_DEFENDER_TASK, - "id=%p: Failed to add string list custom metric " PRInSTR, - (void *)task_config, - AWS_BYTE_CURSOR_PRI(*metric_name)); /* wrong subject */ - aws_string_destroy(custom_metric->metric_name); - aws_mem_release(allocator, custom_metric); - return AWS_OP_ERR; - } + custom_metric->type = DD_METRIC_STRING_LIST; + custom_metric->metric_name = aws_string_new_from_cursor(allocator, metric_name); + custom_metric->supplier_fn.get_string_list_fn = supplier; + custom_metric->metric_cb_userdata = userdata; - task_config->custom_metrics_len++; - return AWS_OP_SUCCESS; - } - return AWS_OP_ERR; + aws_array_list_push_back(&task_config->custom_metrics, &custom_metric); + task_config->custom_metrics_len++; } -int aws_iotdevice_defender_config_register_ip_list_metric( +void aws_iotdevice_defender_config_register_ip_list_metric( struct aws_iotdevice_defender_task_config *task_config, const struct aws_byte_cursor *metric_name, aws_iotdevice_defender_get_ip_list_fn *supplier, @@ -1376,27 +1338,13 @@ int aws_iotdevice_defender_config_register_ip_list_metric( struct aws_allocator *allocator = task_config->allocator; struct defender_custom_metric *custom_metric = aws_mem_calloc(allocator, 1, sizeof(struct defender_custom_metric)); - if (custom_metric) { - custom_metric->type = DD_METRIC_IP_LIST; - custom_metric->metric_name = aws_string_new_from_cursor(allocator, metric_name); - custom_metric->supplier_fn.get_string_list_fn = supplier; - custom_metric->metric_cb_userdata = userdata; - - if (AWS_OP_SUCCESS != aws_array_list_push_back(&task_config->custom_metrics, &custom_metric)) { - AWS_LOGF_ERROR( - AWS_LS_IOTDEVICE_DEFENDER_TASK, - "id=%p: Failed to add IP list custom metric " PRInSTR, - (void *)task_config, - AWS_BYTE_CURSOR_PRI(*metric_name)); /* wrong subject */ - aws_string_destroy(custom_metric->metric_name); - aws_mem_release(allocator, custom_metric); - return AWS_OP_ERR; - } + custom_metric->type = DD_METRIC_IP_LIST; + custom_metric->metric_name = aws_string_new_from_cursor(allocator, metric_name); + custom_metric->supplier_fn.get_string_list_fn = supplier; + custom_metric->metric_cb_userdata = userdata; - task_config->custom_metrics_len++; - return AWS_OP_SUCCESS; - } - return AWS_OP_ERR; + aws_array_list_push_back(&task_config->custom_metrics, &custom_metric); + task_config->custom_metrics_len++; } int aws_iotdevice_defender_config_set_task_cancelation_fn( diff --git a/tests/aws_iot_devicedefender_client_test.c b/tests/aws_iot_devicedefender_client_test.c index b5a6ba23..f9a8d0b4 100644 --- a/tests/aws_iot_devicedefender_client_test.c +++ b/tests/aws_iot_devicedefender_client_test.c @@ -134,7 +134,7 @@ static void s_mqtt_on_disconnect(struct aws_mqtt_client_connection *connection, aws_mutex_unlock(args->mutex); } -static int get_number_metric(int64_t *out, void *userdata) { +static int get_number_metric(double *out, void *userdata) { (void)userdata; *out = 42; return AWS_OP_SUCCESS; @@ -142,7 +142,7 @@ static int get_number_metric(int64_t *out, void *userdata) { static int get_number_list_metric(struct aws_array_list *to_write_list, void *userdata) { (void)userdata; - int64_t number = 64; + double number = 64; aws_array_list_push_back(to_write_list, &number); number = 128; aws_array_list_push_back(to_write_list, &number); @@ -302,21 +302,19 @@ int main(int argc, char **argv) { ASSERT_SUCCESS(aws_iotdevice_defender_config_set_report_rejected_fn(task_config, s_report_rejected)); ASSERT_SUCCESS(aws_iotdevice_defender_config_set_task_failure_fn(task_config, s_task_failure)); - const struct aws_byte_cursor name_metric_number = aws_byte_cursor_from_c_str("TestCustomMetricNumber"); - ASSERT_SUCCESS(aws_iotdevice_defender_config_register_number_metric( - task_config, &name_metric_number, get_number_metric, &args)); + struct aws_byte_cursor name_metric_number = aws_byte_cursor_from_c_str("TestCustomMetricNumber"); + aws_iotdevice_defender_config_register_number_metric(task_config, &name_metric_number, get_number_metric, &args); - const struct aws_byte_cursor name_metric_number_list = aws_byte_cursor_from_c_str("TestCustomMetricNumberList"); - ASSERT_SUCCESS(aws_iotdevice_defender_config_register_number_list_metric( - task_config, &name_metric_number_list, get_number_list_metric, &args)); + struct aws_byte_cursor name_metric_number_list = aws_byte_cursor_from_c_str("TestCustomMetricNumberList"); + aws_iotdevice_defender_config_register_number_list_metric( + task_config, &name_metric_number_list, get_number_list_metric, &args); - const struct aws_byte_cursor name_metric_string_list = aws_byte_cursor_from_c_str("TestCustomMetricStringList"); - ASSERT_SUCCESS(aws_iotdevice_defender_config_register_string_list_metric( - task_config, &name_metric_string_list, get_string_list_metric, &args)); + struct aws_byte_cursor name_metric_string_list = aws_byte_cursor_from_c_str("TestCustomMetricStringList"); + aws_iotdevice_defender_config_register_string_list_metric( + task_config, &name_metric_string_list, get_string_list_metric, &args); - const struct aws_byte_cursor name_metric_ip_list = aws_byte_cursor_from_c_str("TestCustomMetricIpList"); - ASSERT_SUCCESS(aws_iotdevice_defender_config_register_ip_list_metric( - task_config, &name_metric_ip_list, get_ip_list_metric, &args)); + struct aws_byte_cursor name_metric_ip_list = aws_byte_cursor_from_c_str("TestCustomMetricIpList"); + aws_iotdevice_defender_config_register_ip_list_metric(task_config, &name_metric_ip_list, get_ip_list_metric, &args); struct aws_mqtt_connection_options conn_options = { .host_name = host_name_cur, diff --git a/tests/metrics_tests.c b/tests/metrics_tests.c index b04f975a..24c1c88b 100644 --- a/tests/metrics_tests.c +++ b/tests/metrics_tests.c @@ -55,8 +55,9 @@ struct mqtt_connection_test_data { static struct mqtt_connection_test_data mqtt_test_data = {0}; -static int validate_devicedefender_record(struct aws_allocator *allocator, const char *value) { - struct aws_json_value *report = aws_json_value_new_from_string(allocator, aws_byte_cursor_from_c_str(value)); +static int validate_devicedefender_record(struct aws_allocator *allocator, struct aws_byte_cursor *value) { + + struct aws_json_value *report = aws_json_value_new_from_string(allocator, *value); ASSERT_NOT_NULL(report); struct aws_json_value *header = aws_json_value_get_from_object(report, aws_byte_cursor_from_c_str("header")); @@ -70,8 +71,8 @@ static int validate_devicedefender_record(struct aws_allocator *allocator, const struct aws_byte_cursor version_cursor; ASSERT_INT_EQUALS(AWS_OP_SUCCESS, aws_json_value_get_string(version, &version_cursor)); ASSERT_TRUE(aws_byte_cursor_eq_c_str(&version_cursor, "1.0")); - struct aws_json_value *metrics = aws_json_value_get_from_object(report, aws_byte_cursor_from_c_str("metrics")); + struct aws_json_value *metrics = aws_json_value_get_from_object(report, aws_byte_cursor_from_c_str("metrics")); struct aws_json_value *tcpPorts = aws_json_value_get_from_object(metrics, aws_byte_cursor_from_c_str("listening_tcp_ports")); ASSERT_TRUE(aws_json_value_is_object(tcpPorts)); @@ -89,17 +90,21 @@ static int validate_devicedefender_record(struct aws_allocator *allocator, const struct aws_json_value *connections = aws_json_value_get_from_object(metrics, aws_byte_cursor_from_c_str("tcp_connections")); ASSERT_TRUE(aws_json_value_is_object(connections)); + struct aws_json_value *established = - aws_json_value_get_from_object(metrics, aws_byte_cursor_from_c_str("established_connections")); + aws_json_value_get_from_object(connections, aws_byte_cursor_from_c_str("established_connections")); ASSERT_TRUE(aws_json_value_is_object(established)); ASSERT_TRUE(aws_json_value_is_array( aws_json_value_get_from_object(established, aws_byte_cursor_from_c_str("connections")))); + // clean up + aws_json_value_destroy(report); + return AWS_OP_SUCCESS; } -const int64_t cm_number = 42; -const int64_t cm_number_list[] = {64, 128, 256}; +const double cm_number = 42; +const double cm_number_list[] = {64, 128, 256}; const char *cm_string_list[] = {"foo", "bar", "donkey"}; const char *cm_ip_list[] = { "127.0.0.1", @@ -109,11 +114,11 @@ const char *cm_ip_list[] = { }; #define dd_value_len 256 -static int validate_devicedefender_custom_record(struct aws_allocator *allocator, const char *json_report) { +static int validate_devicedefender_custom_record(struct aws_allocator *allocator, struct aws_byte_cursor *json_report) { struct aws_byte_buf value_to_cmp; aws_byte_buf_init(&value_to_cmp, allocator, 0); - struct aws_json_value *report = aws_json_value_new_from_string(allocator, aws_byte_cursor_from_c_str(json_report)); + struct aws_json_value *report = aws_json_value_new_from_string(allocator, *json_report); ASSERT_NOT_NULL(report); struct aws_json_value *custom_metrics = @@ -208,22 +213,24 @@ static int validate_devicedefender_custom_record(struct aws_allocator *allocator aws_json_value_get_from_object(custom_metrics, aws_byte_cursor_from_c_str("TestMetricIpListFail")); ASSERT_NULL(ip_list_metric_fail); + aws_json_value_destroy(report); + return AWS_OP_SUCCESS; } -static int get_number_metric_fail(int64_t *out, void *userdata) { +static int get_number_metric_fail(double *out, void *userdata) { (void)userdata; *out = cm_number; return AWS_OP_ERR; } -static int get_number_metric(int64_t *out, void *userdata) { +static int get_number_metric(double *out, void *userdata) { (void)userdata; *out = cm_number; return AWS_OP_SUCCESS; /* let the caller know we wrote the data successfully */ } -static int get_number_metric_slow(int64_t *out, void *userdata) { +static int get_number_metric_slow(double *out, void *userdata) { (void)userdata; *out = cm_number; /* 2 seconds */ @@ -233,7 +240,7 @@ static int get_number_metric_slow(int64_t *out, void *userdata) { static int get_number_list_metric_fail(struct aws_array_list *to_write_list, void *userdata) { (void)userdata; - int64_t number = cm_number_list[0]; + double number = cm_number_list[0]; aws_array_list_push_back(to_write_list, &number); number = cm_number_list[1]; aws_array_list_push_back(to_write_list, &number); @@ -245,7 +252,7 @@ static int get_number_list_metric_fail(struct aws_array_list *to_write_list, voi static int get_number_list_metric(struct aws_array_list *to_write_list, void *userdata) { (void)userdata; - int64_t number = cm_number_list[0]; + double number = cm_number_list[0]; aws_array_list_push_back(to_write_list, &number); number = cm_number_list[1]; aws_array_list_push_back(to_write_list, &number); @@ -524,7 +531,7 @@ static int s_devicedefender_success_test(struct aws_allocator *allocator, void * defender_task = NULL; struct aws_byte_cursor payload = aws_byte_cursor_from_buf(&state_test_data->payload); - validate_devicedefender_record(allocator, (const char *)payload.ptr); + validate_devicedefender_record(allocator, &payload); return AWS_OP_SUCCESS; } @@ -550,38 +557,36 @@ static int s_devicedefender_custom_metrics_success_test(struct aws_allocator *al aws_iotdevice_defender_config_set_task_period_ns(task_config, 1000000000UL); /* register working metrics */ - const struct aws_byte_cursor name_metric_number = aws_byte_cursor_from_c_str(TM_NUMBER); - ASSERT_SUCCESS( - aws_iotdevice_defender_config_register_number_metric(task_config, &name_metric_number, get_number_metric, ctx)); + struct aws_byte_cursor name_metric_number = aws_byte_cursor_from_c_str(TM_NUMBER); + aws_iotdevice_defender_config_register_number_metric(task_config, &name_metric_number, get_number_metric, ctx); - const struct aws_byte_cursor name_metric_number_list = aws_byte_cursor_from_c_str(TM_NUMBER_LIST); - ASSERT_SUCCESS(aws_iotdevice_defender_config_register_number_list_metric( - task_config, &name_metric_number_list, get_number_list_metric, ctx)); + struct aws_byte_cursor name_metric_number_list = aws_byte_cursor_from_c_str(TM_NUMBER_LIST); + aws_iotdevice_defender_config_register_number_list_metric( + task_config, &name_metric_number_list, get_number_list_metric, ctx); - const struct aws_byte_cursor name_metric_string_list = aws_byte_cursor_from_c_str(TM_STRING_LIST); - ASSERT_SUCCESS(aws_iotdevice_defender_config_register_string_list_metric( - task_config, &name_metric_string_list, get_string_list_metric, ctx)); + struct aws_byte_cursor name_metric_string_list = aws_byte_cursor_from_c_str(TM_STRING_LIST); + aws_iotdevice_defender_config_register_string_list_metric( + task_config, &name_metric_string_list, get_string_list_metric, ctx); - const struct aws_byte_cursor name_metric_ip_list = aws_byte_cursor_from_c_str(TM_IP_LIST); - ASSERT_SUCCESS(aws_iotdevice_defender_config_register_ip_list_metric( - task_config, &name_metric_ip_list, get_ip_list_metric, ctx)); + struct aws_byte_cursor name_metric_ip_list = aws_byte_cursor_from_c_str(TM_IP_LIST); + aws_iotdevice_defender_config_register_ip_list_metric(task_config, &name_metric_ip_list, get_ip_list_metric, ctx); /* register metrics with failing callbacks */ - const struct aws_byte_cursor name_metric_number_fail = aws_byte_cursor_from_c_str(TM_NUMBER_FAIL); - ASSERT_SUCCESS(aws_iotdevice_defender_config_register_number_metric( - task_config, &name_metric_number_fail, get_number_metric_fail, ctx)); + struct aws_byte_cursor name_metric_number_fail = aws_byte_cursor_from_c_str(TM_NUMBER_FAIL); + aws_iotdevice_defender_config_register_number_metric( + task_config, &name_metric_number_fail, get_number_metric_fail, ctx); - const struct aws_byte_cursor name_metric_number_list_fail = aws_byte_cursor_from_c_str(TM_NUMBER_LIST_FAIL); - ASSERT_SUCCESS(aws_iotdevice_defender_config_register_number_list_metric( - task_config, &name_metric_number_list_fail, get_number_list_metric_fail, ctx)); + struct aws_byte_cursor name_metric_number_list_fail = aws_byte_cursor_from_c_str(TM_NUMBER_LIST_FAIL); + aws_iotdevice_defender_config_register_number_list_metric( + task_config, &name_metric_number_list_fail, get_number_list_metric_fail, ctx); - const struct aws_byte_cursor name_metric_string_list_fail = aws_byte_cursor_from_c_str(TM_STRING_LIST_FAIL); - ASSERT_SUCCESS(aws_iotdevice_defender_config_register_string_list_metric( - task_config, &name_metric_string_list_fail, get_string_list_metric_fail, ctx)); + struct aws_byte_cursor name_metric_string_list_fail = aws_byte_cursor_from_c_str(TM_STRING_LIST_FAIL); + aws_iotdevice_defender_config_register_string_list_metric( + task_config, &name_metric_string_list_fail, get_string_list_metric_fail, ctx); - const struct aws_byte_cursor name_metric_ip_list_fail = aws_byte_cursor_from_c_str(TM_IP_LIST_FAIL); - ASSERT_SUCCESS(aws_iotdevice_defender_config_register_ip_list_metric( - task_config, &name_metric_ip_list_fail, get_ip_list_metric_fail, ctx)); + struct aws_byte_cursor name_metric_ip_list_fail = aws_byte_cursor_from_c_str(TM_IP_LIST_FAIL); + aws_iotdevice_defender_config_register_ip_list_metric( + task_config, &name_metric_ip_list_fail, get_ip_list_metric_fail, ctx); struct aws_iotdevice_defender_task *defender_task = NULL; ASSERT_SUCCESS(aws_iotdevice_defender_task_create_ex( @@ -599,7 +604,7 @@ static int s_devicedefender_custom_metrics_success_test(struct aws_allocator *al ASSERT_TRUE(state_test_data->task_stopped); struct aws_byte_cursor payload = aws_byte_cursor_from_buf(&state_test_data->payload); - validate_devicedefender_custom_record(allocator, (const char *)payload.ptr); + validate_devicedefender_custom_record(allocator, &payload); return AWS_OP_SUCCESS; } @@ -630,9 +635,8 @@ static int s_devicedefender_stop_while_running(struct aws_allocator *allocator, aws_iotdevice_defender_config_set_task_period_ns(task_config, 1000000000UL); /* use a slow metric getter to ensure cancel of the stop will wait */ - const struct aws_byte_cursor name_metric_number = aws_byte_cursor_from_c_str(TM_NUMBER); - ASSERT_SUCCESS(aws_iotdevice_defender_config_register_number_metric( - task_config, &name_metric_number, get_number_metric_slow, ctx)); + struct aws_byte_cursor name_metric_number = aws_byte_cursor_from_c_str(TM_NUMBER); + aws_iotdevice_defender_config_register_number_metric(task_config, &name_metric_number, get_number_metric_slow, ctx); struct aws_iotdevice_defender_task *defender_task = NULL; ASSERT_SUCCESS(aws_iotdevice_defender_task_create_ex( @@ -656,7 +660,8 @@ static int s_devicedefender_stop_while_running(struct aws_allocator *allocator, AWS_ZERO_STRUCT(payload); aws_mqtt_client_get_payload_for_outstanding_publish_packet( state_test_data->mqtt_connection, packet_id, allocator, &payload); - validate_devicedefender_record(allocator, (const char *)payload.buffer); + struct aws_byte_cursor payload_cursor = aws_byte_cursor_from_buf(&payload); + validate_devicedefender_record(allocator, &payload_cursor); return AWS_OP_SUCCESS; } @@ -692,9 +697,8 @@ static int s_devicedefender_publish_failure_callback_invoked(struct aws_allocato aws_iotdevice_defender_config_set_task_period_ns(task_config, 1000000000UL); aws_iotdevice_defender_config_set_task_failure_fn(task_config, s_task_failure_fn); - const struct aws_byte_cursor name_metric_number = aws_byte_cursor_from_c_str(TM_NUMBER); - ASSERT_SUCCESS( - aws_iotdevice_defender_config_register_number_metric(task_config, &name_metric_number, get_number_metric, ctx)); + struct aws_byte_cursor name_metric_number = aws_byte_cursor_from_c_str(TM_NUMBER); + aws_iotdevice_defender_config_register_number_metric(task_config, &name_metric_number, get_number_metric, ctx); struct aws_iotdevice_defender_task *defender_task = NULL; ASSERT_SUCCESS(aws_iotdevice_defender_task_create_ex(