# Tests for the gssopaque plug-in. Linked directly against the plug-in's
# objects so the gss_* symbols come from our build, not from the system
# MIT libgssapi_krb5 (which would also export them).
#
# fake_context_rejects_ke3 counts the plug-in's calls of opaque_server_finish
# through __wrap_opaque_server_finish: an unknown user's KE3 must still run
# the MAC check (RFC 9807 §10.9), which no GSS-API result shows. --wrap only
# redirects calls the linker sees as undefined references; with link-time
# optimisation (Arch's and openSUSE's default flags) the plug-in's calls are
# bound to opaque_server_finish directly and the counter stays at 0. So the
# test links its own copy of opaque-mech.c and backend.c built with -fno-lto;
# the shipped gssopaque.so keeps the distribution's flags.

add_library(opaque_backend_test STATIC ${CMAKE_SOURCE_DIR}/libopaque/backend.c)
target_include_directories(opaque_backend_test PUBLIC ${CMAKE_SOURCE_DIR}/driver)
target_link_libraries(opaque_backend_test PUBLIC PkgConfig::LIBOPAQUE PkgConfig::SODIUM)

add_library(gssopaque_test_obj OBJECT ${CMAKE_SOURCE_DIR}/gss-opaque/opaque-mech.c)
target_include_directories(gssopaque_test_obj PUBLIC ${CMAKE_SOURCE_DIR}/gss-opaque)
target_compile_definitions(gssopaque_test_obj PRIVATE _POSIX_C_SOURCE=200809L)
if(OPAQUE_MEASUREMENT_TIMESTAMPS)
    target_compile_definitions(gssopaque_test_obj PRIVATE OPAQUE_MEASUREMENT_TIMESTAMPS)
endif()
target_link_libraries(gssopaque_test_obj PUBLIC
    opaque_backend_test
    PkgConfig::LIBOPAQUE
    PkgConfig::SODIUM
    PkgConfig::GSSAPI)

add_executable(test-gssopaque test-gssopaque.c)

target_link_libraries(test-gssopaque PRIVATE gssopaque_test_obj)

# The default acceptor credential (gss_acquire_cred(GSS_C_ACCEPT), the lazy
# credential) reads DEFAULT_MASTER_KEY_PATH and DEFAULT_RECORDS_DIR. Here they
# point into the build tree, where the tests create them, so the refusal of a
# nameless acceptor is tested with a readable store and not hidden by a
# missing /etc/opaque/master.key.
set(TEST_DEFAULT_ACCEPTOR_DIR ${CMAKE_CURRENT_BINARY_DIR}/default-acceptor)
foreach(test_target IN ITEMS gssopaque_test_obj test-gssopaque)
    target_compile_definitions(${test_target} PRIVATE
        DEFAULT_MASTER_KEY_PATH="${TEST_DEFAULT_ACCEPTOR_DIR}/master.key"
        DEFAULT_RECORDS_DIR="${TEST_DEFAULT_ACCEPTOR_DIR}/records")
endforeach()

foreach(test_target IN ITEMS opaque_backend_test gssopaque_test_obj test-gssopaque)
    target_compile_options(${test_target} PRIVATE -fno-lto)
endforeach()
target_link_options(test-gssopaque PRIVATE -fno-lto "LINKER:--wrap=opaque_server_finish"
    "LINKER:--wrap=opaque_client_ke1" "LINKER:--wrap=opaque_server_ke2")

# POSIX.1-2008 for mkdtemp(3); same precedent as opaque_cfrg's _BSD_SOURCE.
target_compile_definitions(test-gssopaque PRIVATE _POSIX_C_SOURCE=200809L)

add_test(NAME gssopaque_unit COMMAND test-gssopaque)

# pkg-config supplies libopaque/liboprf as -L/-l flags, so no RPATH is
# recorded and the loader cannot find them from their install prefix.
set_tests_properties(gssopaque_unit PROPERTIES
    ENVIRONMENT "LD_LIBRARY_PATH=${LIBOPAQUE_LIBRARY_DIRS}")
