Skip to content

Commit 6e59b46

Browse files
authored
Fix library initialization (#82)
- Fix errors being reported incorrectly - error names were off-by-one due to missing entry in s_errors - `aws_iotdevice_library_init()` now initializes the libraries it depends on
1 parent fc21ca5 commit 6e59b46

File tree

7 files changed

+28
-18
lines changed

7 files changed

+28
-18
lines changed

source/iotdevice.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
#define AWS_DEFINE_ERROR_INFO_IOTDEVICE(C, ES) AWS_DEFINE_ERROR_INFO(C, ES, "libaws-c-iotdevice")
1515
/* clang-format off */
1616
static struct aws_error_info s_errors[] = {
17+
AWS_DEFINE_ERROR_INFO_IOTDEVICE(
18+
AWS_ERROR_IOTDEVICE_INVALID_RESERVED_BITS,
19+
"Bits marked as reserved were incorrectly set"),
1720
AWS_DEFINE_ERROR_INFO_IOTDEVICE(
1821
AWS_ERROR_IOTDEVICE_DEFENDER_INVALID_REPORT_INTERVAL,
1922
"Invalid defender task reporting interval. Must be greater than 5 minutes"),
@@ -62,23 +65,25 @@ static bool s_iotdevice_library_initialized = false;
6265

6366
void aws_iotdevice_library_init(struct aws_allocator *allocator) {
6467
AWS_PRECONDITION(aws_allocator_is_valid(allocator));
65-
(void)(allocator); // ignore unused warning
6668

6769
if (!s_iotdevice_library_initialized) {
70+
s_iotdevice_library_initialized = true;
71+
72+
aws_mqtt_library_init(allocator);
6873
aws_register_error_info(&s_error_list);
6974
aws_register_log_subject_info_list(&s_logging_subjects_list);
70-
71-
s_iotdevice_library_initialized = true;
7275
}
7376
}
7477

7578
void aws_iotdevice_library_clean_up(void) {
7679
if (s_iotdevice_library_initialized) {
80+
s_iotdevice_library_initialized = false;
81+
7782
aws_thread_join_all_managed();
7883

7984
aws_unregister_error_info(&s_error_list);
8085
aws_unregister_log_subject_info_list(&s_logging_subjects_list);
8186

82-
s_iotdevice_library_initialized = false;
87+
aws_mqtt_library_clean_up();
8388
}
8489
}

tests/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ include(AwsTestHarness)
33
enable_testing()
44

55
file(GLOB TEST_HDRS "mqtt_mock_structs.h")
6-
set(TEST_SRC metrics_tests.c secure_tunneling_tests.c)
6+
set(TEST_SRC iotdevice_tests.c metrics_tests.c secure_tunneling_tests.c)
77
file(GLOB TESTS ${TEST_HDRS} ${TEST_SRC})
88

9+
add_test_case(library_init)
910
add_test_case(devicedefender_task_unsupported_report_format)
1011

1112
# Network metrics are only implemented for Linux so far

tests/aws_iot_devicedefender_client_test.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ int main(int argc, char **argv) {
227227
args.mutex = &mutex;
228228
args.condition_variable = &condition_variable;
229229

230-
aws_mqtt_library_init(args.allocator);
231230
aws_iotdevice_library_init(args.allocator);
232231

233232
struct aws_logger_standard_options logger_options = {
@@ -352,7 +351,6 @@ int main(int argc, char **argv) {
352351
aws_logger_clean_up(&logger);
353352

354353
aws_iotdevice_library_clean_up();
355-
aws_mqtt_library_clean_up();
356354

357355
ASSERT_UINT_EQUALS(0, aws_mem_tracer_count(allocator));
358356
allocator = aws_mem_tracer_destroy(allocator);

tests/aws_iot_secure_tunneling_client_test.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ int main(int argc, char **argv) {
119119

120120
struct aws_allocator *allocator = aws_mem_tracer_new(aws_default_allocator(), NULL, AWS_MEMTRACE_BYTES, 0);
121121

122-
aws_http_library_init(allocator);
123122
aws_iotdevice_library_init(allocator);
124123

125124
struct aws_logger_standard_options logger_options = {
@@ -191,7 +190,6 @@ int main(int argc, char **argv) {
191190
aws_event_loop_group_release(elg);
192191
aws_logger_clean_up(&logger);
193192
aws_iotdevice_library_clean_up();
194-
aws_http_library_clean_up();
195193

196194
ASSERT_UINT_EQUALS(0, aws_mem_tracer_count(allocator));
197195
allocator = aws_mem_tracer_destroy(allocator);

tests/iotdevice_tests.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0.
4+
*/
5+
#include <aws/iotdevice/iotdevice.h>
6+
#include <aws/testing/aws_test_harness.h>
7+
8+
static int s_library_init(struct aws_allocator *allocator, void *ctx) {
9+
(void)ctx;
10+
aws_iotdevice_library_init(allocator);
11+
aws_iotdevice_library_clean_up();
12+
return 0;
13+
}
14+
15+
AWS_TEST_CASE(library_init, s_library_init);

tests/metrics_tests.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ static int s_devicedefender_get_network_connections(struct aws_allocator *alloca
421421
}
422422

423423
static int s_setup_mqtt_test_data_fn(struct aws_allocator *allocator, void *ctx) {
424-
aws_mqtt_library_init(allocator);
424+
aws_iotdevice_library_init(allocator);
425425

426426
struct mqtt_connection_test_data *state_test_data = ctx;
427427
AWS_ZERO_STRUCT(*state_test_data);
@@ -472,7 +472,6 @@ static int s_clean_up_mqtt_test_data_fn(struct aws_allocator *allocator, int set
472472
}
473473

474474
aws_iotdevice_library_clean_up();
475-
aws_mqtt_library_clean_up();
476475
return AWS_OP_SUCCESS;
477476
}
478477

@@ -502,7 +501,6 @@ static int s_publish_fn_copy_report(struct aws_byte_cursor payload, void *userda
502501
static int s_devicedefender_success_test(struct aws_allocator *allocator, void *ctx) {
503502
(void)allocator;
504503
struct mqtt_connection_test_data *state_test_data = ctx;
505-
aws_iotdevice_library_init(state_test_data->allocator);
506504

507505
struct aws_iotdevice_defender_task_config *task_config = NULL;
508506
struct aws_byte_cursor thing_name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("TestSuccessThing");
@@ -547,7 +545,6 @@ static int s_devicedefender_custom_metrics_success_test(struct aws_allocator *al
547545
(void)allocator;
548546
struct mqtt_connection_test_data *state_test_data = ctx;
549547

550-
aws_iotdevice_library_init(state_test_data->allocator);
551548
struct aws_byte_cursor thing_name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("TestCustomMetricSuccessThing");
552549
struct aws_iotdevice_defender_task_config *task_config = NULL;
553550
ASSERT_SUCCESS(aws_iotdevice_defender_config_create(&task_config, allocator, &thing_name, AWS_IDDRF_JSON));
@@ -625,7 +622,6 @@ static int s_devicedefender_stop_while_running(struct aws_allocator *allocator,
625622
(void)allocator;
626623
struct mqtt_connection_test_data *state_test_data = ctx;
627624

628-
aws_iotdevice_library_init(state_test_data->allocator);
629625
struct aws_byte_cursor thing_name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("TestCustomMetricSuccessThing");
630626
struct aws_iotdevice_defender_task_config *task_config = NULL;
631627
ASSERT_SUCCESS(aws_iotdevice_defender_config_create(&task_config, allocator, &thing_name, AWS_IDDRF_JSON));
@@ -687,7 +683,6 @@ static int s_devicedefender_publish_failure_callback_invoked(struct aws_allocato
687683
(void)allocator;
688684
struct mqtt_connection_test_data *state_test_data = ctx;
689685

690-
aws_iotdevice_library_init(state_test_data->allocator);
691686
struct aws_byte_cursor thing_name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("TestCustomMetricSuccessThing");
692687
struct aws_iotdevice_defender_task_config *task_config = NULL;
693688
ASSERT_SUCCESS(aws_iotdevice_defender_config_create(&task_config, allocator, &thing_name, AWS_IDDRF_JSON));

tests/secure_tunneling_tests.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,7 @@ static struct aws_secure_tunnel *s_secure_tunnel_new_mock(const struct aws_secur
180180
static int before(struct aws_allocator *allocator, void *ctx) {
181181
struct secure_tunneling_test_context *test_context = ctx;
182182

183-
/* Initialize aws-c-http and aws-c-iot libraries. */
184-
aws_http_library_init(allocator);
183+
/* Initialize aws-c-iot library. */
185184
aws_iotdevice_library_init(allocator);
186185

187186
/* Initialize event loop. */
@@ -246,7 +245,6 @@ static int after(struct aws_allocator *allocator, int setup_result, void *ctx) {
246245
aws_thread_join_all_managed();
247246

248247
aws_iotdevice_library_clean_up();
249-
aws_http_library_clean_up();
250248

251249
return AWS_OP_SUCCESS;
252250
}

0 commit comments

Comments
 (0)