add_executable (test_cotp test_otp.c)
add_executable (test_base32encode test_base32encode.c)
add_executable (test_base32decode test_base32decode.c)
add_executable (test_base32_roundtrip test_base32_roundtrip.c)
add_executable (test_strerror test_strerror.c)
add_executable (test_otpauth_uri test_otpauth_uri.c)
add_executable (test_secure test_secure.c)
add_executable (test_yaotp test_yaotp.c)
add_executable (test_yaotp_uri test_yaotp_uri.c)

target_link_libraries (test_cotp PRIVATE cotp criterion)
target_link_libraries (test_base32encode PRIVATE cotp criterion)
target_link_libraries (test_base32decode PRIVATE cotp criterion)
target_link_libraries (test_base32_roundtrip PRIVATE cotp criterion)
target_link_libraries (test_strerror PRIVATE cotp criterion)
target_link_libraries (test_otpauth_uri PRIVATE cotp criterion)
target_link_libraries (test_secure PRIVATE cotp criterion)
target_link_libraries (test_yaotp PRIVATE cotp criterion)
target_link_libraries (test_yaotp_uri PRIVATE cotp criterion)

add_test (NAME TestCOTP COMMAND test_cotp)
add_test (NAME TestBase32Encode COMMAND test_base32encode)
add_test (NAME TestBase32Decode COMMAND test_base32decode)
add_test (NAME TestBase32Roundtrip COMMAND test_base32_roundtrip)
add_test (NAME TestStrerror COMMAND test_strerror)
add_test (NAME TestOTPAuthURI COMMAND test_otpauth_uri)
add_test (NAME TestSecure COMMAND test_secure)
add_test (NAME TestYAOTP COMMAND test_yaotp)
add_test (NAME TestYAOTPURI COMMAND test_yaotp_uri)

if (COTP_ENABLE_VALIDATION)
    add_executable (test_validation test_validation.c)
    target_link_libraries (test_validation PRIVATE cotp criterion)
    add_test (NAME TestValidation COMMAND test_validation)
endif()

# H2 regression: inject an allocation failure into finalize() and assert the
# callers report MEMORY_ALLOCATION_ERROR. The library sources are compiled into
# the test executable so that -Wl,--wrap=calloc catches library references
# regardless of BUILD_SHARED_LIBS. GNU ld/LLD specific; skipped elsewhere.
if (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
    # SOURCE_FILES paths are relative to the top-level source dir.
    set(_cotp_sources_abs "")
    foreach(_cotp_src IN LISTS SOURCE_FILES)
        list(APPEND _cotp_sources_abs "${CMAKE_SOURCE_DIR}/${_cotp_src}")
    endforeach()
    add_executable (test_alloc_failure test_alloc_failure.c ${_cotp_sources_abs})
    target_include_directories (test_alloc_failure PRIVATE
        ${CMAKE_SOURCE_DIR}/src
        ${HMAC_INCLUDE_DIR})
    target_link_libraries (test_alloc_failure PRIVATE criterion ${HMAC_LIBRARIES})
    target_link_options (test_alloc_failure PRIVATE "LINKER:--wrap=calloc")
    if (HAVE_EXPLICIT_BZERO)
        target_compile_definitions (test_alloc_failure PRIVATE HAVE_EXPLICIT_BZERO)
    endif()
    if (COTP_ENABLE_VALIDATION)
        target_compile_definitions (test_alloc_failure PRIVATE COTP_ENABLE_VALIDATION)
    endif()
    add_test (NAME TestAllocFailure COMMAND test_alloc_failure)
endif()
