include(AwsLibFuzzer)
include(AwsTestHarness)
enable_testing()

file(GLOB TEST_SRC "*.c")
file(GLOB TEST_HDRS "*.h")
file(GLOB TESTS ${TEST_HDRS} ${TEST_SRC})

add_test_case(credentials_create_destroy_test)
add_test_case(anonymous_credentials_create_destroy_test)
add_test_case(static_credentials_provider_basic_test)
add_test_case(anonymous_credentials_provider_basic_test)
add_test_case(environment_credentials_provider_basic_test)
add_test_case(environment_credentials_provider_empty_env_test)
add_test_case(environment_credentials_provider_negative_test)
add_test_case(cached_credentials_provider_elapsed_test)
add_test_case(cached_credentials_provider_expired_test)
add_test_case(cached_credentials_provider_queued_async_test)
add_test_case(profile_credentials_provider_new_destroy_defaults_test)
add_test_case(profile_credentials_provider_cached_test)
add_test_case(profile_credentials_provider_default_test)
add_test_case(profile_credentials_provider_nondefault_test)
add_test_case(profile_credentials_provider_environment_test)
add_test_case(credentials_provider_first_in_chain_test)
add_test_case(credentials_provider_second_in_chain_test)
add_test_case(credentials_provider_null_chain_test)
add_net_test_case(credentials_provider_default_basic_test)
add_net_test_case(credentials_provider_default_manual_tls_test)
add_test_case(credentials_provider_imds_new_destroy)
add_test_case(credentials_provider_imds_connect_failure)
add_test_case(credentials_provider_imds_token_request_failure)
add_test_case(credentials_provider_imds_role_name_request_failure)
add_test_case(credentials_provider_imds_role_request_failure)
add_test_case(credentials_provider_imds_bad_document_failure)
add_test_case(credentials_provider_imds_secure_success)
add_test_case(credentials_provider_imds_connection_closed_success)
add_test_case(credentials_provider_imds_insecure_success)
add_test_case(credentials_provider_imds_insecure_then_secure_success)
add_test_case(credentials_provider_imds_success_multi_part_role_name)
add_test_case(credentials_provider_imds_success_multi_part_doc)
add_test_case(credentials_provider_imds_real_new_destroy)

if(AWS_BUILDING_ON_EC2)
    add_test_case(credentials_provider_imds_real_success)
endif()

add_test_case(credentials_provider_ecs_new_destroy)
add_test_case(credentials_provider_ecs_connect_failure)
add_test_case(credentials_provider_ecs_request_failure)
add_test_case(credentials_provider_ecs_bad_document_failure)
add_test_case(credentials_provider_ecs_basic_success)
add_test_case(credentials_provider_ecs_no_auth_token_success)
add_test_case(credentials_provider_ecs_success_multi_part_doc)
add_test_case(credentials_provider_ecs_real_new_destroy)

if(AWS_BUILDING_ON_ECS)
    add_test_case(credentials_provider_ecs_real_success)
endif()

add_test_case(credentials_provider_x509_new_destroy)
add_test_case(credentials_provider_x509_connect_failure)
add_test_case(credentials_provider_x509_request_failure)
add_test_case(credentials_provider_x509_bad_document_failure)
add_test_case(credentials_provider_x509_basic_success)
add_test_case(credentials_provider_x509_success_multi_part_doc)
add_test_case(credentials_provider_x509_real_new_destroy)

add_net_test_case(credentials_provider_sts_web_identity_new_destroy_from_parameters)
add_net_test_case(credentials_provider_sts_web_identity_new_destroy_from_env)
add_net_test_case(credentials_provider_sts_web_identity_new_destroy_from_config)
add_net_test_case(credentials_provider_sts_web_identity_new_destroy_from_cached_config)
add_net_test_case(credentials_provider_sts_web_identity_new_failed_without_env_and_config)
add_net_test_case(credentials_provider_sts_web_identity_connect_failure)
add_net_test_case(credentials_provider_sts_web_identity_request_failure)
add_net_test_case(credentials_provider_sts_web_identity_bad_document_failure)
add_net_test_case(credentials_provider_sts_web_identity_test_retry_error1)
add_net_test_case(credentials_provider_sts_web_identity_test_retry_error2)
add_net_test_case(credentials_provider_sts_web_identity_basic_success_env)
add_net_test_case(credentials_provider_sts_web_identity_basic_success_config)
add_net_test_case(credentials_provider_sts_web_identity_success_multi_part_doc)
add_net_test_case(credentials_provider_sts_web_identity_real_new_destroy)

add_net_test_case(credentials_provider_sts_direct_config_succeeds)
add_net_test_case(credentials_provider_sts_direct_config_succeeds_after_retry)
add_net_test_case(credentials_provider_sts_direct_config_invalid_doc)
add_net_test_case(credentials_provider_sts_direct_config_connection_failed)
add_net_test_case(credentials_provider_sts_direct_config_service_fails)
add_net_test_case(credentials_provider_sts_from_profile_config_succeeds)
add_net_test_case(credentials_provider_sts_from_profile_config_manual_tls_succeeds)
add_net_test_case(credentials_provider_sts_from_profile_config_environment_succeeds)
add_net_test_case(credentials_provider_sts_cache_expiration_conflict)

add_test_case(credentials_provider_process_new_destroy_from_config)
add_test_case(credentials_provider_process_new_destroy_from_config_without_token)
add_test_case(credentials_provider_process_new_failed)
add_test_case(credentials_provider_process_bad_command)
add_test_case(credentials_provider_process_incorrect_command_output)
add_test_case(credentials_provider_process_basic_success)
add_test_case(credentials_provider_process_basic_success_cached)

add_net_test_case(credentials_provider_cognito_new_destroy)
add_net_test_case(credentials_provider_cognito_failure_connect)
add_net_test_case(credentials_provider_cognito_failure_request)
add_net_test_case(credentials_provider_cognito_failure_bad_document)
add_net_test_case(credentials_provider_cognito_success)
add_net_test_case(credentials_provider_cognito_success_after_retry)

if(AWS_HAS_CI_ENVIRONMENT)
    add_net_test_case(credentials_provider_cognito_success_unauthenticated)
endif()

add_test_case(sso_token_provider_profile_invalid_profile_test)
add_test_case(sso_token_provider_profile_valid_profile_test)
add_net_test_case(sso_token_provider_sso_session_invalid_config_test)
add_net_test_case(sso_token_provider_sso_session_valid_config_test)
add_net_test_case(sso_token_provider_sso_session_basic_success)
add_net_test_case(sso_token_provider_sso_session_config_file_cached)
add_net_test_case(sso_token_provider_sso_session_expired_token)
add_test_case(sso_token_provider_profile_basic_success)
add_test_case(sso_token_provider_profile_cached_config_file)
add_test_case(sso_token_provider_profile_expired_token)

add_test_case(parse_token_location_url_test)
add_test_case(parse_token_location_session_test)
add_test_case(parse_sso_token_valid)
add_test_case(parse_sso_token_invalid)
add_test_case(parse_sso_token_invalid_missing_access_token)
add_test_case(parse_sso_token_missing_expires_at)
add_test_case(parse_sso_token_invalid_expires_at)

add_net_test_case(credentials_provider_sso_failed_invalid_config)
add_net_test_case(credentials_provider_sso_create_destroy_valid_config)
add_net_test_case(credentials_provider_sso_connect_failure)
add_net_test_case(credentials_provider_sso_failure_token_missing)
add_net_test_case(credentials_provider_sso_failure_token_expired)
add_net_test_case(credentials_provider_sso_failure_token_empty)
add_net_test_case(credentials_provider_sso_request_failure)
add_net_test_case(credentials_provider_sso_bad_response)
add_net_test_case(credentials_provider_sso_retryable_error)
add_net_test_case(credentials_provider_sso_basic_success)
add_net_test_case(credentials_provider_sso_basic_success_cached_config_file)
add_net_test_case(credentials_provider_sso_basic_success_profile)
add_net_test_case(credentials_provider_sso_basic_success_profile_cached_config_file)
add_net_test_case(credentials_provider_sso_basic_success_after_failure)

add_test_case(imds_client_new_release)
add_test_case(imds_client_connect_failure)
add_test_case(imds_client_token_request_failure)
add_test_case(imds_client_insecure_fallback_request_failure)
add_test_case(imds_client_resource_request_failure)
add_test_case(imds_client_resource_request_success)
add_test_case(imds_client_insecure_resource_request_success)
add_test_case(imds_client_insecure_then_secure_resource_request_success)
add_test_case(imds_client_multiple_resource_requests_random_responses_finally_all_success)
add_test_case(imds_client_get_ami_id_success)
add_test_case(imds_client_get_ancestor_ami_ids_success)
add_test_case(imds_client_get_iam_profile_success)
add_test_case(imds_client_get_instance_info_success)
add_test_case(imds_client_get_credentials_success)
add_test_case(imds_client_cache_token_refresh)

if(AWS_BUILDING_ON_EC2)
    add_test_case(imds_client_real_success)
endif()

add_test_case(config_file_path_override_test)
add_test_case(config_file_path_environment_test)
add_test_case(credentials_file_path_override_test)
add_test_case(credentials_file_path_environment_test)
add_test_case(profile_override_test)
add_test_case(profile_environment_test)

add_test_case(sigv4_skip_xray_header_test)
add_test_case(sigv4_skip_user_agent_header_test)
add_test_case(sigv4_skip_custom_header_test)

add_test_case(sigv4_fail_date_header_test)
add_test_case(sigv4_fail_content_header_test)
add_test_case(sigv4_fail_authorization_header_test)
add_test_case(sigv4_fail_signature_param_test)
add_test_case(sigv4_fail_date_param_test)
add_test_case(sigv4_fail_credential_param_test)
add_test_case(sigv4_fail_algorithm_param_test)
add_test_case(sigv4_fail_signed_headers_param_test)
add_test_case(signer_null_credentials_test)
add_test_case(signer_anonymous_credentials_test)
add_test_case(signer_anonymous_credentials_provider_test)
add_test_case(sigv4_chunked_signing_test)
add_test_case(sigv4_event_signing_test)
add_test_case(sigv4a_chunked_signing_test)
add_test_case(sigv4_trailing_headers_signing_test)
add_test_case(sigv4a_trailing_headers_signing_test)

add_test_case(credentials_derive_ecc_key_fixed)
add_test_case(credentials_new_ecc_fixed)
add_test_case(credentials_derive_ecc_key_long_access_key)
add_test_case(be_sequence_add_one)
add_test_case(be_sequence_compare)

add_test_case(sigv4a_get_header_key_duplicate_test)
add_test_case(sigv4a_get_header_value_multiline_test)
add_test_case(sigv4a_get_header_value_order_test)
add_test_case(sigv4a_get_header_value_trim_test)
add_test_case(sigv4a_get_unreserved_test)
add_test_case(sigv4a_get_utf8_test)
add_test_case(sigv4a_get_vanilla_test)
add_test_case(sigv4a_get_vanilla_with_session_token_test)
add_test_case(sigv4a_get_vanilla_empty_query_key_test)
add_test_case(sigv4a_get_vanilla_query_test)
add_test_case(sigv4a_get_vanilla_query_order_key_case_test)
add_test_case(sigv4a_get_vanilla_query_order_encoded_test)
add_test_case(sigv4a_get_vanilla_unreserved_test)
add_test_case(sigv4a_get_vanilla_utf8_query_test)
add_test_case(sigv4a_post_header_key_case_test)
add_test_case(sigv4a_post_header_key_sort_test)
add_test_case(sigv4a_post_header_value_case_test)
add_test_case(sigv4a_post_vanilla_test)
add_test_case(sigv4a_post_vanilla_empty_query_value_test)
add_test_case(sigv4a_post_vanilla_query_test)
add_test_case(sigv4a_post_x_www_form_urlencoded_test)
add_test_case(sigv4a_post_x_www_form_urlencoded_parameters_test)
add_test_case(sigv4a_post_sts_header_after_test)
add_test_case(sigv4a_post_sts_header_before_test)
add_test_case(sigv4a_get_relative_normalized_test)
add_test_case(sigv4a_get_relative_unnormalized_test)
add_test_case(sigv4a_get_relative_relative_normalized_test)
add_test_case(sigv4a_get_relative_relative_unnormalized_test)
add_test_case(sigv4a_get_slash_normalized_test)
add_test_case(sigv4a_get_slash_unnormalized_test)
add_test_case(sigv4a_get_slash_dot_slash_normalized_test)
add_test_case(sigv4a_get_slash_dot_slash_unnormalized_test)
add_test_case(sigv4a_get_slash_pointless_dot_normalized_test)
add_test_case(sigv4a_get_slash_pointless_dot_unnormalized_test)
add_test_case(sigv4a_get_slashes_normalized_test)
add_test_case(sigv4a_get_slashes_unnormalized_test)
add_test_case(sigv4a_get_space_normalized_test)
add_test_case(sigv4a_get_space_unnormalized_test)

add_test_case(sigv4_get_header_key_duplicate_test)
add_test_case(sigv4_get_header_value_multiline_test)
add_test_case(sigv4_get_header_value_order_test)
add_test_case(sigv4_get_header_value_trim_test)
add_test_case(sigv4_get_unreserved_test)
add_test_case(sigv4_get_utf8_test)
add_test_case(sigv4_get_vanilla_test)
add_test_case(sigv4_get_vanilla_with_session_token_test)
add_test_case(sigv4_get_vanilla_empty_query_key_test)
add_test_case(sigv4_get_vanilla_query_test)
add_test_case(sigv4_get_vanilla_query_order_key_case_test)
add_test_case(sigv4_get_vanilla_query_order_encoded_test)
add_test_case(sigv4_get_vanilla_unreserved_test)
add_test_case(sigv4_get_vanilla_utf8_query_test)
add_test_case(sigv4_post_header_key_case_test)
add_test_case(sigv4_post_header_key_sort_test)
add_test_case(sigv4_post_header_value_case_test)
add_test_case(sigv4_post_vanilla_test)
add_test_case(sigv4_post_vanilla_empty_query_value_test)
add_test_case(sigv4_post_vanilla_query_test)
add_test_case(sigv4_post_x_www_form_urlencoded_test)
add_test_case(sigv4_post_x_www_form_urlencoded_parameters_test)
add_test_case(sigv4_post_sts_header_after_test)
add_test_case(sigv4_post_sts_header_before_test)
add_test_case(sigv4_get_relative_normalized_test)
add_test_case(sigv4_get_relative_unnormalized_test)
add_test_case(sigv4_get_relative_relative_normalized_test)
add_test_case(sigv4_get_relative_relative_unnormalized_test)
add_test_case(sigv4_get_slash_normalized_test)
add_test_case(sigv4_get_slash_unnormalized_test)
add_test_case(sigv4_get_slash_dot_slash_normalized_test)
add_test_case(sigv4_get_slash_dot_slash_unnormalized_test)
add_test_case(sigv4_get_slash_pointless_dot_normalized_test)
add_test_case(sigv4_get_slash_pointless_dot_unnormalized_test)
add_test_case(sigv4_get_slashes_normalized_test)
add_test_case(sigv4_get_slashes_unnormalized_test)
add_test_case(sigv4_get_space_normalized_test)
add_test_case(sigv4_get_space_unnormalized_test)

set(TEST_BINARY_NAME ${PROJECT_NAME}-tests)
generate_test_driver(${TEST_BINARY_NAME})

# sigv4 test suite files
add_custom_command(TARGET ${TEST_BINARY_NAME} PRE_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
    ${CMAKE_CURRENT_SOURCE_DIR}/aws-signing-test-suite $<TARGET_FILE_DIR:${TEST_BINARY_NAME}>)

file(GLOB FUZZ_TESTS "fuzz/*.c")
aws_add_fuzz_tests("${FUZZ_TESTS}" "" "${CMAKE_CURRENT_SOURCE_DIR}/fuzz/corpus")
