add_executable(test_m1a test_m1a.cpp)
target_link_libraries(test_m1a PRIVATE tlsfp_core)
if(nlohmann_json_FOUND)
  target_link_libraries(test_m1a PRIVATE nlohmann_json::nlohmann_json)
endif()

add_test(NAME m1a COMMAND test_m1a)
set_tests_properties(m1a PROPERTIES
  ENVIRONMENT "TLSFP_LAB_DIR=/home/liupeitao/projects/tlsfp-lab;TLSFP_PROFILE_DIR=${CMAKE_SOURCE_DIR}/profiles;TLSFP_TEST_DATA=${CMAKE_CURRENT_SOURCE_DIR}/data")

add_test(NAME m1a_tshark
         COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/verify_tshark.py $<TARGET_FILE:tlsfp-ch>)
set_tests_properties(m1a_tshark PROPERTIES SKIP_RETURN_CODE 77)

# Offline certificate-verification suite: local loopback TLS server with
# locally generated certificates, no network. Must never be skipped.
add_test(NAME tls_verify
         COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/tls_verify_test.py $<TARGET_FILE:tlsfp>)

add_test(NAME live_peet
         COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/live_peet.py $<TARGET_FILE:tlsfp>)
set_tests_properties(live_peet PROPERTIES SKIP_RETURN_CODE 77)

# CertificateVerify (TLS 1.3 4.4.3) unit tests: real verifier, generated keys.
add_executable(test_certverify test_certverify.cpp)
target_link_libraries(test_certverify PRIVATE tlsfp_core)
add_test(NAME certverify COMMAND test_certverify)

# M2: async epoll client + h2 multiplexing against a local loopback origin.
add_executable(test_m2 test_m2.cpp)
target_link_libraries(test_m2 PRIVATE tlsfp_core)
if(nlohmann_json_FOUND)
  target_link_libraries(test_m2 PRIVATE nlohmann_json::nlohmann_json)
endif()
add_test(NAME m2_async
         COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/m2_async_test.py $<TARGET_FILE:test_m2>)

# RFC 8879 CompressedCertificate: brotli/zlib/zstd decode, layout, fail-closed.
add_executable(test_compressed_cert test_compressed_cert.cpp)
target_link_libraries(test_compressed_cert PRIVATE tlsfp_core ${BROTLI_DEC_LIBRARY}
                      ${BROTLI_COMMON_LIBRARY})
target_include_directories(test_compressed_cert PRIVATE ${BROTLI_INCLUDE_DIR})
find_library(BROTLI_ENC_LIBRARY brotlienc)
if(BROTLI_ENC_LIBRARY)
  target_link_libraries(test_compressed_cert PRIVATE ${BROTLI_ENC_LIBRARY})
else()
  target_compile_definitions(test_compressed_cert PRIVATE TLSFP_NO_BROTLI_ENC)
endif()
if(ZLIB_FOUND)
  target_link_libraries(test_compressed_cert PRIVATE ZLIB::ZLIB)
endif()
if(TLSFP_WITH_ZSTD AND ZSTD_LIBRARY)
  target_link_libraries(test_compressed_cert PRIVATE ${ZSTD_LIBRARY})
  target_include_directories(test_compressed_cert PRIVATE ${ZSTD_INCLUDE_DIR})
  # IKY8QL: the zstd case is built only when the library itself was. A
  # no-libzstd build must still compile and pass the brotli/zlib cases.
  target_compile_definitions(test_compressed_cert PRIVATE TLSFP_HAVE_ZSTD)
endif()
target_compile_definitions(test_compressed_cert PRIVATE
  TLSFP_TEST_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/data")
add_test(NAME compressed_cert COMMAND test_compressed_cert)

# Review hardening C: HTTP/2 control-frame budget (PING/SETTINGS/WU floods are
# refused, DATA/GOAWAY and ordinary traffic are never charged).
add_executable(test_h2_budget test_h2_budget.cpp)
target_link_libraries(test_h2_budget PRIVATE tlsfp_core)
add_test(NAME h2_budget COMMAND test_h2_budget)

# Iteration 3: HTTP client completeness (redirects, cookies, decoding,
# chunked, streaming, limits, timeouts) against loopback TLS origins.
add_test(NAME http_client
         COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/http_client_test.py $<TARGET_FILE:tlsfp>)

# Fingerprint ladder (IKKX3I7): fixed rung order, host memory, pool identity.
add_executable(test_ladder test_ladder.cpp)
target_link_libraries(test_ladder PRIVATE tlsfp_core)
add_test(NAME ladder_unit COMMAND test_ladder)

add_executable(test_tls12_state test_tls12_state.cpp)
target_link_libraries(test_tls12_state PRIVATE tlsfp_core)
add_test(NAME tls12_state COMMAND test_tls12_state)

add_test(NAME ladder
         COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/ladder_test.py $<TARGET_FILE:tlsfp>)

# IKX691: multi-origin batch driver. Default path (no extra requests), TLS 1.2
# host memory, fingerprint ladder, https->http downgrade and lane concurrency.
# Loopback origins only; TLS1.2 checks skip themselves in a build without the
# BoringSSL TLS 1.2 backend.
add_test(NAME multi_origin
         COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/multi_origin_test.py
                 $<TARGET_FILE:tlsfp-multi>)

# IKX690: the multi-origin public API (tlsfp::MultiAsyncClient) and its asyncio
# binding. Loopback origins only. The Python half runs when this build also
# produced libtlsfp.so (the ctypes binding needs the shared C ABI).
set(_multi_args $<TARGET_FILE:tlsfp-multi-async>)
if(TLSFP_BUILD_SHARED)
  list(APPEND _multi_args $<TARGET_FILE:tlsfp_shared>)
else()
  list(APPEND _multi_args "")
endif()
add_test(NAME multi_async
         COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/multi_async_test.py ${_multi_args})
set_tests_properties(multi_async PROPERTIES
  ENVIRONMENT "PYTHONPATH=${CMAKE_SOURCE_DIR}/python"
  TIMEOUT 300)

# Tunnel-level reuse: h2 multiplexing + h1 keep-alive on one connection, a
# same-origin 302 on the open session, and both proxy kinds (HTTP Basic CONNECT
# and SOCKS5 RFC 1929). Loopback origins only; needs the shared C ABI.
if(TLSFP_BUILD_SHARED)
  add_test(NAME reuse_client
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/reuse_client_test.py
                   $<TARGET_FILE:tlsfp_shared>)
  set_tests_properties(reuse_client PROPERTIES
    ENVIRONMENT "PYTHONPATH=${CMAKE_SOURCE_DIR}/python"
    TIMEOUT 300)

  # IKYBOY: transport retries and redirects share one absolute deadline; a
  # silent origin costs one timeout_ms budget, while a first RST still retries.
  add_test(NAME reuse_deadline
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/reuse_deadline_test.py
                   $<TARGET_FILE:tlsfp_shared>)
  set_tests_properties(reuse_deadline PROPERTIES
    ENVIRONMENT "PYTHONPATH=${CMAKE_SOURCE_DIR}/python"
    TIMEOUT 30)

  # IKY994: a TLS1.2-only origin answers 200 on the reuse path, the fallback is
  # remembered per host (one connect on the next dial), and a pure timeout is
  # still a timeout. Only in a build with the TLS 1.2 backend.
  if(TLSFP_BUILD_TLS12_BORINGSSL)
    add_test(NAME reuse_tls12_fallback
             COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/reuse_tls12_fallback_test.py
                     $<TARGET_FILE:tlsfp_shared>)
    set_tests_properties(reuse_tls12_fallback PROPERTIES
      ENVIRONMENT "PYTHONPATH=${CMAKE_SOURCE_DIR}/python;TLSFP_TLS12_BORING_LIB=$<TARGET_FILE:tlsfp_tls12_boring>"
      TIMEOUT 180)
  endif()

  # IKY0X0: a stream stopped locally by max_body_bytes sends RST_STREAM(CANCEL)
  # so the origin's stream ends; the connection keeps serving later streams.
  add_test(NAME h2_abort
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/h2_abort_test.py
                   $<TARGET_FILE:tlsfp_shared>)
  set_tests_properties(h2_abort PROPERTIES
    ENVIRONMENT "PYTHONPATH=${CMAKE_SOURCE_DIR}/python"
    TIMEOUT 120)

  # IKY0XC: N tunnels per origin over N egress proxies, failure isolation and
  # per-tunnel counters. One TLS origin + local CONNECT proxies, loopback only.
  add_test(NAME reuse_tunnel_pool
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/reuse_tunnel_pool_test.py
                   $<TARGET_FILE:tlsfp_shared>)
  set_tests_properties(reuse_tunnel_pool PROPERTIES
    ENVIRONMENT "PYTHONPATH=${CMAKE_SOURCE_DIR}/python"
    TIMEOUT 300)

  # IKY0YG: max_conns is per origin. Two origins hold eight tunnels each with
  # the default cap, and adaptive handshake workers grow predictably.
  add_test(NAME reuse_per_origin
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/reuse_per_origin_test.py
                   $<TARGET_FILE:tlsfp_shared>)
  set_tests_properties(reuse_per_origin PROPERTIES
    ENVIRONMENT "PYTHONPATH=${CMAKE_SOURCE_DIR}/python"
    TIMEOUT 120)

  # IKY0YE: a CONNECT that times out must release its tunnel slot, so N
  # tunnels recover after a hanging proxy instead of collapsing to N-1.
  add_test(NAME reuse_tunnel_recovery
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/reuse_tunnel_recovery_test.py
                   $<TARGET_FILE:tlsfp_shared>)
  set_tests_properties(reuse_tunnel_recovery PROPERTIES
    ENVIRONMENT "PYTHONPATH=${CMAKE_SOURCE_DIR}/python"
    TIMEOUT 300)

  # IKY0Z9/IKY0ZA: the pool dials missing slots without request pressure, and
  # an egress whose origin-reset rate crosses the threshold is struck out,
  # rotated and rebuilt. Loopback h2 origin + CONNECT proxies + black hole.
  add_test(NAME reuse_pool_selfheal
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/reuse_pool_selfheal_test.py
                   $<TARGET_FILE:tlsfp_shared>)
  set_tests_properties(reuse_pool_selfheal PROPERTIES
    ENVIRONMENT "PYTHONPATH=${CMAKE_SOURCE_DIR}/python"
    TIMEOUT 300)

  # IKY14E: a slot whose egress completes the handshake and then stops
  # answering produces no RST/429/dead-conn event, so the old health rules
  # never saw it. One silent egress out of 8 must be struck and rebuilt inside
  # 30s while the 7 healthy siblings keep serving; a slow-but-alive origin and
  # a fleet-wide silent origin must rotate nothing.
  add_test(NAME reuse_no_progress
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/reuse_no_progress_strike_test.py
                   $<TARGET_FILE:tlsfp_shared>)
  set_tests_properties(reuse_no_progress PROPERTIES
    ENVIRONMENT "PYTHONPATH=${CMAKE_SOURCE_DIR}/python"
    TIMEOUT 300)
  # IKY5FD: the fingerprint booked on a connection and the resumption cache
  # key both come from the identity the dial was dispatched with, never from
  # the tunnel's mutable fields. >= 100 sampled (profile, JA4) pairs under
  # profile churn must agree, and every wire ClientHello must be a known
  # profile fingerprint.
  add_test(NAME reuse_profile_identity
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/reuse_profile_identity_test.py
                   $<TARGET_FILE:tlsfp_shared>)
  set_tests_properties(reuse_profile_identity PROPERTIES
    ENVIRONMENT "PYTHONPATH=${CMAKE_SOURCE_DIR}/python"
    TIMEOUT 600)

  # IKY9K7: a dial that crosses a strike must not rebind the slot to the old
  # profile. The fault injector strikes the first dial after dispatch, so the
  # late completion deterministically arrives in a newer slot epoch.
  add_test(NAME reuse_stale_dial
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/reuse_stale_dial_test.py
                   $<TARGET_FILE:tlsfp_shared>)
  set_tests_properties(reuse_stale_dial PROPERTIES
    ENVIRONMENT "PYTHONPATH=${CMAKE_SOURCE_DIR}/python"
    TIMEOUT 300)

  # IKY6HL: the TLS ticket cache key is (origin, profile), not the egress. A
  # random-egress run must resume (>= 80%), a rejected ticket must fall back to
  # a full handshake and be counted, and hash_by_egress must stay full-handshake.
  add_test(NAME reuse_resumption
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/reuse_resumption_test.py
                   $<TARGET_FILE:tlsfp_shared>)
  set_tests_properties(reuse_resumption PROPERTIES
    ENVIRONMENT "PYTHONPATH=${CMAKE_SOURCE_DIR}/python"
    TIMEOUT 600)

  # IKY8MH: a TLS 1.3 ticket is single-use, so one ticket per (origin,
  # profile) left a burst of rotating tunnels presenting the same spent
  # ticket. The pool hands out up to pool_size tickets oldest-first; the unit
  # test pins the mechanism (32 concurrent takes => exactly min(pool, 32)
  # presentations), the integration test pins the steady-state rate.
  add_executable(test_ticket_pool test_ticket_pool.cpp)
  target_link_libraries(test_ticket_pool PRIVATE tlsfp_core)
  add_test(NAME ticket_pool_unit COMMAND test_ticket_pool)

  add_test(NAME ticket_pool
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/ticket_pool_test.py
                   $<TARGET_FILE:tlsfp_shared>)
  set_tests_properties(ticket_pool PROPERTIES
    ENVIRONMENT "PYTHONPATH=${CMAKE_SOURCE_DIR}/python"
    TIMEOUT 600)

  # IKY0YF: statistics ABI. The legacy call must not write past the 152-byte
  # struct an old client compiled against; v2 negotiates size explicitly.
  add_executable(c_stats_abi_test c_stats_abi_test.c)
  target_link_libraries(c_stats_abi_test PRIVATE tlsfp_shared)
  add_test(NAME c_stats_abi COMMAND c_stats_abi_test)

  add_executable(c_stats_v2_test c_stats_v2_test.c)
  target_link_libraries(c_stats_v2_test PRIVATE tlsfp_shared)
  add_test(NAME c_stats_v2 COMMAND c_stats_v2_test)

  # IKY15M: per-tunnel stats ABI. The legacy symbol keeps the 64-byte element
  # (and 64-byte stride) of an old header; v2 negotiates the element size.
  add_executable(c_tunnel_stats_v2_test c_tunnel_stats_v2_test.c)
  target_link_libraries(c_tunnel_stats_v2_test PRIVATE tlsfp_shared)
  add_test(NAME c_tunnel_stats_v2 COMMAND c_tunnel_stats_v2_test)

  # IKY13R: one fingerprint profile per tunnel, constant for that tunnel's
  # connection generation. The origin tees the ClientHello, so "each tunnel
  # keeps one JA4" is checked on the wire, not from the library's own stats.
  add_test(NAME reuse_fingerprint_per_tunnel
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/reuse_fingerprint_per_tunnel_test.py
                   $<TARGET_FILE:tlsfp_shared>)
  set_tests_properties(reuse_fingerprint_per_tunnel PROPERTIES
    ENVIRONMENT "PYTHONPATH=${CMAKE_SOURCE_DIR}/python"
    TIMEOUT 600)

  # IKY55K: request policy -- reuse_mode (persistent/burst/per_request),
  # streams_per_tunnel, pace (interval + jitter) and profile_policy
  # (round_robin/hash_by_egress). Measured at the origin: requests per TCP
  # connection, request arrival times, and one JA4 per egress. IKY6OZ also
  # compares 100 gaps from three independent entropy-seeded processes and
  # checks exact replay for an explicit seed.
  add_executable(pace_jitter_probe pace_jitter_probe.cpp)
  target_include_directories(pace_jitter_probe PRIVATE ${CMAKE_SOURCE_DIR}/src)
  add_test(NAME reuse_request_policy
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/reuse_request_policy_test.py
                   $<TARGET_FILE:tlsfp_shared> $<TARGET_FILE:pace_jitter_probe>)
  set_tests_properties(reuse_request_policy PROPERTIES
    ENVIRONMENT "PYTHONPATH=${CMAKE_SOURCE_DIR}/python"
    TIMEOUT 600)
endif()

# IKX9PP: the TLS 1.2 fallback hop must report the same socket byte caliber as
# the TLS 1.3 path. A counting TCP proxy in front of a TLS1.2-only origin pins
# the counters to the wire; a 1000-URL mixed batch checks row sums and totals.
# Registered only when the build has the BoringSSL backend it exercises.
if(TLSFP_BUILD_TLS12_BORINGSSL)
  set(_t12bytes_args $<TARGET_FILE:tlsfp-multi-async>)
  if(TLSFP_BUILD_SHARED)
    list(APPEND _t12bytes_args $<TARGET_FILE:tlsfp_shared>)
  else()
    list(APPEND _t12bytes_args "")
  endif()
  add_test(NAME tls12_socket_bytes
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/tls12_socket_bytes_test.py
                   ${_t12bytes_args})
  set_tests_properties(tls12_socket_bytes PROPERTIES
    ENVIRONMENT "PYTHONPATH=${CMAKE_SOURCE_DIR}/python"
    TIMEOUT 300)
endif()

# Iteration 55 (IKX3I5): one TLS handshake retry with the same ClientHello
# after alert 50 / a close before ServerHello. Loopback origins, no network.
add_test(NAME tls_retry
         COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/tls_retry_test.py $<TARGET_FILE:tlsfp>)
set_tests_properties(tls_retry PROPERTIES TIMEOUT 180)

# Iteration 55 (IKX3I6-A): HelloRetryRequest parsing + P-384/P-256 retry and
# the stable unsupported-group error. Loopback origin pinned to one EC group.
add_test(NAME hrr
         COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/hrr_test.py $<TARGET_FILE:tlsfp>)
set_tests_properties(hrr PROPERTIES TIMEOUT 180)

# Iteration 49 (IKX1PQ): timeout=N is an end-to-end cap (DNS+connect+TLS+body)
# and the response exposes dns_ms/connect_ms/tls_ms/ttfb_ms/body_ms/total_ms.
# Slow loopback H1/H2 origin; Python checks run when the shared library and the
# native extension are part of this build.
set(_timeout_args $<TARGET_FILE:tlsfp>)
if(TLSFP_BUILD_SHARED)
  list(APPEND _timeout_args $<TARGET_FILE:tlsfp_shared>)
else()
  list(APPEND _timeout_args "")
endif()
if(TARGET _tlsfp_native)
  list(APPEND _timeout_args $<TARGET_FILE_DIR:_tlsfp_native>)
else()
  list(APPEND _timeout_args "")
endif()
add_test(NAME timeout
         COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/timeout_test.py ${_timeout_args})
set_tests_properties(timeout PROPERTIES
  ENVIRONMENT "PYTHONPATH=${CMAKE_SOURCE_DIR}/python"
  TIMEOUT 300)

# Iteration 32: http:// + Content-Encoding fallback + TLS version reporting.
# Pure unit tests (no sockets): decode.cpp and tls_parse.cpp directly.
add_executable(test_http_compat test_http_compat.cpp)
target_link_libraries(test_http_compat PRIVATE tlsfp_core)
if(ZLIB_FOUND)
  target_link_libraries(test_http_compat PRIVATE ZLIB::ZLIB)
endif()
add_test(NAME http_compat COMMAND test_http_compat)

# Iteration 4: stable C ABI shared library. The C binary asserts over the C
# ABI; the Python harness supplies a loopback TLS origin + throw-away CA.
# Live fingerprints stay opt-in (TLSFP_LIVE=1).
if(TLSFP_BUILD_SHARED)
  add_executable(c_api_test c_api_test.c)
  target_link_libraries(c_api_test PRIVATE tlsfp_shared)
  add_test(NAME c_api
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/c_api_test.py $<TARGET_FILE:c_api_test>)
  set_tests_properties(c_api PROPERTIES TIMEOUT 240)
endif()

# Iteration 47: streaming C ABI (tlsfp_perform_stream) against loopback H1/H2
# origins: >8 MiB bodies, chunked, gzip, callback abort, max bytes, memory.
if(TLSFP_BUILD_SHARED)
  add_executable(stream_test stream_test.c)
  target_link_libraries(stream_test PRIVATE tlsfp_shared)
  add_test(NAME stream
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/stream_test.py $<TARGET_FILE:stream_test>)
  set_tests_properties(stream PROPERTIES TIMEOUT 300)
endif()

# IKXEHF: authenticated proxies (RFC 7617 Basic for http:// CONNECT, RFC
# 1929 for socks5://) against loopback proxies only. Also asserts that no
# credential reaches stdout/stderr/JSON.
add_test(NAME proxy_auth
         COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/proxy_auth_test.py
                 $<TARGET_FILE:tlsfp>)
set_tests_properties(proxy_auth PROPERTIES TIMEOUT 180)

# Iteration 6: reverse-engineering toolkit (fpcheck / tlsfp-import / HAR).
add_test(NAME re_toolkit
         COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/re_toolkit_test.py
                 $<TARGET_FILE:fpcheck> $<TARGET_FILE:tlsfp-import> $<TARGET_FILE:tlsfp>)
set_tests_properties(re_toolkit PROPERTIES TIMEOUT 240)

# Iteration 4: pure-stdlib ctypes binding (python/tlsfp.py) over libtlsfp.so.
if(TLSFP_BUILD_SHARED)
  add_test(NAME python_api
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/python_api_test.py
                   $<TARGET_FILE:tlsfp_shared>)
  set_tests_properties(python_api PROPERTIES
    ENVIRONMENT "PYTHONPATH=${CMAKE_SOURCE_DIR}/python"
    TIMEOUT 240)

  # IKY9SH: an unset read_timeout inherits `timeout` instead of a hidden
  # 20 s, on both the C ABI and the Python bindings. A loopback origin holds
  # its first byte for 25 s; the four slow cases run concurrently.
  add_test(NAME read_timeout
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/read_timeout_test.py
                   $<TARGET_FILE:tlsfp_shared>)
  set_tests_properties(read_timeout PROPERTIES
    ENVIRONMENT "PYTHONPATH=${CMAKE_SOURCE_DIR}/python"
    TIMEOUT 180)

  # Iteration 21: requests-compatible layer (python/compat/requests). Offline
  # loopback origin; also runs the suite through the native extension when the
  # build has one, and diffs against PyPI requests in a clean subprocess.
  set(_compat_args)
  if(TARGET _tlsfp_native)
    list(APPEND _compat_args --native-dir $<TARGET_FILE_DIR:_tlsfp_native>)
  endif()
  add_test(NAME requests_compat
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/requests_compat_test.py
                   $<TARGET_FILE:tlsfp_shared> ${_compat_args})
  set_tests_properties(requests_compat PROPERTIES
    ENVIRONMENT "PYTHONPATH=${CMAKE_SOURCE_DIR}/python/compat:${CMAKE_SOURCE_DIR}/python"
    TIMEOUT 900)

  # Iteration 18: synchronous keep-alive reuse, native cookie jar, throughput.
  add_test(NAME sync_reuse
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/sync_reuse_test.py
                   $<TARGET_FILE:tlsfp_shared>)
  set_tests_properties(sync_reuse PROPERTIES TIMEOUT 240)
  # IKXFNV: a same-origin redirect reuses the hop-1 connection (one TCP+TLS
  # connection for two requests); a cross-origin redirect does not.
  add_test(NAME redirect_reuse
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/redirect_reuse_test.py
                   $<TARGET_FILE:tlsfp_shared>)
  set_tests_properties(redirect_reuse PROPERTIES TIMEOUT 240)
  add_test(NAME cookie_jar
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/cookie_jar_test.py
                   $<TARGET_FILE:tlsfp_shared>)
  set_tests_properties(cookie_jar PROPERTIES TIMEOUT 240)
  add_test(NAME sync_perf
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/sync_perf_test.py
                   $<TARGET_FILE:tlsfp_shared>)
  set_tests_properties(sync_perf PROPERTIES TIMEOUT 300)
endif()

# IKY6EV: AsyncSession drives the ReuseClient pool by default; this is the
# transport switch + connection-count/cookie/timeout/cancel/stream gate.
if(TARGET _tlsfp_native)
  add_test(NAME async_reuse
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/async_reuse_test.py
                   $<TARGET_FILE_DIR:_tlsfp_native>)
  set_tests_properties(async_reuse PROPERTIES TIMEOUT 300)
  # IKY6EV rework: the four reviewer blockers, measured against the lane
  # transport on the same origin/URL order/cookie scenario rather than against
  # hand-written expectations.
  add_test(NAME async_reuse_parity
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/async_reuse_parity_test.py
                   $<TARGET_FILE_DIR:_tlsfp_native>)
  set_tests_properties(async_reuse_parity PROPERTIES TIMEOUT 300)
  # IKY8QK: a reused request must not replay its non-idempotent hops, and the
  # redirect contract (HEAD+303, r.redirects, RedirectError, self-loop) has to
  # match the lane transport hop for hop.
  add_test(NAME async_reuse_redirect
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/async_reuse_redirect_test.py
                   $<TARGET_FILE_DIR:_tlsfp_native>)
  set_tests_properties(async_reuse_redirect PROPERTIES TIMEOUT 300)
  # IKY8QL: `content-encoding: zstd` decodes on both transports, and a build
  # without libzstd neither advertises zstd nor breaks the gzip path.
  if(TLSFP_BUILD_SHARED)
    # IKY8QL: in a build without libzstd the same test asserts the opposite
    # half -- no zstd in Accept-Encoding, gzip unchanged.
    set(_zstd_test_args "")
    if(NOT (TLSFP_WITH_ZSTD AND ZSTD_LIBRARY))
      set(_zstd_test_args --no-zstd-build)
    endif()
    add_test(NAME zstd_content_encoding
             COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/zstd_content_encoding_test.py
                     $<TARGET_FILE:tlsfp_shared> ${_zstd_test_args})
    set_tests_properties(zstd_content_encoding PROPERTIES TIMEOUT 120)
  endif()
endif()

# Iteration 18: builtin profile JA4/Akamai (firefox-151, edge-152, aliases).
add_test(NAME profiles
         COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/profile_fingerprint_test.py
                 $<TARGET_FILE:fpcheck>)
set_tests_properties(profiles PROPERTIES TIMEOUT 120)

# Iteration 7: libFuzzer harnesses. Built only with -DTLSFP_FUZZ=ON (Clang);
# ctest never runs them (fuzzing is time-based), scripts/fuzz.sh drives them.
if(TLSFP_FUZZ)
  foreach(fuzz_target fuzz_h2_frames fuzz_tls_handshake fuzz_http_parse fuzz_ws_frames)
    add_executable(${fuzz_target} fuzz/${fuzz_target}.cpp)
    target_link_libraries(${fuzz_target} PRIVATE tlsfp_core)
    target_link_options(${fuzz_target} PRIVATE -fsanitize=fuzzer,address,undefined)
    set_target_properties(${fuzz_target} PROPERTIES
      RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/fuzz)
  endforeach()
endif()

# Iteration 27: async/epoll event-sequence stress driver. Never in ctest:
# tests/async_stress.py starts a TLS origin + fault proxy and drives it.
if(TLSFP_ASYNC_FUZZ)
  add_executable(async_fuzz async_fuzz.cpp)
  target_link_libraries(async_fuzz PRIVATE tlsfp_core)
  if(nlohmann_json_FOUND)
    target_link_libraries(async_fuzz PRIVATE nlohmann_json::nlohmann_json)
  endif()
endif()

# Negative-input parser checks (driven by tests/security_test.py; not part of
# the default ctest set so the iteration-6 suite stays 11/11).
add_executable(security_parse security_parse.cpp)
target_link_libraries(security_parse PRIVATE tlsfp_core)

# Iteration 8: Happy Eyeballs v4/v6, warm pool, non-blocking handshake.
add_executable(test_connect test_connect.cpp)
target_link_libraries(test_connect PRIVATE tlsfp_core)
if(nlohmann_json_FOUND)
  target_link_libraries(test_connect PRIVATE nlohmann_json::nlohmann_json)
endif()
add_test(NAME connect
         COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/connect_test.py $<TARGET_FILE:test_connect>)
set_tests_properties(connect PROPERTIES TIMEOUT 300)

# Iteration 9: WebSocket H1 Upgrade. The parser unit tests need no network; the
# python suite drives `tlsfp ws` against tests/ws_origin.py (TLS + plain).
add_executable(test_ws_frame test_ws_frame.cpp)
target_link_libraries(test_ws_frame PRIVATE tlsfp_core)
add_test(NAME ws_frame COMMAND test_ws_frame)
add_test(NAME websocket
         COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/ws_test.py $<TARGET_FILE:tlsfp>)
set_tests_properties(websocket PROPERTIES TIMEOUT 300)

# Iteration 10: permessage-deflate (RFC 7692) response parsing, context
# takeover, window bits, corrupt streams and the decoded-size bound.
add_executable(test_ws_deflate test_ws_deflate.cpp)
target_link_libraries(test_ws_deflate PRIVATE tlsfp_core)
add_test(NAME ws_deflate COMMAND test_ws_deflate)

# Iteration 10: RFC 8441 extended CONNECT against a Python h2 origin, plus
# the H1 fallback paths (no SETTINGS 0x8, CONNECT 404, --http1).
add_test(NAME ws_h2
         COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/ws_h2_test.py $<TARGET_FILE:tlsfp>)
set_tests_properties(ws_h2 PROPERTIES TIMEOUT 300)

# Iteration 12: HTTP/3 fingerprint profiles. Offline aioquic origin on
# loopback; checks profile application (QUIC TP, H3 SETTINGS, QPACK, header
# order), the server echo on the wire and fail-closed profile loading.
if(TARGET h3probe)
  add_test(NAME h3_profile
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/h3_profile_test.py
                   $<TARGET_FILE:h3probe>)
  set_tests_properties(h3_profile PROPERTIES TIMEOUT 300)
endif()

# Iteration 13: `tlsfp get --http3` end to end. Offline aioquic H3 origin plus
# a loopback TLS/TCP origin for the H2/H1 fallback. Live endpoints stay opt-in
# through TLSFP_H3_LIVE=1 and are never part of the default run.
if(TARGET tlsfp AND TARGET h3probe)
  add_test(NAME h3_cli
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/http3_cli_test.py
                   $<TARGET_FILE:tlsfp>)
  set_tests_properties(h3_cli PROPERTIES TIMEOUT 300)
endif()

# Iteration 24: HTTP/3 over a SOCKS5 UDP ASSOCIATE relay (RFC 1928). Offline
# loopback proxy (no-auth + RFC 1929) plus an aioquic H3 origin; injects auth
# failure, CMD rejection, missing UDP support, a silent relay, a fragmented
# reply and a closed control connection, and asserts every classification.
if(TARGET tlsfp AND TARGET h3probe)
  add_test(NAME h3_socks5
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/http3_socks5_test.py
                   $<TARGET_FILE:tlsfp>)
  set_tests_properties(h3_socks5 PROPERTIES TIMEOUT 300 SKIP_RETURN_CODE 77)
endif()

# Iteration 16: opt-in H3 TLS ClientHello profiles (tlsfp-h3-tls-profile/1).
# Offline aioquic origin; asserts the captured cipher list, extension set,
# groups, ALPS/trust-anchor payloads and ECH grease, plus fail-closed loading
# and an unchanged default ClientHello. Wire order is reported, not asserted.
if(TARGET tlsfp AND TARGET h3probe)
  add_test(NAME h3_tls_profile
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/http3_tls_test.py
                   $<TARGET_FILE:h3probe> $<TARGET_FILE:tlsfp>)
  # The script resolves profile names; pin the directory so an out-of-tree
  # build directory (../profiles is then unrelated) still finds them.
  set_tests_properties(h3_tls_profile PROPERTIES TIMEOUT 300
    ENVIRONMENT "TLSFP_PROFILE_DIR=${CMAKE_SOURCE_DIR}/profiles")
endif()

# Iteration 14: multi-request streams on one QUIC connection + qlog. Opt-in
# (-DTLSFP_H3_MULTI_TESTS=ON) so the default suite keeps its iteration-13
# shape; the script itself skips with 77 when aioquic is missing.
option(TLSFP_H3_MULTI_TESTS "Register the opt-in h3_multi/qlog tests" OFF)
if(TLSFP_H3_MULTI_TESTS AND TARGET tlsfp AND TARGET h3probe)
  add_test(NAME h3_multi
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/http3_multi_test.py
                   $<TARGET_FILE:tlsfp>)
  set_tests_properties(h3_multi PROPERTIES TIMEOUT 300 SKIP_RETURN_CODE 77)
endif()

# Iteration 17: mTLS (client Certificate/CertificateVerify) and
# multipart/form-data. Both are offline loopback suites with generated certs.
add_test(NAME mtls
         COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/mtls_test.py $<TARGET_FILE:tlsfp>)
set_tests_properties(mtls PROPERTIES TIMEOUT 300)

add_test(NAME multipart
         COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/multipart_test.py $<TARGET_FILE:tlsfp>)
set_tests_properties(multipart PROPERTIES TIMEOUT 300)

# Iteration 20: native CPython extension + AsyncSession. Only present in
# builds configured with -DTLSFP_PYTHON=ON, so the default ctest list is
# unchanged (25 tests).
if(TLSFP_PYTHON)
  add_test(NAME python_native
           COMMAND ${Python3_EXECUTABLE}
                   ${CMAKE_CURRENT_SOURCE_DIR}/python_native_test.py
                   $<TARGET_FILE:_tlsfp_native>)

  # IKXEHG: a cancelled/timed-out request must not pin its native lane. One
  # lane, a stalled loopback origin, and the wall time of the next request.
  add_test(NAME async_cancel
           COMMAND ${Python3_EXECUTABLE}
                   ${CMAKE_CURRENT_SOURCE_DIR}/async_cancel_test.py
                   $<TARGET_FILE_DIR:_tlsfp_native>)
  set_tests_properties(async_cancel PROPERTIES
    ENVIRONMENT "PYTHONPATH=${CMAKE_SOURCE_DIR}/python"
    TIMEOUT 180)
endif()

# Iteration 23: dependency CVE scan. Fixture-only: no network, no cache writes
# outside a temp dir, so it runs in the default offline suite.
add_test(NAME cve_scan
         COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/cve_scan_test.py)
set_tests_properties(cve_scan PROPERTIES TIMEOUT 180)

# Iteration 26: TLS 1.3 0-RTT. The origin is a real OpenSSL server (early data
# read before the handshake completes, accept/reject modes, request counters);
# tests/early_data_test.py drives it through the tlsfp CLI.
add_executable(rtt0_origin rtt0_origin.cpp)
target_link_libraries(rtt0_origin PRIVATE tlsfp_core OpenSSL::SSL OpenSSL::Crypto Threads::Threads)
add_test(NAME early_data
         COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/early_data_test.py
                 $<TARGET_FILE:tlsfp> $<TARGET_FILE:rtt0_origin>)
set_tests_properties(early_data PROPERTIES TIMEOUT 300)

# Iteration 29: the opt-in BoringSSL HTTP/3 backend. Only registered when the
# backend is built (-DTLSFP_BUILD_H3_BORINGSSL=ON), so the default ctest list
# stays at 29 tests. Offline: aioquic origin + ClientHello capture; live
# (cloudflare-quic.com / quic.nginx.org) stays behind TLSFP_H3_LIVE=1.
if(TLSFP_BUILD_H3_BORINGSSL AND TLSFP_BUILD_H3)
  # IKX4XC: alert vs certificate-failure classification (no network).
  add_test(NAME h3_classify COMMAND $<TARGET_FILE:test_h3_classify>)
  add_test(NAME h3_boring
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/h3_boring_test.py
                   --phase2a $<TARGET_FILE:tlsfp>)
  set_tests_properties(h3_boring PROPERTIES TIMEOUT 600)
  # Phase 2b: multi-request/reuse, redirects, decompression, limits, SOCKS5
  # UDP and qlog. Split so a transport regression is attributable at a glance.
  add_test(NAME h3_boring_phase2b
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/h3_boring_test.py
                   --phase2b $<TARGET_FILE:tlsfp>)
  set_tests_properties(h3_boring_phase2b PROPERTIES TIMEOUT 600)
  # Phase 2c: PSK/1-RTT resumption (--session-cache), mTLS (--cert/--key),
  # --har and profile-level --seed.
  add_test(NAME h3_boring_phase2c
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/h3_boring_test.py
                   --phase2c $<TARGET_FILE:tlsfp>)
  set_tests_properties(h3_boring_phase2c PROPERTIES TIMEOUT 600)
  # Phase 2d: HTTP/3 0-RTT (--early-data + --session-cache). Needs a server
  # that decrypts 0-RTT packets, so the origin is the pinned quic-go program
  # under tests/origins/h3_0rtt_go. The test skips (does not fail) when the Go
  # toolchain or its module cache is unavailable.
  add_test(NAME h3_boring_phase2d
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/h3_boring_test.py
                   --phase2d $<TARGET_FILE:tlsfp>)
  set_tests_properties(h3_boring_phase2d PROPERTIES TIMEOUT 600)
endif()

# Iteration 44: opt-in TCP TLS 1.2 backend + `tlsfp get --tls12-fallback`.
# Only registered when the backend is part of the build; the default build is
# unchanged and never links BoringSSL.
if(TLSFP_BUILD_TLS12_BORINGSSL)
  add_test(NAME tls12
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/tls12_test.py
                   $<TARGET_FILE:tlsfp> $<TARGET_FILE:tls12probe>)
  set_tests_properties(tls12 PROPERTIES
    ENVIRONMENT "TLSFP_LAB_DIR=/home/liupeitao/projects/tlsfp-lab;TLSFP_PROFILE_DIR=${CMAKE_SOURCE_DIR}/profiles")
endif()

# Iteration 49 (IKX1PR): tls12_fallback through the Python bindings (ctypes,
# native, AsyncSession). The no-backend branch is asserted when the build has
# no TLS 1.2 backend, so this test is meaningful in both configurations.
if(TLSFP_BUILD_SHARED)
  set(_py_tls12_args $<TARGET_FILE:tlsfp_shared>)
  if(TARGET _tlsfp_native)
    list(APPEND _py_tls12_args $<TARGET_FILE_DIR:_tlsfp_native>)
  endif()
  if(TLSFP_BUILD_TLS12_BORINGSSL)
    list(APPEND _py_tls12_args --backend)
    if(TARGET tlsfp_tls12_boring)
      list(APPEND _py_tls12_args --plugin $<TARGET_FILE:tlsfp_tls12_boring>)
    endif()
  endif()
  add_test(NAME python_tls12
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/python_tls12_test.py ${_py_tls12_args})
  set_tests_properties(python_tls12 PROPERTIES
    ENVIRONMENT "PYTHONPATH=${CMAKE_SOURCE_DIR}/python"
    TIMEOUT 240)
endif()

# Iteration 45: opt-in TLS 1.1/1.0 legacy transport. Registered only in the
# -DTLSFP_BUILD_LEGACY_TLS=ON build; the default suite is unchanged.
if(TLSFP_BUILD_LEGACY_TLS)
  add_test(NAME legacy_tls
           COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/legacy_tls_test.py
                   $<TARGET_FILE:tlsfp>)
  set_tests_properties(legacy_tls PROPERTIES TIMEOUT 300)
endif()
