Skip to content

Commit da56fdb

Browse files
committed
fix metrics with null username
1 parent e924c6c commit da56fdb

4 files changed

Lines changed: 67 additions & 18 deletions

File tree

source/v5/mqtt5_options_storage.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,9 @@ static size_t s_aws_mqtt5_packet_connect_compute_storage_size(
645645
size_t storage_size = 0;
646646

647647
storage_size += view->client_id.len;
648-
if (view->username != NULL) {
648+
649+
if (view->username != NULL || (options && options->metrics_storage != NULL)) {
650+
649651
if (options) {
650652
size_t username_size = 0;
651653
aws_mqtt_append_sdk_metrics_to_username(
@@ -699,17 +701,18 @@ int aws_mqtt5_packet_connect_storage_init(
699701
return AWS_OP_ERR;
700702
}
701703

702-
if (view->username != NULL) {
703-
storage->username = *view->username;
704+
if (view->username != NULL || (client_options_storage && client_options_storage->metrics_storage != NULL)) {
705+
if (view->username) {
706+
storage->username = *view->username;
707+
}
704708
struct aws_byte_buf metrics_username_buf;
705709
AWS_ZERO_STRUCT(metrics_username_buf);
706710

707711
/* Apply metrics to username if configured */
708712
if (client_options_storage) {
709-
struct aws_byte_cursor username_cur = storage->username;
710713
if (aws_mqtt_append_sdk_metrics_to_username(
711714
allocator,
712-
&username_cur,
715+
&storage->username,
713716
client_options_storage->metrics_storage ? &client_options_storage->metrics_storage->storage_view
714717
: NULL,
715718
&metrics_username_buf,

tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ add_test_case(mqtt_websocket_failed_transform)
115115
# Connection state tests
116116
add_test_case(mqtt_connection_set_metrics_valid)
117117
add_test_case(mqtt_connection_set_metrics_null)
118+
add_test_case(mqtt_connection_set_metrics_with_null_username)
118119
add_test_case(mqtt_connection_set_metrics_invalid_utf8_library)
119120
add_test_case(mqtt_connection_set_metrics_modify_on_reconnect)
120121

@@ -414,6 +415,7 @@ add_test_case(mqtt5_client_manual_puback_disconnect_cancellation)
414415
# Mqtt5 Metrics tests
415416
add_test_case(mqtt5_client_set_metrics_valid)
416417
add_test_case(mqtt5_client_set_metrics_null)
418+
add_test_case(mqtt5_client_set_metrics_with_null_username)
417419

418420
add_test_case(rate_limiter_token_bucket_init_invalid)
419421
add_test_case(rate_limiter_token_bucket_regeneration_integral)

tests/v3/connection_state_test.c

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4115,10 +4115,15 @@ AWS_TEST_CASE_FIXTURE(
41154115

41164116
/**
41174117
* helper function to test client with different metrics by checking the received username field
4118+
* @param allocator The allocator to use
4119+
* @param metrics The metrics to set (can be NULL to disable metrics)
4120+
* @param username The username to set (can be NULL to skip setting username)
4121+
* @param ctx The test context
41184122
*/
41194123
static int s_create_mqtt_connection_and_set_metrics(
41204124
struct aws_allocator *allocator,
41214125
struct aws_mqtt_iot_metrics *metrics,
4126+
struct aws_byte_cursor *username,
41224127
void *ctx) {
41234128
struct mqtt_connection_state_test *state_test_data = ctx;
41244129

@@ -4131,8 +4136,9 @@ static int s_create_mqtt_connection_and_set_metrics(
41314136
.on_connection_complete = aws_test311_on_connection_complete_fn,
41324137
};
41334138

4134-
struct aws_byte_cursor username = aws_byte_cursor_from_c_str("testuser");
4135-
ASSERT_SUCCESS(aws_mqtt_client_connection_set_login(state_test_data->mqtt_connection, &username, NULL));
4139+
if (username != NULL) {
4140+
ASSERT_SUCCESS(aws_mqtt_client_connection_set_login(state_test_data->mqtt_connection, username, NULL));
4141+
}
41364142

41374143
ASSERT_SUCCESS(aws_mqtt_client_connection_set_metrics(state_test_data->mqtt_connection, metrics));
41384144

@@ -4154,9 +4160,9 @@ static int s_create_mqtt_connection_and_set_metrics(
41544160
struct aws_byte_buf expected_buf;
41554161
AWS_ZERO_STRUCT(expected_buf);
41564162
if (metrics) {
4157-
aws_test_mqtt_build_expected_metrics(allocator, &username, metrics->library_name, NULL, &expected_buf);
4158-
} else {
4159-
aws_byte_buf_init_copy_from_cursor(&expected_buf, allocator, username);
4163+
aws_test_mqtt_build_expected_metrics(allocator, username, metrics->library_name, NULL, &expected_buf);
4164+
} else if (username != NULL) {
4165+
aws_byte_buf_init_copy_from_cursor(&expected_buf, allocator, *username);
41604166
}
41614167

41624168
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&received_packet->username, &expected_buf));
@@ -4179,7 +4185,8 @@ static int s_test_mqtt_connection_set_metrics_valid_fn(struct aws_allocator *all
41794185
// .metadata_count = 0,
41804186
};
41814187

4182-
ASSERT_SUCCESS(s_create_mqtt_connection_and_set_metrics(allocator, &metrics, ctx));
4188+
struct aws_byte_cursor username = aws_byte_cursor_from_c_str("testuser");
4189+
ASSERT_SUCCESS(s_create_mqtt_connection_and_set_metrics(allocator, &metrics, &username, ctx));
41834190

41844191
return AWS_OP_SUCCESS;
41854192
}
@@ -4196,7 +4203,8 @@ AWS_TEST_CASE_FIXTURE(
41964203
*/
41974204
static int s_test_mqtt_connection_set_metrics_null_fn(struct aws_allocator *allocator, void *ctx) {
41984205

4199-
ASSERT_SUCCESS(s_create_mqtt_connection_and_set_metrics(allocator, NULL, ctx));
4206+
struct aws_byte_cursor username = aws_byte_cursor_from_c_str("testuser");
4207+
ASSERT_SUCCESS(s_create_mqtt_connection_and_set_metrics(allocator, NULL, &username, ctx));
42004208

42014209
return AWS_OP_SUCCESS;
42024210
}
@@ -4208,6 +4216,27 @@ AWS_TEST_CASE_FIXTURE(
42084216
s_clean_up_mqtt_server_fn,
42094217
&test_data)
42104218

4219+
static int s_test_mqtt_connection_set_metrics_with_null_username_fn(struct aws_allocator *allocator, void *ctx) {
4220+
4221+
struct aws_mqtt_iot_metrics metrics = {
4222+
.library_name = aws_byte_cursor_from_c_str("TestSDK/1.0"),
4223+
// TODO: enable metadata testing when metadata support is added
4224+
// .metadata_entries = NULL,
4225+
// .metadata_count = 0,
4226+
};
4227+
4228+
ASSERT_SUCCESS(s_create_mqtt_connection_and_set_metrics(allocator, &metrics, NULL, ctx));
4229+
4230+
return AWS_OP_SUCCESS;
4231+
}
4232+
4233+
AWS_TEST_CASE_FIXTURE(
4234+
mqtt_connection_set_metrics_with_null_username,
4235+
s_setup_mqtt_server_fn,
4236+
s_test_mqtt_connection_set_metrics_with_null_username_fn,
4237+
s_clean_up_mqtt_server_fn,
4238+
&test_data)
4239+
42114240
/**
42124241
* Test that aws_mqtt_client_connection_set_metrics rejects invalid UTF-8 in library name
42134242
*/

tests/v5/mqtt5_client_tests.c

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6659,6 +6659,7 @@ AWS_TEST_CASE(mqtt5_client_auto_assigned_client_id_iot_core, s_mqtt5_client_auto
66596659
static int s_mqtt5_client_metrics_in_username_fn(
66606660
struct aws_allocator *allocator,
66616661
struct aws_mqtt_iot_metrics *metrics,
6662+
struct aws_byte_cursor *original_username,
66626663
void *ctx) {
66636664
(void)ctx;
66646665

@@ -6668,14 +6669,11 @@ static int s_mqtt5_client_metrics_in_username_fn(
66686669
aws_mqtt5_client_test_init_default_options(&test_options);
66696670
test_options.client_options.metrics = metrics;
66706671

6671-
/* Set up username and metrics */
6672-
struct aws_byte_cursor original_username = aws_byte_cursor_from_c_str("test_user");
6673-
66746672
struct aws_mqtt5_packet_connect_view connect_view = {
66756673
.keep_alive_interval_seconds = 30,
66766674
.client_id = aws_byte_cursor_from_string(g_default_client_id),
66776675
.clean_start = true,
6678-
.username = &original_username};
6676+
.username = original_username};
66796677

66806678
test_options.connect_options = connect_view;
66816679

@@ -6776,14 +6774,31 @@ static int s_test_mqtt5_client_set_metrics_valid(struct aws_allocator *allocator
67766774
// .metadata_count = 0,
67776775
};
67786776

6779-
return s_mqtt5_client_metrics_in_username_fn(allocator, &metrics, ctx);
6777+
struct aws_byte_cursor username = aws_byte_cursor_from_c_str("test_user");
6778+
6779+
return s_mqtt5_client_metrics_in_username_fn(allocator, &metrics, &username, ctx);
67806780
}
67816781

67826782
AWS_TEST_CASE(mqtt5_client_set_metrics_valid, s_test_mqtt5_client_set_metrics_valid)
67836783

6784+
static int s_test_mqtt5_client_set_metrics_with_null_username(struct aws_allocator *allocator, void *ctx) {
6785+
6786+
struct aws_mqtt_iot_metrics metrics = {
6787+
.library_name = aws_byte_cursor_from_c_str("TestSDK/1.0")
6788+
// TODO: enable when metadata is supported
6789+
// .metadata_entries = NULL,
6790+
// .metadata_count = 0,
6791+
};
6792+
6793+
return s_mqtt5_client_metrics_in_username_fn(allocator, &metrics, NULL, ctx);
6794+
}
6795+
6796+
AWS_TEST_CASE(mqtt5_client_set_metrics_with_null_username, s_test_mqtt5_client_set_metrics_with_null_username)
6797+
67846798
static int s_test_mqtt5_client_set_metrics_null(struct aws_allocator *allocator, void *ctx) {
67856799

6786-
return s_mqtt5_client_metrics_in_username_fn(allocator, NULL, ctx);
6800+
struct aws_byte_cursor username = aws_byte_cursor_from_c_str("test_user");
6801+
return s_mqtt5_client_metrics_in_username_fn(allocator, NULL, &username, ctx);
67876802
}
67886803

67896804
AWS_TEST_CASE(mqtt5_client_set_metrics_null, s_test_mqtt5_client_set_metrics_null)

0 commit comments

Comments
 (0)