@@ -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 */
41194123static 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 */
41974204static 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 */
0 commit comments