|
11 | 11 | #include <aws/io/channel_bootstrap.h> |
12 | 12 | #include <aws/io/event_loop.h> |
13 | 13 | #include <aws/mqtt/mqtt.h> |
| 14 | +#include <aws/mqtt/private/client_impl_shared.h> |
14 | 15 | #include <aws/mqtt/private/v5/mqtt5_utils.h> |
15 | 16 | #include <aws/mqtt/v5/mqtt5_client.h> |
16 | 17 | #include <aws/mqtt/v5/mqtt5_listener.h> |
@@ -6652,3 +6653,135 @@ static int s_mqtt5_client_auto_assigned_client_id_iot_core_fn(struct aws_allocat |
6652 | 6653 | } |
6653 | 6654 |
|
6654 | 6655 | AWS_TEST_CASE(mqtt5_client_auto_assigned_client_id_iot_core, s_mqtt5_client_auto_assigned_client_id_iot_core_fn) |
| 6656 | +/* |
| 6657 | + * Test that verifies metrics are properly appended to username in CONNECT packet |
| 6658 | + * Based on s_mqtt5_client_direct_connect_success_fn |
| 6659 | + */ |
| 6660 | +static int s_mqtt5_client_metrics_in_username_fn( |
| 6661 | + struct aws_allocator *allocator, |
| 6662 | + struct aws_mqtt_iot_sdk_metrics *metrics, |
| 6663 | + void *ctx) { |
| 6664 | + (void)ctx; |
| 6665 | + |
| 6666 | + aws_mqtt_library_init(allocator); |
| 6667 | + |
| 6668 | + struct mqtt5_client_test_options test_options; |
| 6669 | + aws_mqtt5_client_test_init_default_options(&test_options); |
| 6670 | + |
| 6671 | + /* Set up username and metrics */ |
| 6672 | + struct aws_byte_cursor original_username = aws_byte_cursor_from_c_str("test_user"); |
| 6673 | + |
| 6674 | + struct aws_mqtt5_packet_connect_view connect_view = { |
| 6675 | + .keep_alive_interval_seconds = 30, |
| 6676 | + .client_id = aws_byte_cursor_from_string(g_default_client_id), |
| 6677 | + .clean_start = true, |
| 6678 | + .metrics = metrics, |
| 6679 | + .username = &original_username}; |
| 6680 | + |
| 6681 | + test_options.connect_options = connect_view; |
| 6682 | + |
| 6683 | + struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = { |
| 6684 | + .client_options = &test_options.client_options, |
| 6685 | + .server_function_table = &test_options.server_function_table, |
| 6686 | + }; |
| 6687 | + |
| 6688 | + struct aws_mqtt5_client_mock_test_fixture test_context; |
| 6689 | + ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options)); |
| 6690 | + |
| 6691 | + struct aws_mqtt5_client *client = test_context.client; |
| 6692 | + ASSERT_SUCCESS(aws_mqtt5_client_start(client)); |
| 6693 | + |
| 6694 | + aws_wait_for_connected_lifecycle_event(&test_context); |
| 6695 | + |
| 6696 | + struct aws_mqtt5_packet_disconnect_view disconnect_options = { |
| 6697 | + .reason_code = AWS_MQTT5_DRC_NORMAL_DISCONNECTION, |
| 6698 | + }; |
| 6699 | + |
| 6700 | + struct aws_mqtt5_disconnect_completion_options completion_options = { |
| 6701 | + .completion_callback = s_on_disconnect_completion, |
| 6702 | + .completion_user_data = &test_context, |
| 6703 | + }; |
| 6704 | + |
| 6705 | + ASSERT_SUCCESS(aws_mqtt5_client_stop(client, &disconnect_options, &completion_options)); |
| 6706 | + |
| 6707 | + aws_wait_for_stopped_lifecycle_event(&test_context); |
| 6708 | + s_wait_for_disconnect_completion(&test_context); |
| 6709 | + s_wait_for_mock_server_to_receive_disconnect_packet(&test_context); |
| 6710 | + |
| 6711 | + struct aws_mqtt5_client_lifecycle_event expected_events[] = { |
| 6712 | + { |
| 6713 | + .event_type = AWS_MQTT5_CLET_ATTEMPTING_CONNECT, |
| 6714 | + }, |
| 6715 | + { |
| 6716 | + .event_type = AWS_MQTT5_CLET_CONNECTION_SUCCESS, |
| 6717 | + }, |
| 6718 | + { |
| 6719 | + .event_type = AWS_MQTT5_CLET_DISCONNECTION, |
| 6720 | + .error_code = AWS_ERROR_MQTT5_USER_REQUESTED_STOP, |
| 6721 | + }, |
| 6722 | + { |
| 6723 | + .event_type = AWS_MQTT5_CLET_STOPPED, |
| 6724 | + }, |
| 6725 | + }; |
| 6726 | + ASSERT_SUCCESS( |
| 6727 | + s_verify_simple_lifecycle_event_sequence(&test_context, expected_events, AWS_ARRAY_SIZE(expected_events))); |
| 6728 | + |
| 6729 | + enum aws_mqtt5_client_state expected_states[] = { |
| 6730 | + AWS_MCS_CONNECTING, |
| 6731 | + AWS_MCS_MQTT_CONNECT, |
| 6732 | + AWS_MCS_CONNECTED, |
| 6733 | + AWS_MCS_CLEAN_DISCONNECT, |
| 6734 | + AWS_MCS_CHANNEL_SHUTDOWN, |
| 6735 | + AWS_MCS_STOPPED, |
| 6736 | + }; |
| 6737 | + |
| 6738 | + ASSERT_SUCCESS(aws_verify_client_state_sequence(&test_context, expected_states, AWS_ARRAY_SIZE(expected_states))); |
| 6739 | + |
| 6740 | + struct aws_mqtt5_packet_connect_storage expected_connect_storage; |
| 6741 | + |
| 6742 | + aws_mqtt5_packet_connect_storage_init(&expected_connect_storage, allocator, &connect_view); |
| 6743 | + |
| 6744 | + struct aws_mqtt5_packet_disconnect_storage expected_disconnect_storage; |
| 6745 | + ASSERT_SUCCESS(s_aws_mqtt5_client_test_init_default_disconnect_storage(&expected_disconnect_storage, allocator)); |
| 6746 | + expected_disconnect_storage.storage_view.reason_code = AWS_MQTT5_DRC_NORMAL_DISCONNECTION; |
| 6747 | + |
| 6748 | + struct aws_mqtt5_mock_server_packet_record expected_packets[] = { |
| 6749 | + { |
| 6750 | + .packet_type = AWS_MQTT5_PT_CONNECT, |
| 6751 | + .packet_storage = &expected_connect_storage, |
| 6752 | + }, |
| 6753 | + { |
| 6754 | + .packet_type = AWS_MQTT5_PT_DISCONNECT, |
| 6755 | + .packet_storage = &expected_disconnect_storage, |
| 6756 | + }, |
| 6757 | + }; |
| 6758 | + ASSERT_SUCCESS( |
| 6759 | + aws_verify_received_packet_sequence(&test_context, expected_packets, AWS_ARRAY_SIZE(expected_packets))); |
| 6760 | + |
| 6761 | + aws_mqtt5_packet_connect_storage_clean_up(&expected_connect_storage); |
| 6762 | + |
| 6763 | + aws_mqtt5_client_mock_test_fixture_clean_up(&test_context); |
| 6764 | + aws_mqtt_library_clean_up(); |
| 6765 | + |
| 6766 | + return AWS_OP_SUCCESS; |
| 6767 | +} |
| 6768 | + |
| 6769 | +static int s_test_mqtt5_client_set_metrics_valid(struct aws_allocator *allocator, void *ctx) { |
| 6770 | + struct aws_mqtt_iot_sdk_metrics metrics = { |
| 6771 | + .library_name = aws_byte_cursor_from_c_str("TestSDK/1.0") |
| 6772 | + // TODO: enable when metadata is supported |
| 6773 | + // .metadata_entries = NULL, |
| 6774 | + // .metadata_count = 0, |
| 6775 | + }; |
| 6776 | + |
| 6777 | + return s_mqtt5_client_metrics_in_username_fn(allocator, &metrics, ctx); |
| 6778 | +} |
| 6779 | + |
| 6780 | +AWS_TEST_CASE(mqtt5_client_set_metrics_valid, s_test_mqtt5_client_set_metrics_valid) |
| 6781 | + |
| 6782 | +static int s_test_mqtt5_client_set_metrics_null(struct aws_allocator *allocator, void *ctx) { |
| 6783 | + |
| 6784 | + return s_mqtt5_client_metrics_in_username_fn(allocator, NULL, ctx); |
| 6785 | +} |
| 6786 | + |
| 6787 | +AWS_TEST_CASE(mqtt5_client_set_metrics_null, s_test_mqtt5_client_set_metrics_null) |
0 commit comments